[Mono]: Compiling AsyncStateMachine won't emit safepoints on specific loop pattern.

Open
#116,275 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
40/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
compilers

Research direction

Begin with the full reproduction linked from dotnet/android#9365 and inspect Mono's async state-machine compilation and safepoint emission for the label_10/label_20 loop. Done means the generated code injects safepoints on this looping path and the reproduction no longer causes a hanging GC stop-the-world or ANR.

Written by the indexing model from the issue text.

Description

area-Codegen-JIT-mono runtime-mono
Description

The following issue, https://github.com/dotnet/android/issues/9365, investigated an ANR that appears to happen since an async state machine was compiled into code that never reached a safepoint. On hybrid/coop suspend modes, not reaching a safepoint as part of executing managed code will lead to hanging GC STW and ANR reports.

Example code hitting the issue:

void IAsyncStateMachine.MoveNext()
{
  int num1 = this.<>1__state;
  try
  {
    switch (num1)
    {
      case 0:
        //
      default:
        //
        goto label_20;
    }
label_10:
    awaiter2.GetResult();
    if (!this.<>8__1.token.IsCancellationRequested)
    {
      if (this.<>4__this._game.GameState == GameState.Running)
      {
        //
      }
      else
        goto label_20;
    }
    else
      goto label_22;
label_20:
    if (!this.<>8__1.token.IsCancellationRequested)
    {
      awaiter2 = new AsyncTaskAwaiter(this.<>4__this._idleMonitor.WaitForIdleAsync());
      if (!awaiter2.IsCompleted)
      {
        //
        return;
      }
      goto label_10;
    }
  }
  catch (Exception ex)
  {
    this.<>1__state = -2;
    this.<>t__builder.SetException(ex);
    return;
  }
label_22:
  //
}

The workaround was to introduce a call to a managed none leaf function or a p/invoke that would hit a safepoint in the path ending up looping between label_10 and label_20.

Reproduction Steps

Full repro provided here, https://github.com/dotnet/android/issues/9365#issuecomment-2893984568.

Expected behavior

Generated code to inject safepoints on loops.

Actual behavior

No safepoints emitted leading to hanging GC STW and ANR.

Regression?

No response

Known Workarounds

Inject call to none leaf managed method not getting inlined or p/invoke.

Configuration

No response

Other information

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.