Fix UnobservedTaskException in TaskToAsyncResult when APM callback skips EndXxx#126165
Open
mad1081 wants to merge 1 commit intodotnet:mainfrom
Open
Fix UnobservedTaskException in TaskToAsyncResult when APM callback skips EndXxx#126165mad1081 wants to merge 1 commit intodotnet:mainfrom
mad1081 wants to merge 1 commit intodotnet:mainfrom
Conversation
…ips EndXxx In the APM pattern, exceptions from the underlying operation are meant to be surfaced exclusively through the End method. However, if the callback passed to Begin does not call End (a valid shutdown pattern), the wrapped Task's exception remains unobserved. When the TaskAsyncResult and its inner Task are garbage-collected, TaskScheduler.UnobservedTaskException fires — a regression from .NET Framework 4.8, which had no Task wrapper. Fix: access task.Exception (marking it observed) before invoking the callback in both the synchronous-completion path and the async continuation path. End still re-throws the exception correctly if it is called. This matches the pattern used in PR dotnet#114226 for ResettableValueTaskSource. Fixes dotnet#126148
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-threading-tasks |
Author
|
@dotnet-policy-service agree |
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.
Summary
On .NET 6+,
NetworkStream.BeginRead(and any APM operation backed byTaskToAsyncResult) canfire
TaskScheduler.UnobservedTaskExceptionwhen:SocketException(995)), andBeginReadcallback does not callEndRead— a valid shutdown pattern where the callbackchecks a "stopping" flag and returns early.
This is a regression from .NET Framework 4.8, where
BeginReadused the native APM/OverlappedAsyncResultpath with no Task wrapper involved.
Root cause:
Socket.BeginReceivewrapsReceiveAsync(...).AsTask()in aTaskAsyncResultviaTaskToAsyncResult.Begin. When the socket is disposed, the inner Task faults. If the callback nevercalls
End,task.Exceptionis never accessed, so the exception is unobserved. On GC, the faultedTask finalizer raises
UnobservedTaskException.Fix: In
TaskAsyncResult, accesstask.Exception(which callsMarkExceptionObserved()internally)before invoking the callback, in both the synchronous-completion and async-continuation paths.
Endstillre-throws the exception correctly if called — observing does not suppress.
This is the same pattern used in #114226 for
ResettableValueTaskSourcein QUIC.Changes
TaskToAsyncResult.cs: Observetask.Exceptionbefore invoking the callback in both paths.TaskToAsyncResultTests.cs: Two regression tests covering async and sync completion paths.Fixes #126148
Reference: #114226