[Mono]: Compiling AsyncStateMachine won't emit safepoints on specific loop pattern.
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 40/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·