fix: stop iterAsync reliably and never lose a failure of its action - #38
Open
xperiandri wants to merge 1 commit into
Open
xperiandri wants to merge 1 commit into
xperiandri wants to merge 1 commit into
Conversation
Composing R3's `SelectAwait` with `ForEachAsync` left four gaps in both flavours of `iterAsync`: - Over a synchronous source, R3 attaches the terminal to the mapped stage only after `Subscribe` returns, so the action kept running for every element after it had thrown. - With an already cancelled token the action still ran for every element of a synchronous source, although the iteration was cancelled. - R3 drops a failure of the action that happens after the source completed, so the iteration completed successfully. - `SelectAwait` swallows an `OperationCanceledException`: a timeout such as HttpClient's `TaskCanceledException` stopped the sequential worker for good and `iterAsync` never completed. The action's exceptions no longer travel through R3 at all. An internal `IterationGuard` records the first one, skips the remaining actions and cancels an internal token that completes the iteration through `TakeUntil`, which also unsubscribes from the source at once; the iteration then fails with the recorded exception. A cancellation that R3 itself caused for an invocation (`AwaitSwitch`, `CancelOnCompleted`, disposal) is not a failure, and a cancelled token cancels the iteration without subscribing. The `iterAsync` docs now describe which elements reach the action, when the iteration completes, also with `CancelOnCompleted`, and how it fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 5, 2026
This was referenced Oct 5, 2026
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Concurrent cancellation can still hide an unrelated OperationCanceledException in both implementations.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Fixes iterAsync cancellation and failure propagation for both Async and Task APIs.
Changes:
- Adds an internal guard to stop iteration and preserve action failures.
- Updates both
iterAsyncimplementations and documentation. - Registers the guard in the project and changelog.
| File | Description |
|---|---|
.github/copilot-instructions.md |
Documents the new guard file. |
CHANGELOG.md |
Records the corrected behavior. |
AsyncObservable.fs |
Guards Async action execution and failures. |
FSharp.Control.R3.fsproj |
Compiles the new guard. |
IterationGuard.fs |
Implements failure capture and stopping. |
TaskObservable.fs |
Guards Task action execution and failures. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| try | ||
| do! action value | ||
| with | ||
| | :? OperationCanceledException when actionToken.IsCancellationRequested -> |
| try | ||
| do! action ct value | ||
| with | ||
| | :? OperationCanceledException when ct.IsCancellationRequested -> |
This branch has not been deployed
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.

Proposed Changes
Part of splitting #22 into small pull requests that can be reviewed one at a time. It is stacked on #37 (
fix/iter-async-overflow), so it shows only its own change; merge #37 first.Composing R3's
SelectAwaitwithForEachAsyncleft four gaps in both flavours ofiterAsync:Subscribereturns, so the action kept running for every element after it had thrown.SelectAwaitswallows anOperationCanceledException: a timeout such as HttpClient'sTaskCanceledExceptionstopped the sequential worker for good anditerAsyncnever completed.The action's exceptions no longer travel through R3 at all. An internal
IterationGuardrecords the first one, skips the remaining actions and cancels an internal token that completes the iteration throughTakeUntil, which also unsubscribes from the source at once; the iteration then fails with the recorded exception. A cancellation that R3 itself caused for an invocation (AwaitSwitch,CancelOnCompleted, disposal) is not a failure, and a cancelled token cancels the iteration without subscribing.Types of changes
What types of changes does your code introduce to FSharp.Control.R3?
Put an
xin the boxes that applyChecklist
Put an
xin the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.Further comments
The
iterAsyncdocs now describe which elements reach the action, when the iteration completes, also withCancelOnCompleted, and how it fails.Tests (in #22): the
iterAsynctests ofAsyncObservableTests.fsandTaskObservableTests.fs, one for each gap above.Stack – every pull request is based on the one before it, so each shows only its own change. Merge them in this order:
AwaitOperationConfigurationcases withAwait#23 refactor!: prefixAwaitOperationConfigurationcases withAwaitmapAsyncoptions eagerly #24 fix: validate the concurrency limit ofmapAsyncoptions eagerlyyield/zerosynchronously to keep element order #25 fix(rxquery): emityield/zerosynchronously to keep element ordersumBywithUnchecked.defaultof#26 fix(rxquery)!: stop seedingsumBywithUnchecked.defaultofrxqueryWithto cancel the terminal query operators #27 feat(rxquery): addrxqueryWithto cancel the terminal query operatorsofSeqreachable asObservable.ofSeq#29 feat(observable)!: makeofSeqreachable asObservable.ofSeqObservable.choosetake avoptionchooser, addObservableOption#30 feat(observable)!: makeObservable.choosetake avoptionchooser, addObservableOptionchunkByBoundariesfor boundaries of any element type #31 feat(observable): addchunkByBoundariesfor boundaries of any element typecancellationTokenfromtoLookup#35 fix(async)!: remove the ignoredcancellationTokenfromtoLookupiterAsyncthroughForEachAsyncinstead ofCountAsync#37 fix: wait foriterAsyncthroughForEachAsyncinstead ofCountAsynciterAsyncreliably and never lose a failure of its action #38 fix: stopiterAsyncreliably and never lose a failure of its action ← this pull requestofTaskconfigureAwaittotrue#39 fix(task)!: defaultofTaskconfigureAwaittotruetoArrayandtoListinto theTask.Observablemodule #40 feat(task)!: movetoArrayandtoListinto theTask.Observablemodulebind,catchandmapAsync#42 docs: document the R3 1.3.1 limitations ofbind,catchandmapAsync🤖 Generated with Claude Code