Skip to content

AsyncVoidMethodBuilder.NotifySynchronizationContextOfCompletion() crashes with NullReferenceException #99452

Closed

Description

@FlsZen

Description

We noticed in our release builds that we were getting sporadic crashes with this stack:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.NotifySynchronizationContextOfCompletion()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

I've reduced it down to the code listed in the reproduction steps. It is critical to have the SynchronizationContext be non-null when entering the async void method to reproduce this issue.

I can provide a Time Travel Debug trace for the crash if desired, although it seems easy enough to reproduce that folks can capture their own.

Reproduction Steps

The top-level program below causes the crash when built for release (not debug). On my computer it's usually within seconds. Experiment with the SpinWait count if this doesn't work for you. The value of 25 used below tends to crash on my computer within seconds while a value of 1000 generally crashes between 20 seconds and several minutes. You may need a value in this range reproduce the crash quickly.

SynchronizationContext.SetSynchronizationContext(new());
while (true)
{
    Run();
    Thread.SpinWait(25);
}

async void Run()
{
    await Task.Yield();
    await Task.Yield();
}

Expected behavior

A compiled release build runs without crashing

Actual behavior

The compiled release build readily crashes with the stack:

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.NotifySynchronizationContextOfCompletion()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()

Regression?

I tested on .NET 6.0.27 and the crash was present there as well, so it seems like this has been an issue for quite a while.

Known Workarounds

In our case we:

  1. Set up a first chance exception handler to capture the initial NullReferenceException (not the rethrown one that causes the crash)
  2. Identify our particular method that was compiling with AsyncVoidMethodBuilder
  3. Changed the method to avoid that particular method builder
  4. (optional) Wring hands over every other async void method that the compiler makes for our code

Since SynchronizationContext must be non-null to encounter this bug, another workaround would be to try to keep it null.

Configuration

  • .NET 8.0.2, .NET 6.0.27
  • Windows 11 23H2 (x64) and macOS 14.2.1 (ARM64)
  • Appears to be specific to release builds

Other information

This seems related to the previously reported crashes in #40463 and #93969.

When I look at the crash in TTD, the thread originating the NullReferenceException is accessing memory that was very recently zeroed by another thread. I'm not sure how to get good symbols in WinDbg for this particular compiler generated code so I haven't been able to determine where in the C# code the memory is being zeroed.

The quickest fix that I see is to have the three callers of NotifySynchronizationContextOfCompletion pass in their already-checked field value. But, this only gets around the exception. I suspect the memory could still be zeroed too soon and thus NotifySynchronizationContextOfCompletion wouldn't be called when there had, in fact, been a SynchronizationContext assigned at one point.

Activity

  1. dotnet-policy-service commented on Mar 8, 2024

    @dotnet-policy-service
    Contributor

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

  2. added
    untriaged
    on Mar 8, 2024
  3. sharwell commented on Mar 8, 2024

    @sharwell
    Contributor

    This is the same stack trace I observed in dotnet/roslyn#72437. In the Roslyn case, I believe we used a debug build of the application, but a release build of the runtime.

  4. self-assigned this
    on Mar 8, 2024
  5. added and removed
    untriaged
    on Mar 8, 2024
  6. stephentoub commented on Mar 8, 2024

    @stephentoub
    Member

    Thank you for the very helpful repro. We've seen a few cases of this over the years but were never able to pinpoint the cause; I was able to find it with your repro.

    The problem is here:


    In the builder's MoveNext, after calling MoveNext on the state machine, it then checks to see whether the task has completed, and clears out the state machine if it has; this is done to avoid keeping strong references to any state that might be referenced by the state machine. But in clearing that out, it's also clearing out the builder, which in this case also contains that SynchronizationContext field. The intent of the check was to only be clearing as part of the last invocation, but your repro highlights that if MoveNext ends up queueing the continuation, it's possible that with race conditions the MoveNext for a previous turn of the crank may end up seeing IsCompleted as true and do the clearing while the completion code is running.

  7. locked and limited conversation to collaborators on Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions