Fix terminal cancellation in switchToLatest - #63
Merged
Merged
Conversation
Record cancellation in the protected iterator state, resume outer waiters, and cancel producer tasks outside the lock. Reject late transitions and replacement child tasks while preserving normal latest-child draining. Add nine regression tests, including non-cooperative outer sequences and cancellation racing delivery, and document the fix in the changelog.
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.
Description
Cancelling a
switchToLatest()collector while the outer stream and latest channel remain open could leave it suspended indefinitely. A cancelled child task could cause iteration to create a replacement task, and a consumer waiting for the outer sequence depended on that sequence cooperating with cancellation.Make cancellation a terminal state in the existing protected iterator state. Claim and resume the outer waiter directly, cancel producer tasks outside the lock, reject late outer transitions, and prevent replacement child tasks. Also clean up existing producer work when cancellation occurs between
next()calls. Normal outer completion still lets the latest inner sequence drain; public APIs and dependencies are unchanged.Add nine regression tests covering suspended open channels, a non-cooperative outer sequence, late outer and inner values/errors, cancellation before and between iterator calls, finite-outer draining, and 100 cancellation-versus-delivery races. Test gates are explicitly released during cleanup.
Closes #53.
Validation
swift test --enable-code-coverageon the isolated PR checkout: 159 tests passed, zero failures.swift build -Xswiftc -suppress-warnings: passed.git diff --check: passed.The full test build has 21 existing warnings in unrelated subject, multicast, and share tests; none are in the changed files. Focused and sanitizer validation used the same implementation and regression tests before isolating the PR from unrelated working-tree edits.
Cancellation remains cooperative for the inner task: a custom inner
next()that never returns despite cancellation can still delay the await of its task value. Any late result is discarded when it returns. The consumer waiting for an outer sequence now finishes independently of outer cooperation.Checklist