Sitelet https://github.com/fsprojects/FSharp.Control.R3/pull/38
Skip to content

fix: stop iterAsync reliably and never lose a failure of its action - #38

Open
xperiandri wants to merge 1 commit into
fix/iter-async-overflowfrom
fix/iter-async-guard
Open

xperiandri wants to merge 1 commit into
fix/iter-async-overflowfrom
fix/iter-async-guard

Conversation

@xperiandri

@xperiandri xperiandri commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

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 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.

Types of changes

What types of changes does your code introduce to FSharp.Control.R3?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in 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.

  • Build and tests pass locally – 0 warnings, Fantomas is clean, and the tests that exist at this commit pass
  • I have added tests that prove my fix is effective or that my feature works (if appropriate) – in test: cover the library with integration tests #22, the top of this stack
  • I have added necessary documentation (if appropriate)

Further comments

The iterAsync docs now describe which elements reach the action, when the iteration completes, also with CancelOnCompleted, and how it fails.

Tests (in #22): the iterAsync tests of AsyncObservableTests.fs and TaskObservableTests.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:

  1. refactor!: prefix AwaitOperationConfiguration cases with Await #23 refactor!: prefix AwaitOperationConfiguration cases with Await
  2. fix: validate the concurrency limit of mapAsync options eagerly #24 fix: validate the concurrency limit of mapAsync options eagerly
  3. fix(rxquery): emit yield/zero synchronously to keep element order #25 fix(rxquery): emit yield/zero synchronously to keep element order
  4. fix(rxquery)!: stop seeding sumBy with Unchecked.defaultof #26 fix(rxquery)!: stop seeding sumBy with Unchecked.defaultof
  5. feat(rxquery): add rxqueryWith to cancel the terminal query operators #27 feat(rxquery): add rxqueryWith to cancel the terminal query operators
  6. docs(rxquery): document the query builder and every query operator #28 docs(rxquery): document the query builder and every query operator
  7. feat(observable)!: make ofSeq reachable as Observable.ofSeq #29 feat(observable)!: make ofSeq reachable as Observable.ofSeq
  8. feat(observable)!: make Observable.choose take a voption chooser, add ObservableOption #30 feat(observable)!: make Observable.choose take a voption chooser, add ObservableOption
  9. feat(observable): add chunkByBoundaries for boundaries of any element type #31 feat(observable): add chunkByBoundaries for boundaries of any element type
  10. fix(observable): validate every chunk length eagerly #32 fix(observable): validate every chunk length eagerly
  11. docs: document the processing and chunk configuration types #33 docs: document the processing and chunk configuration types
  12. docs(observable): correct misleading XML docs and document every function #34 docs(observable): correct misleading XML docs and document every function
  13. fix(async)!: remove the ignored cancellationToken from toLookup #35 fix(async)!: remove the ignored cancellationToken from toLookup
  14. fix(async)!: surface the original exception and cancellation from Async wrappers #36 fix(async)!: surface the original exception and cancellation from Async wrappers
  15. fix: wait for iterAsync through ForEachAsync instead of CountAsync #37 fix: wait for iterAsync through ForEachAsync instead of CountAsync
  16. fix: stop iterAsync reliably and never lose a failure of its action #38 fix: stop iterAsync reliably and never lose a failure of its action ← this pull request
  17. fix(task)!: default ofTask configureAwait to true #39 fix(task)!: default ofTask configureAwait to true
  18. feat(task)!: move toArray and toList into the Task.Observable module #40 feat(task)!: move toArray and toList into the Task.Observable module
  19. docs: correct the Async and Task module docs and document every member #41 docs: correct the Async and Task module docs and document every member
  20. docs: document the R3 1.3.1 limitations of bind, catch and mapAsync #42 docs: document the R3 1.3.1 limitations of bind, catch and mapAsync
  21. test: categorize the tests per tested component #43 test: categorize the tests per tested component
  22. test: cover the library with integration tests #22 test: cover the library with integration tests

🤖 Generated with Claude Code

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>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 00:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Concurrent cancellation can still hide an unrelated OperationCanceledException in both implementations.

Review effort: Balanced
Findings: 2 Medium severity

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 iterAsync implementations 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

No deployments
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.

2 participants