Conversation
Add ThrowIfCancellationRequested() check at the start of each MoveNextAsync call (in the active iteration path). This ensures that when a CancellationToken passed to GetAsyncEnumerator is cancelled, the next call to MoveNextAsync will throw OperationCanceledException. The check is a no-op for CancellationToken.None, so the common case of no cancellation token has no overhead beyond a single field read. Adds 8 tests covering: - Pre-cancelled token throws on first MoveNextAsync - Token cancelled mid-iteration throws on next MoveNextAsync - Normal iteration is unaffected (no token, CancellationToken.None) - Multiple independent enumerators respect independent tokens Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
11 tasks
dsyme
approved these changes
Mar 8, 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.
🤖 This is an automated PR from Repo Assist.
Closes #179
Summary
When a
CancellationTokenis passed toGetAsyncEnumerator(ct), it was stored in the state machine data but never checked during iteration. This meant that infinite or long-runningtaskSeqsequences could not be cancelled via the enumerator's token — the cancel request was silently ignored.Root Cause
In
TaskSeqBuilder.fs,MoveNextAsync()calleddata.builder.MoveNext()without first checking whetherdata.cancellationTokenhad been cancelled. The token was correctly stored and passed to async bindings (e.g.,Async<'T>computations and nestedIAsyncEnumerable<'T>sources), but a pure synchronous loop likewhile true do yield 1never reached an async checkpoint and therefore never saw the cancellation.Fix
Added a single
ThrowIfCancellationRequested()call at the start of the active iteration path inMoveNextAsync():ThrowIfCancellationRequested()is a no-op forCancellationToken.None(the common case), so there is no overhead beyond a single volatile boolean read perMoveNextAsynccall.Behaviour
MoveNextAsync()throwsOperationCanceledException— cooperative, not preemptive.completed = true) returnsfalsenormally even if the token is cancelled, since the enumeration is already done.CancellationToken.None.Tests
Added
TaskSeq.CancellationToken.Tests.fswith 8 new tests:MoveNextAsyncMoveNextAsyncCancellationToken.NoneMoveNextAsyncTest Status
✅ Build succeeded (Release)
✅ All 3858 tests pass (8 new + 3850 existing)
✅
dotnet fantomas . --checkpasses