Skip to content

Fix terminal cancellation in switchToLatest - #63

Merged
twittemb merged 1 commit into
mainfrom
codex/issue-53-switch-cancellation
Oct 3, 2026
Merged

twittemb merged 1 commit into
mainfrom
codex/issue-53-switch-cancellation

Conversation

@twittemb

@twittemb twittemb commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

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-coverage on the isolated PR checkout: 159 tests passed, zero failures.
  • Focused switch-to-latest and flat-map-latest tests: 18 passed.
  • Nine new cancellation tests under Thread Sanitizer: passed with no reported races.
  • swift build -Xswiftc -suppress-warnings: passed.
  • git diff --check: passed.
  • Before the fix, three new regression tests reproduced the two cancellation hangs and late error delivery.

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

  • this PR is based on the main branch and is up-to-date
  • the commits inside this PR have explicit commit messages
  • unit tests cover the bug fix
  • the feature is documented in the README.md if it makes sense — not applicable; this restores existing cancellation behavior without changing the API
  • the CHANGELOG is up-to-date

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.
@twittemb
twittemb merged commit 507ed0c into main Oct 3, 2026
2 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.

[BUG] switchToLatest collection can hang after cancellation

1 participant