Skip to content

Fix potential resource leaks in MessageFrameWSHandler stream management#208

Merged
swhitty merged 3 commits into
swhitty:mainfrom
phuccvx12:fix/ws-handler-stream-termination
May 16, 2026
Merged

Fix potential resource leaks in MessageFrameWSHandler stream management#208
swhitty merged 3 commits into
swhitty:mainfrom
phuccvx12:fix/ws-handler-stream-termination

Conversation

@phuccvx12

Copy link
Copy Markdown
Contributor

Summary

This PR fixes a potential resource leak and improves the reliability of WebSocket connection teardown in MessageFrameWSHandler. It ensures that all associated message streams are explicitly terminated and that task cancellation is handled gracefully without spurious errors.

Changes

  • WSHandler.swift: Refactored MessageFrameWSHandler.start to use a centralized defer block for finishing both messagesIn and framesOut continuations.
  • Cancellation Handling: Updated concurrent task loops to catch and ignore CancellationError, preventing clean shutdowns from being reported as errors to the client.
  • Compiler Safety: Standardized task group closures with exhaustive error handling to ensure they remain non-throwing as required by the Swift concurrency model.

Rationale

The previous implementation lacked explicit termination for the internal messagesIn stream. This could lead to WSMessageHandler implementations hanging indefinitely as they waited for a stream that would never end naturally, effectively leaking resources. Additionally, the lack of explicit cancellation handling meant that a successful shutdown in one task often triggered an error-state finish in its sibling task, causing inconsistent connection closure states.

Verification

  • Unit Tests: Verified correct behavior by running the WSHandlerTests suite (all tests passed).
  • Manual Verification: Confirmed that message handlers correctly exit their processing loops immediately upon connection closure, ensuring no orphaned tasks or leaked memory.

@phuccvx12 phuccvx12 force-pushed the fix/ws-handler-stream-termination branch from 82f5843 to a424932 Compare April 25, 2026 09:15
@codecov

codecov Bot commented Apr 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.75%. Comparing base (e173ffe) to head (e501c65).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #208      +/-   ##
==========================================
+ Coverage   92.67%   92.75%   +0.08%     
==========================================
  Files          69       69              
  Lines        3551     3549       -2     
==========================================
+ Hits         3291     3292       +1     
+ Misses        260      257       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

The `MessageFrameWSHandler.start` implementation previously lacked explicit
termination for the `messagesIn` stream, which could lead to message handlers
hanging indefinitely if they relied on the stream ending naturally. Additionally,
concurrent tasks could prematurely finish the `framesOut` stream with a
`CancellationError` when one task completed before the other.

This change:
- Uses `defer` blocks to guarantee that both `messagesIn` and `framesOut`
  continuations are finished when the handler exits.
- Filters out `CancellationError` during task cleanup to prevent reporting
  spurious errors to the client during a clean shutdown.
- Centralizes stream termination logic, removing redundant `.finish()` calls
  from individual tasks.
- Adds an exhaustive catch block to ensure the task group closures remain
  non-throwing as required by the Swift concurrency model.
@phuccvx12 phuccvx12 force-pushed the fix/ws-handler-stream-termination branch 3 times, most recently from 0bb096f to f8ef945 Compare April 25, 2026 10:04
@phuccvx12 phuccvx12 force-pushed the fix/ws-handler-stream-termination branch from f8ef945 to e501c65 Compare April 25, 2026 10:06
@swhitty swhitty merged commit 524d267 into swhitty:main May 16, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants