-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Save/restore contexts for custom notifiers #121743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
With dotnet#121448 we are saving/restoring the contexts as we unwind during suspension. However, since we delay the clal to OnCompleted, we need to restore the leaf contexts explicitly now.
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs
Show resolved
Hide resolved
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs
Outdated
Show resolved
Hide resolved
|
The Push/Pop of the contexts in Task-returning thunks is not completely useless, then. We can move the try/finally to be just around Wrapping only the FinalizeTaskReturningThunk in try/finally would also make sure that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements context capture and restoration for custom notifiers in the runtime async system. Since PR #121448 introduced context restoration during unwinding, and the call to OnCompleted is now delayed, leaf contexts must be explicitly restored. The changes also enable runtime async support by default.
Key changes:
- Added context capture before suspension in
AwaitAwaiter,UnsafeAwaitAwaiter, andTransparentAwait(for ValueTaskSource only) - Added context restoration before calling
OnCompleted/UnsafeOnCompletedon custom notifiers - Enabled runtime async by default and removed test disabling configuration
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/tests/async/Directory.Build.targets | Removed file that was disabling async tests, enabling them to run now that runtime async is ready |
| src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.cs | Added context capture calls in AwaitAwaiter and UnsafeAwaitAwaiter before suspension |
| src/coreclr/inc/clrconfigvalues.h | Changed RuntimeAsync config default from 0 to 1, enabling runtime async by default |
| src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs | Added ExecutionContext/SynchronizationContext fields to state struct, implemented CaptureContexts() method, added context capture in TransparentAwait for ValueTaskSource, and added context restoration before notifier callbacks in HandleSuspended |
VSadov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!!
|
Now we have some failures in #119432 related to too many context restores. Specifically in The former I think is just a bug. With the current approach The latter makes assumptions about the upper bound on how many times the context is restored. It assumes at most 1 restore per suspension, while with this change I think the leaf context may be flowed twice. I think the best fix to both is that we change the suspension restores to skip notifying callbacks for We will avoid notifying for an |
|
I applied the changes above and also pushed it to #119432 for testing. |
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs
Show resolved
Hide resolved
I think that is valid optimization already because if exec context is not accessed it cannot change. Notifications are always sent like: if (previousExecCtx != currentExecCtx)
{
ExecutionContext.RestoreChangedContextToThread(thread, previousExecCtx, currentExecCtx);
}Regardless of that, context manipulations for the purpose of the infrastructure should not be visible to the user code. |
|
Also, I think I have seen failures related to exec contexts in sockets even before the restore moved from call site to callee. Not sure if that were exactly same tests, but we had some issues with exec contexts even before. |
Hmm yes, I think that's true. |
This reverts commit 96e5432.
With #121448 we are restoring contexts as we unwind during suspension. However, since we delay the call to
OnCompleted, we need to restore the leaf contexts explicitly now.