Skip to content

Read the DisposeAsync result in async builders when it completes successfully - #386

Merged
TheAngryByrd merged 4 commits into
masterfrom
fix/async-dispose-getresult
Nov 17, 2026
Merged

TheAngryByrd merged 4 commits into
masterfrom
fix/async-dispose-getresult

Conversation

@TheAngryByrd

@TheAngryByrd TheAngryByrd commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Proposed Changes

use on an IAsyncDisposable in asyncResult, asyncOption, asyncResultOption, parallelAsyncResult, and parallelAsyncValidation calls DisposeAsync() and gets a ValueTask. When that ValueTask had already completed successfully, TryFinallyAsync returned without a call to GetResult:

if vTask.IsCompletedSuccessfully then
    return ()
else
    return! vTask.AsTask() |> Async.AwaitTask

The ValueTask contract requires each instance to be consumed once. An IValueTaskSource can do work in GetResult. For example, a PoolingAsyncValueTaskMethodBuilder box returns to its pool in GetResult. With the old code, that source never saw its result read. Faulted and canceled disposals were not affected, because they take the AsTask branch.

This PR changes the success branch to return vTask.GetAwaiter().GetResult() in the five builders. #385 fixed the same problem in the task* builders.

Tests

  • TestHelpers.asyncBuilderDisposalSucceedsTests runs the existing assertAsyncDisposalOutcome (with ObservedDisposalSource, which counts GetResult calls) for a completed and a pending successful disposal. Each of the five builders has both cases.
  • The 5 completed successful disposal cases fail on master with The DisposeAsync result must be read exactly once. The 5 pending successful disposal cases pass on master. They guard the AsTask branch.
  • tests/FsToolkit.ErrorHandling.Tests/AsyncOptionCE.fs: the IAsyncDisposable tests were under #if NET7_0. The test project targets net10.0;net11.0, so they did not compile. They now use #if !FABLE_COMPILER, the same as the sibling files.

Not changed

  • When both the body and DisposeAsync fault, Async.TryFinallyAsync raises an AggregateException. This PR does not change that.
  • #if !FABLE_COMPILER && NETSTANDARD2_1 in five test files also never compiles for the test targets. That is a separate cleanup.
  • The src changes and the tests stay inside #if !FABLE_COMPILER. Fable 5.27.0 (the latest release) rejects ValueTask.IsCompletedSuccessfully, GetAwaiter, GetResult, and AsTask for JavaScript and Python with is not supported by Fable.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)

The five members are inline. An assembly that inlined them keeps the old body until it is recompiled.

Checklist

  • Build and tests pass locally (net10.0: 2129 passed, 8 skipped. net11.0: 2127 passed, 10 skipped. 0 failed.)
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added necessary documentation (if appropriate): RELEASE_NOTES.md

🤖 Generated with Claude Code

TheAngryByrd and others added 3 commits November 17, 2026 14:43
The asyncResult, asyncOption, asyncResultOption, parallelAsyncResult and
parallelAsyncValidation builders returned without GetResult when the
DisposeAsync ValueTask had already completed successfully. An
IValueTaskSource then never saw its result read.

Also compile the asyncOption IAsyncDisposable tests on every non-Fable
target. They were under #if NET7_0, which the test project no longer targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

🟢 Approval recommended

No active review issues block approval of the async-disposal fix.

No open issues 🎉

What changed in this PR

This PR fixes successful async disposal in five FsToolkit.ErrorHandling computation-expression builders, extending the task-builder fix from #385 to the async builders.

Changes:

  • Calls GetResult() when a disposal ValueTask has already completed successfully, ensuring its result is consumed.
  • Adds completed and pending disposal regression cases for all five builders and enables existing asyncOption disposal tests on supported .NET targets.
  • Documents the fix and the need to recompile assemblies that inline these builders.
File Description
tests/​FsToolkit.ErrorHandling.Tests/​TestHelpers.fs Adds shared completed and pending disposal checks.
tests/​FsToolkit.ErrorHandling.Tests/​ParallelAsyncValidationCE.fs Adds disposal result-consumption tests.
tests/​FsToolkit.ErrorHandling.Tests/​ParallelAsyncResultCE.fs Adds disposal result-consumption tests.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncResultOptionCE.fs Adds disposal result-consumption tests.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncResultCE.fs Adds disposal result-consumption tests.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncOptionCE.fs Adds regression tests and corrects the disposal-test compilation guard.
src/​FsToolkit.ErrorHandling/​ParallelAsyncValidationCE.fs Consumes already-successful disposal results.
src/​FsToolkit.ErrorHandling/​ParallelAsyncResultCE.fs Consumes already-successful disposal results.
src/​FsToolkit.ErrorHandling/​AsyncResultOptionCE.fs Consumes already-successful disposal results.
src/​FsToolkit.ErrorHandling/​AsyncResultCE.fs Consumes already-successful disposal results.
src/​FsToolkit.ErrorHandling/​AsyncOptionCE.fs Consumes already-successful disposal results.
RELEASE_NOTES.md Records the fix and recompilation guidance.

Review effort: Balanced

@TheAngryByrd
TheAngryByrd requested a balanced review from Copilot November 17, 2026 22:32
@TheAngryByrd
TheAngryByrd merged commit 8795d10 into master Nov 17, 2026
24 checks passed
@TheAngryByrd
TheAngryByrd deleted the fix/async-dispose-getresult branch November 17, 2026 22:34

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.

🟢 Approval recommended

No active review issues are recorded for the fix to completed asynchronous-disposal result consumption across the five builders.

No open issues 🎉

Review effort: Balanced

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