Skip to content

Save and restore Thread.CurrentThread._synchronizationContext for synchronous runtime async calls #117725

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

Merged
merged 30 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e7f430a
WIP
jakobbotsch Jul 15, 2025
3b85c95
JIT: Preparatory refactoring to support multiple defs per single node
jakobbotsch Jul 15, 2025
fba620a
Mitigate some TP diffs
jakobbotsch Jul 16, 2025
3939f24
Nit
jakobbotsch Jul 16, 2025
2e151fe
Merge branch 'multiple-defs-per-node' into restore-sync-context-field
jakobbotsch Jul 16, 2025
5fec55f
WIP
jakobbotsch Jul 16, 2025
b9483e7
Attempt at jump threading optimization
jakobbotsch Jul 16, 2025
8301152
Drop jump threading opt for now
jakobbotsch Jul 16, 2025
4a88fe8
Run jit-format
jakobbotsch Jul 16, 2025
c2b3e88
Clean up
jakobbotsch Jul 17, 2025
f9e62e1
Fix SPMI
jakobbotsch Jul 17, 2025
41158a3
Merge branch 'main' of github.com:dotnet/runtime into restore-sync-co…
jakobbotsch Jul 17, 2025
576d138
Fix build
jakobbotsch Jul 17, 2025
39549be
JIT: Fix unique successors in `BBswtDesc` in runtime async
jakobbotsch Jul 17, 2025
a38902b
Clean up
jakobbotsch Jul 17, 2025
f91aa6a
Expand test
jakobbotsch Jul 17, 2025
c786545
Nit
jakobbotsch Jul 17, 2025
aa31bfb
Fix release build
jakobbotsch Jul 17, 2025
9e3a686
Fix dangling pointer bug
jakobbotsch Jul 17, 2025
c78ca39
Update JIT-EE GUID
jakobbotsch Jul 17, 2025
24cd099
Redisable runtime async
jakobbotsch Jul 17, 2025
f40c255
Require AsyncSuspendedIndicator to be present when HasSuspensionIndic…
jakobbotsch Jul 21, 2025
2c073ab
Update async comments
jakobbotsch Jul 21, 2025
0741517
Assert NumPosts
jakobbotsch Jul 21, 2025
8066afb
Add another test that we capture the sync context from before the call
jakobbotsch Jul 21, 2025
ddd7584
Use sync context from before call as continuation context
jakobbotsch Jul 21, 2025
ce2f834
Reset continuation context handling for tailcalls:
jakobbotsch Jul 21, 2025
0405154
Add some more comments
jakobbotsch Jul 23, 2025
b1329a7
Run jit-format
jakobbotsch Jul 23, 2025
6510e37
Make SyncContext parameter the first one
jakobbotsch Jul 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -591,19 +591,42 @@ private static ValueTask FinalizeValueTaskReturningThunk(Continuation continuati
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RestoreExecutionContext(ExecutionContext? previousExecutionCtx)
private static void RestoreExecutionContext(ExecutionContext? previousExecCtx)
{
Thread thread = Thread.CurrentThreadAssumedInitialized;
ExecutionContext? currentExecutionCtx = thread._executionContext;
if (previousExecutionCtx != currentExecutionCtx)
ExecutionContext? currentExecCtx = thread._executionContext;
if (previousExecCtx != currentExecCtx)
{
ExecutionContext.RestoreChangedContextToThread(thread, previousExecutionCtx, currentExecutionCtx);
ExecutionContext.RestoreChangedContextToThread(thread, previousExecCtx, currentExecCtx);
}
}

private static void CaptureContinuationContext(ref object context, ref CorInfoContinuationFlags flags)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void CaptureContexts(out ExecutionContext? execCtx, out SynchronizationContext? syncCtx)
{
Thread thread = Thread.CurrentThreadAssumedInitialized;
execCtx = thread._executionContext;
syncCtx = thread._synchronizationContext;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RestoreContexts(bool suspended, ExecutionContext? previousExecCtx, SynchronizationContext? previousSyncCtx)
{
Thread thread = Thread.CurrentThreadAssumedInitialized;
if (!suspended && previousSyncCtx != thread._synchronizationContext)
{
thread._synchronizationContext = previousSyncCtx;
}

ExecutionContext? currentExecCtx = thread._executionContext;
if (previousExecCtx != currentExecCtx)
{
ExecutionContext.RestoreChangedContextToThread(thread, previousExecCtx, currentExecCtx);
}
}

private static void CaptureContinuationContext(ref object context, SynchronizationContext syncCtx, ref CorInfoContinuationFlags flags)
{
SynchronizationContext? syncCtx = Thread.CurrentThreadAssumedInitialized._synchronizationContext;
if (syncCtx != null && syncCtx.GetType() != typeof(SynchronizationContext))
{
flags |= CorInfoContinuationFlags.CORINFO_CONTINUATION_CONTINUE_ON_CAPTURED_SYNCHRONIZATION_CONTEXT;
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,8 @@ struct CORINFO_ASYNC_INFO
// Method handle for AsyncHelpers.RestoreExecutionContext
CORINFO_METHOD_HANDLE restoreExecutionContextMethHnd;
CORINFO_METHOD_HANDLE captureContinuationContextMethHnd;
CORINFO_METHOD_HANDLE captureContextsMethHnd;
CORINFO_METHOD_HANDLE restoreContextsMethHnd;
};

// Flags passed from JIT to runtime.
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/inc/jiteeversionguid.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@

#include <minipal/guid.h>

constexpr GUID JITEEVersionIdentifier = { /* d24a67e0-9e57-4c9e-ad31-5785df2526f2 */
0xd24a67e0,
0x9e57,
0x4c9e,
{0xad, 0x31, 0x57, 0x85, 0xdf, 0x25, 0x26, 0xf2}
constexpr GUID JITEEVersionIdentifier = { /* 2d40ec46-2e41-4a8b-8349-3c1267b95821 */
0x2d40ec46,
0x2e41,
0x4a8b,
{0x83, 0x49, 0x3c, 0x12, 0x67, 0xb9, 0x58, 0x21}
};

#endif // JIT_EE_VERSIONING_GUID_H
Loading
Loading