Skip to content

Keep every inner exception when an async delegate's task faults with several [patch] - #110

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/109-whenall-inner-exceptions
Nov 14, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/109-whenall-inner-exceptions

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #109

What was wrong

Both ForwardCompletionAsync overloads (behind InvokeAsync(Func<Task>), InvokeAsync<T>(Func<Task<T>>) and the InvokeValueTaskAsync overloads) used await inner to wait for the delegate's task. await rethrows only the first exception, so InvokeAsync(() => Task.WhenAll(a, b)) faulted with 1 inner exception where calling the delegate directly gives 2.

Change

New helper TrySetFaultsAsync, called by both overloads before the existing await:

  • It waits for the inner task without throwing (Task.WhenAny(inner), which works on every target framework). When the task faulted with more than one exception, it passes all of them to completion.TrySetException(...).
  • Everything else takes the existing path unchanged: success, cancellation, and a fault with a single exception. A single OperationCanceledException therefore still cancels the caller's task instead of faulting it, and a null returned task behaves as before.
  • await invoker.InvokeAsync(...) and the synchronous Invoke(...) still throw the first exception.

Tests

New AggregateFaultTests, using Task.WhenAll over two pre-faulted tasks (InvalidOperationException, FormatException):

  • Void and TReturn forms, on the owner-thread (inline) path and on the queued (Task.Run + DoInvokes) path: the task faults with both inner exceptions, in order.
  • Both InvokeValueTaskAsync forms.
  • Awaiting the call, and the synchronous Invoke, still throw the first exception.

The five multi-exception tests failed before the fix ("Expected collection to contain a specific number of elements") and pass after it. The full suite passes (83/83), and dotnet build is clean on every target.


Generated by Claude Code

…several [patch]

ForwardCompletionAsync awaited the delegate's task, which rethrows only
the first of its exceptions, so InvokeAsync(() => Task.WhenAll(a, b))
faulted with one inner exception where the delegate's own task had two.
Wait without throwing first and, when the task faulted with more than
one exception, hand all of them to the caller's task. Awaiting the call
still throws the first, and a single exception keeps its existing
mapping (an OperationCanceledException still cancels).

Fixes #109

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5chbrSDfDAQ2U7T5G
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5jpiuVuBRXSgitSY1
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 9daefa4 into main Nov 14, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/109-whenall-inner-exceptions branch November 14, 2026 01:45
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.

InvokeAsync(() => Task.WhenAll(a, b)) faults with only the first exception: the returned task drops every inner exception but one

2 participants