Skip to content

Conversation

@stephentoub
Copy link
Member

Task's IAsyncResult.AsyncWaitHandle implementation lazily creates a ManualResetEventSlim, which is then set as part of the Task completing. That AsyncWaitHandle is exposed publicly out of the surface area. If code (arguably erroneously) Dispose's of that WaitHandle before the Task has completed, the task's completion routine will incur an ObjectDisposedException, which will propagate out and potentially disrupt caller logic or even crash the process if allowed to propagate on a thread without user code to catch it.

This just adds a catch (ObjectDisposedException) block to eat such an exception in order to make the system more reliable in the face of such spooky action at a distance.

Fixes #64400

@ghost
Copy link

ghost commented Feb 1, 2022

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

Issue Details

Task's IAsyncResult.AsyncWaitHandle implementation lazily creates a ManualResetEventSlim, which is then set as part of the Task completing. That AsyncWaitHandle is exposed publicly out of the surface area. If code (arguably erroneously) Dispose's of that WaitHandle before the Task has completed, the task's completion routine will incur an ObjectDisposedException, which will propagate out and potentially disrupt caller logic or even crash the process if allowed to propagate on a thread without user code to catch it.

This just adds a catch (ObjectDisposedException) block to eat such an exception in order to make the system more reliable in the face of such spooky action at a distance.

Fixes #64400

Author: stephentoub
Assignees: -
Labels:

area-System.Threading.Tasks

Milestone: -

…mpletion

Task's IAsyncResult.AsyncWaitHandle implementation lazily creates a ManualResetEventSlim, which is then set as part of the Task completing.  That AsyncWaitHandle is exposed publicly out of the surface area.  If code (arguably erroneously) Dispose's of that WaitHandle before the Task has completed, the task's completion routine will incur an ObjectDisposedException, which will propagate out and potentially disrupt caller logic or even crash the process if allowed to propagate on a thread without user code to catch it.

This just adds a catch (ObjectDisposedException) block to eat such an exception in order to make the system more reliable in the face of such spooky action at a distance.
@stephentoub stephentoub force-pushed the asyncwaithandledispose branch from 2f6fbb6 to 53f56f2 Compare February 4, 2022 04:56
@stephentoub stephentoub merged commit 6b14c1e into dotnet:main Feb 4, 2022
@stephentoub stephentoub deleted the asyncwaithandledispose branch February 4, 2022 18:48
@ghost ghost locked as resolved and limited conversation to collaborators Mar 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Closing the AsyncWaitHandle from the result of socket.BeginConnect crashes the application in .NET 6.0

3 participants