Avoid holding MergeStorage lock across continuation registration - #444
Open
thliu21 wants to merge 2 commits into
Open
Avoid holding MergeStorage lock across continuation registration#444thliu21 wants to merge 2 commits into
thliu21 wants to merge 2 commits into
Conversation
FranzBusch
reviewed
Jul 22, 2026
Member
There was a problem hiding this comment.
We really should remove all the manual lock/unlock calls now and only use withLock
There was a problem hiding this comment.
I think we should also split this function up, as we did for AsyncStream and MPSC Channel.
NotTheNHK
reviewed
Jul 22, 2026
| // The continuation is registered after `next()` releases the lock. While | ||
| // it was unlocked, an upstream may have produced an element or finished. | ||
| if let element = buffer.popFirst() { | ||
| state = .merging( |
There was a problem hiding this comment.
I think it would be a good idea to eventually refactor this into a single type. We can do that in a separate PR.
thliu21
marked this pull request as ready for review
July 22, 2026 15:48
FranzBusch
approved these changes
Jul 23, 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
MergeStoragedownstream continuations after releasing the thread-affine lockRoot cause
MergeStorage.next()previously held its lock while enteringwithUnsafeThrowingContinuation. Swift 6.0–6.3.3 could re-enqueue before that closure executed, allowing a thread-affine lock to be unlocked from a different thread. The state machine now owns the race window explicitly, so continuation registration is protected by a short lock acquisition instead.Validation
swift test --filter TestMerge— 32 tests passedswift test— 362 tests passedgit diff --checkFixes #430.