Skip to content

[Wasm][Threading] Task.Delay only continues after a GC #70571

Closed

Description

Description

Given a Wasm Threads enabled runtime, using Task.Delay waits for the next GC to continue.

Reproduction Steps

Given a Wasm Threads enabled runtime, and the following code:

static void Main()
{
    Console.WriteLine($"Runtime Version: " + RuntimeInformation.FrameworkDescription);
    Console.WriteLine($"Runtime Mode: " + runtimeMode);
    Console.WriteLine($"TID: {Thread.CurrentThread.ManagedThreadId}");

    Bug01();
}

private static void Bug01()
{
    Timer t = new Timer(_ =>
    {
        Log($"Before GC");
        GC.Collect();
        Log($"After GC");
    });
    t.Change(5000, 0);

    Log($"-> Bug01");
    Task.Run(async () =>
    {
        var sw = Stopwatch.StartNew();
        Log($"Before 2000ms delay");
        await Task.Delay(2000);
        Log($"After delay {sw.ElapsedMilliseconds}");
    });
    Log($"<- Bug01");

    void Log(string message) => Console.WriteLine($"[TID:{Thread.CurrentThread.ManagedThreadId}] {message}");
}

Shows the following

Expected behavior

The Async method continues after about 2 seconds, not when the GC has been invoked.

Actual behavior

Runtime Version: .NET 7.0.0-dev
dotnet.js:12 Runtime Mode: InterpreterAndAOT
dotnet.js:12 TID: 1
dotnet.js:12 [TID:1] -> Bug01
dotnet.js:12 [TID:1] <- Bug01
dotnet.js:12 [TID:2] Before 2000ms delay
dotnet.js:12 
       Blocking on the main thread is very dangerous, see https://emscripten.org/docs/porting/pthreads.html#blocking-on-the-main-browser-thread
dotnet.js:12 [TID:2] Before GC
dotnet.js:12 [TID:1] After delay 5690
dotnet.js:12 [TID:2] After GC

Regression?

no

Known Workarounds

none

Configuration

be4d292

Other information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions