Fix potential resource leaks in MessageFrameWSHandler stream management#208
Merged
swhitty merged 3 commits intoMay 16, 2026
Merged
Conversation
82f5843 to
a424932
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. |
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.
0bb096f to
f8ef945
Compare
f8ef945 to
e501c65
Compare
swhitty
approved these changes
May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
MessageFrameWSHandler.startto use a centralizeddeferblock for finishing bothmessagesInandframesOutcontinuations.CancellationError, preventing clean shutdowns from being reported as errors to the client.Rationale
The previous implementation lacked explicit termination for the internal
messagesInstream. This could lead toWSMessageHandlerimplementations 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
WSHandlerTestssuite (all tests passed).