mono / mono

Mono open source ECMA CLI, C# and .NET implementation.
https://www.mono-project.com
Other
11.13k stars 3.82k forks source link

[Wasm][Threading] condition 'jit_tls' not met #18356

Open jeromelaban opened 4 years ago

jeromelaban commented 4 years ago

Steps to Reproduce

Using theads-release, with 384db9e89053822a2d51cc19d7084ce75a636b37:

static void Main()
{
    Run();
}

private static async Task Run()
{
    var messages = new List<string>();
    var tcs = new TaskCompletionSource<bool>();

    new Thread(_ => {
        lock (gate)
        {
            messages.Add("In thread");
        }
            Thread.Sleep(500);
        tcs.TrySetResult(true);
    }).Start();

    await tcs.Task;

    lock (gate)
    {
        messages.Add("In Run");
    }

    Console.WriteLine($"tid:{Thread.CurrentThread.ManagedThreadId} messages: {string.Join(", ", messages)}");

    Console.WriteLine($"tid:{Thread.CurrentThread.ManagedThreadId} before timer");

        // Required to go back to the main thread ?
    var t = new Timer(_ => {
        Runtime.InvokeJS($"Interop.appendResult(\"{string.Join("\n", messages)}\")", out var result);
    });
    t.Change(1, Timeout.Infinite);
    Console.WriteLine($"tid:{Thread.CurrentThread.ManagedThreadId} after timer");
}

Where this JS file is available:

document.body.innerHTML = "<div id='results' />";

var Interop = {
    appendResult: function (str) {
        var txt = document.createTextNode(str);
        var parent = document.getElementById('results');
        parent.appendChild(txt, parent.lastChild);
    }
};

Current Behavior

* Assertion at /mnt/jenkins/workspace/test-mono-mainline-wasm/label/ubuntu-1804-amd64/mono/mini/interp/interp.c:432, condition `jit_tls' not met, function:set_context, ThreadContext needs initialized JIT TLS
put_char @ mono.js:1
mono.js:1 Pthread aborting at Error
    at abort (http://localhost:52417/mono.js:1:18077)
    at _abort (http://localhost:52417/mono.js:1:133765)
    at wasm-function[3645]:0xb157c
    at wasm-function[3904]:0xbfeec
    at wasm-function[3756]:0xba9df
    at wasm-function[2253]:0x69023
    at wasm-function[162]:0x3b77
    at wasm-function[2999]:0x8a445
    at wasm-function[1159]:0x32f90
    at wasm-function[5108]:0xee7ca

Most probably right before executing the timer's callback.

Expected Behavior

I'm not too sure of the expected behavior of this code. Using the timer is about getting back on the main Javascript thread (because Threading.Timer uses setTimeout), but it may not be the right way to do that.

lewing commented 4 years ago

/cc @vargaz

vargaz commented 4 years ago

I tried creating a testcase based on this code, but cannot reproduce it. Does it fail for something like:

var t = new Timer(_ => { Console.WriteLine ("HELLO!");
    });

as well ?

jeromelaban commented 4 years ago

With a more recent build of mono (91e026de9db) the failure is different, and the HELLO! is not displayed:

tid:2 messages: In thread, In Run
dotnet.js:1 tid:2 before timer
dotnet.js:1 tid:2 after timer
Error
    at 665835 (http://localhost:52823/dotnet.js:1:21051)
    at _emscripten_asm_const_iii (http://localhost:52823/dotnet.js:1:21272)
    at wasm_logger (<anonymous>:wasm-function[3267]:0x9fea5)
    at eglib_log_adapter (<anonymous>:wasm-function[6340]:0x118982)
    at monoeg_g_logstr (<anonymous>:wasm-function[4726]:0xcfe15)
    at monoeg_g_logv_nofree (<anonymous>:wasm-function[2300]:0x646c3)
    at monoeg_assertion_message (<anonymous>:wasm-function[464]:0xdf49)
    at mono_assertion_message (<anonymous>:wasm-function[224]:0x5120)
    at mono_assertion_message_disabled (<anonymous>:wasm-function[141]:0x31ce)
    at set_context (<anonymous>:wasm-function[2200]:0x60d27)
dotnet.js:1 * Assertion at /mnt/jenkins/workspace/test-mono-mainline-wasm/label/ubuntu-1804-amd64/mono/mini/interp/interp.c:425, condition `<disabled>' not met
put_char @ dotnet.js:1
dotnet.js:1 
put_char @ dotnet.js:1
dotnet.js:1 Pthread aborting at Error
    at abort (http://localhost:52823/dotnet.js:1:17847)
    at _abort (http://localhost:52823/dotnet.js:1:132948)
    at wasm_logger (<anonymous>:wasm-function[3267]:0x9fec7)
    at eglib_log_adapter (<anonymous>:wasm-function[6340]:0x118982)
    at monoeg_g_logstr (<anonymous>:wasm-function[4726]:0xcfe15)
    at monoeg_g_logv_nofree (<anonymous>:wasm-function[2300]:0x646c3)
    at monoeg_assertion_message (<anonymous>:wasm-function[464]:0xdf49)
    at mono_assertion_message (<anonymous>:wasm-function[224]:0x5120)
    at mono_assertion_message_disabled (<anonymous>:wasm-function[141]:0x31ce)
    at set_context (<anonymous>:wasm-function[2200]:0x60d27)
a8nguyen commented 1 year ago

+1