Skip to content

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Sep 19, 2025

Backport of #119818 to release/10.0

/cc @jakobbotsch

Customer Impact

  • Customer reported
  • Found internally

Runtime async does not properly propagate synchronization contexts around in some cases; in particular the synchronization context is sometimes restored too early, leading to custom awaiters not observing a synchronization context that was explicitly set by an async method.

Regression

  • Yes
  • No

This is new code with runtime async.

Testing

The fix was verified on existing libraries tests that caught the issue.

Risk

Low, change to new code that is only enabled with runtime async.

jakobbotsch and others added 3 commits September 19, 2025 13:39
The previous IL generated approximately implements the following:
```csharp
ExecutionAndSyncBlockStore store = default;
store.Push();
Continuation cont = null;
T result = default;
try
{
  try
  {
    result = Inner(args);
    cont = StubHelpers.AsyncCallContinuation();
  }
  catch (Exception ex)
  {
    return Task.FromException(ex);
  }
}
finally
{
  store.Pop();
}

if (cont == null)
  return Task.FromResult(result);

return FinalizeTaskReturningThunk(cont);
```

However, since `FinalizeTaskReturningThunk` is where we call
`OnCompleted`, this means that user code gets called after we have
restored contexts.

To fix the issue change the thunks to look approximately like
```csharp
ExecutionAndSyncBlockStore store = default;
store.Push();
try
{
  Continuation cont;
  try
  {
    T result = Inner(args);
    cont = StubHelpers.AsyncCallContinuation();
    if (cont == null)
      return Task.FromResult(result);
  }
  catch (Exception ex)
  {
    return Task.FromException(ex);
  }

  return FinalizeTaskReturningThunk(cont);
}
finally
{
  store.Pop();
}
```
which ensures that the contexts do not get restored until after
`OnCompleted` has been called.
@jakobbotsch jakobbotsch requested a review from VSadov September 19, 2025 13:42
@JulieLeeMSFT JulieLeeMSFT added Servicing-approved Approved for servicing release area-VM-coreclr labels Sep 19, 2025
@JulieLeeMSFT JulieLeeMSFT added this to the 10.0.0 milestone Sep 19, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@JulieLeeMSFT
Copy link
Member

We can merge when tests pass.
CC @jeffschwMSFT .

@jakobbotsch
Copy link
Member

/ba-g Failures are known

@jkotas jkotas merged commit 1e72b83 into release/10.0 Sep 20, 2025
100 of 103 checks passed
@jkotas jkotas deleted the backport/pr-119818-to-release/10.0 branch September 22, 2025 23:27
@github-actions github-actions bot locked and limited conversation to collaborators Oct 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-VM-coreclr Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants