dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

[Wasm][Profiled AOT] Error: TaskT_TransitionToFinal_AlreadyCompleted #77122

Open jeromelaban opened 2 years ago

jeromelaban commented 2 years ago

Description

When running the code below, the app crashes when built with an AOT profile. When running with the interpreter, or without a profile, the app runs properly.

Reproduction Steps

When running the following code using profiled AOT:

public static class Program
{
    static async Task Main(string[] args)
    {
        await Fetch();
    }

    private static async Task Fetch()
    {
        try
        {
            var client = new System.Net.Http.HttpClient()
            {
                DefaultRequestHeaders = { { "origin", "WindowsCalculator" } }
            };

            var r = await client.GetAsync(new Uri("https://cors-anywhere.herokuapp.com/https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png"));
            Console.WriteLine($"Got Response");
            var c = await r.Content.ReadAsByteArrayAsync();
            Console.WriteLine($"Got {c.Length} bytes");
        }
        catch (Exception e)
        {
            Console.WriteLine($"Failure: " + e);
        }
    }
}

Profile: aot.zip Full repro: https://github.com/jeromelaban/Wasm.Samples/tree/master/AotIssueMismatch77122

Expected behavior

No crash, bytes are reported.

Actual behavior

Uncaught (in promise) Error: TaskT_TransitionToFinal_AlreadyCompleted
    at marshal_exception_to_js (dotnet.js:5:53508)
    at invoke_method_and_handle_exception (dotnet.js:5:76910)
    at runtimeHelpers.javaScriptExports.complete_task (dotnet.js:5:81214)
    at dotnet.js:5:39700
marshal_exception_to_js @   dotnet.js:5
invoke_method_and_handle_exception  @   dotnet.js:5
runtimeHelpers.javaScriptExports.complete_task  @   dotnet.js:5
(anonymous) @   dotnet.js:5
Promise.catch (async)       
_marshal_task_to_cs @   dotnet.js:5
_bound_js_INTERNAL_http_wasm_fetch  @   _bound_js_INTERNAL_http_wasm_fetch:14
mono_wasm_invoke_bound_function @   dotnet.js:5
_mono_wasm_invoke_bound_function    @   dotnet.js:6228
$aot_wrapper_System_dot_Runtime_dot_InteropServices_dot_JavaScript_System_dot_Runtime_dot_InteropServices_dot_JavaScript__Interop_sl_Runtime__InvokeJSFunction_pinvoke_void_iicl7_void_2a_void_iicl7_void_2a_   @   dotnet.wasm:0x27de96
$System_Net_Http_System_Net_Http_BrowserHttpInterop_Fetch_string_string___string___string___object___System_Runtime_InteropServices_JavaScript_JSObject_string  @   dotnet.wasm:0x269b23
$System_Net_Http_System_Net_Http_BrowserHttpHandler__CallFetchd__53_MoveNext    @   dotnet.wasm:0x26846c
$aot_instances_aot_wrapper_gsharedvt_out_sig_void_this_ @   dotnet.wasm:0x28e984
$jit_call_cb    @   dotnet.wasm:0x20aa8
$mono_llvm_cpp_catch_exception  @   dotnet.wasm:0x2081b1
$do_jit_call    @   dotnet.wasm:0x1f6a9
$interp_exec_method @   dotnet.wasm:0xfff5
$interp_entry   @   dotnet.wasm:0x1eaaa
$interp_entry_static_1  @   dotnet.wasm:0x21820
$aot_instances_aot_wrapper_gsharedvt_in_sig_void_bii    @   dotnet.wasm:0x296a62
$System_Net_Http_System_Net_Http_BrowserHttpHandler_CallFetch_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_System_Nullable_1_bool  @   dotnet.wasm:0x264a77
$System_Net_Http_System_Net_Http_BrowserHttpHandler___SendAsyncg__Impl_55_0d_MoveNext   @   dotnet.wasm:0x2658cd
$aot_instances_aot_wrapper_gsharedvt_out_sig_void_this_ @   dotnet.wasm:0x28e984
$jit_call_cb    @   dotnet.wasm:0x20aa8
$mono_llvm_cpp_catch_exception  @   dotnet.wasm:0x2081b1
$do_jit_call    @   dotnet.wasm:0x1f6a9
$interp_exec_method @   dotnet.wasm:0xfff5
$interp_entry   @   dotnet.wasm:0x1eaaa
$interp_entry_static_1  @   dotnet.wasm:0x21820
$aot_instances_aot_wrapper_gsharedvt_in_sig_void_bii    @   dotnet.wasm:0x296a62
$System_Net_Http_System_Net_Http_BrowserHttpHandler__SendAsyncg__Impl_55_0_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_System_Nullable_1_bool @   dotnet.wasm:0x2652f1

Regression?

Yes, .NET 6 does not have this issue.

Known Workarounds

No response

Configuration

Tested on both: main: 9567f277c65 release/7.0: cd2d8379838

Other information

No response

ghost commented 2 years ago

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

Issue Details
### Description When running the code below, the app crashes when built with an AOT profile. When running with the interpreter, or without a profile, the app runs properly. ### Reproduction Steps When running the following code using profiled AOT: ```csharp public static class Program { static async Task Main(string[] args) { await Fetch(); } private static async Task Fetch() { try { var client = new System.Net.Http.HttpClient() { DefaultRequestHeaders = { { "origin", "WindowsCalculator" } } }; var r = await client.GetAsync(new Uri("https://cors-anywhere.herokuapp.com/https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png")); Console.WriteLine($"Got Response"); var c = await r.Content.ReadAsByteArrayAsync(); Console.WriteLine($"Got {c.Length} bytes"); } catch (Exception e) { Console.WriteLine($"Failure: " + e); } } } ``` Profile: [aot.zip](https://github.com/dotnet/runtime/files/9803433/aot.zip) ### Expected behavior No crash, bytes are reported. ### Actual behavior ``` Uncaught (in promise) Error: TaskT_TransitionToFinal_AlreadyCompleted at marshal_exception_to_js (dotnet.js:5:53508) at invoke_method_and_handle_exception (dotnet.js:5:76910) at runtimeHelpers.javaScriptExports.complete_task (dotnet.js:5:81214) at dotnet.js:5:39700 marshal_exception_to_js @ dotnet.js:5 invoke_method_and_handle_exception @ dotnet.js:5 runtimeHelpers.javaScriptExports.complete_task @ dotnet.js:5 (anonymous) @ dotnet.js:5 Promise.catch (async) _marshal_task_to_cs @ dotnet.js:5 _bound_js_INTERNAL_http_wasm_fetch @ _bound_js_INTERNAL_http_wasm_fetch:14 mono_wasm_invoke_bound_function @ dotnet.js:5 _mono_wasm_invoke_bound_function @ dotnet.js:6228 $aot_wrapper_System_dot_Runtime_dot_InteropServices_dot_JavaScript_System_dot_Runtime_dot_InteropServices_dot_JavaScript__Interop_sl_Runtime__InvokeJSFunction_pinvoke_void_iicl7_void_2a_void_iicl7_void_2a_ @ dotnet.wasm:0x27de96 $System_Net_Http_System_Net_Http_BrowserHttpInterop_Fetch_string_string___string___string___object___System_Runtime_InteropServices_JavaScript_JSObject_string @ dotnet.wasm:0x269b23 $System_Net_Http_System_Net_Http_BrowserHttpHandler__CallFetchd__53_MoveNext @ dotnet.wasm:0x26846c $aot_instances_aot_wrapper_gsharedvt_out_sig_void_this_ @ dotnet.wasm:0x28e984 $jit_call_cb @ dotnet.wasm:0x20aa8 $mono_llvm_cpp_catch_exception @ dotnet.wasm:0x2081b1 $do_jit_call @ dotnet.wasm:0x1f6a9 $interp_exec_method @ dotnet.wasm:0xfff5 $interp_entry @ dotnet.wasm:0x1eaaa $interp_entry_static_1 @ dotnet.wasm:0x21820 $aot_instances_aot_wrapper_gsharedvt_in_sig_void_bii @ dotnet.wasm:0x296a62 $System_Net_Http_System_Net_Http_BrowserHttpHandler_CallFetch_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_System_Nullable_1_bool @ dotnet.wasm:0x264a77 $System_Net_Http_System_Net_Http_BrowserHttpHandler___SendAsyncg__Impl_55_0d_MoveNext @ dotnet.wasm:0x2658cd $aot_instances_aot_wrapper_gsharedvt_out_sig_void_this_ @ dotnet.wasm:0x28e984 $jit_call_cb @ dotnet.wasm:0x20aa8 $mono_llvm_cpp_catch_exception @ dotnet.wasm:0x2081b1 $do_jit_call @ dotnet.wasm:0x1f6a9 $interp_exec_method @ dotnet.wasm:0xfff5 $interp_entry @ dotnet.wasm:0x1eaaa $interp_entry_static_1 @ dotnet.wasm:0x21820 $aot_instances_aot_wrapper_gsharedvt_in_sig_void_bii @ dotnet.wasm:0x296a62 $System_Net_Http_System_Net_Http_BrowserHttpHandler__SendAsyncg__Impl_55_0_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_System_Nullable_1_bool @ dotnet.wasm:0x2652f1 ``` ### Regression? Yes, .NET 6 does not have this issue. ### Known Workarounds _No response_ ### Configuration Tested on both: main: 9567f277c65 release/7.0: cd2d8379838 ### Other information _No response_
Author: jeromelaban
Assignees: -
Labels: `area-System.Net.Http`
Milestone: -
ghost commented 2 years ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

Issue Details
### Description When running the code below, the app crashes when built with an AOT profile. When running with the interpreter, or without a profile, the app runs properly. ### Reproduction Steps When running the following code using profiled AOT: ```csharp public static class Program { static async Task Main(string[] args) { await Fetch(); } private static async Task Fetch() { try { var client = new System.Net.Http.HttpClient() { DefaultRequestHeaders = { { "origin", "WindowsCalculator" } } }; var r = await client.GetAsync(new Uri("https://cors-anywhere.herokuapp.com/https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png")); Console.WriteLine($"Got Response"); var c = await r.Content.ReadAsByteArrayAsync(); Console.WriteLine($"Got {c.Length} bytes"); } catch (Exception e) { Console.WriteLine($"Failure: " + e); } } } ``` Profile: [aot.zip](https://github.com/dotnet/runtime/files/9803433/aot.zip) Full repro: https://github.com/jeromelaban/Wasm.Samples/tree/master/AotIssueMismatch77122 ### Expected behavior No crash, bytes are reported. ### Actual behavior ``` Uncaught (in promise) Error: TaskT_TransitionToFinal_AlreadyCompleted at marshal_exception_to_js (dotnet.js:5:53508) at invoke_method_and_handle_exception (dotnet.js:5:76910) at runtimeHelpers.javaScriptExports.complete_task (dotnet.js:5:81214) at dotnet.js:5:39700 marshal_exception_to_js @ dotnet.js:5 invoke_method_and_handle_exception @ dotnet.js:5 runtimeHelpers.javaScriptExports.complete_task @ dotnet.js:5 (anonymous) @ dotnet.js:5 Promise.catch (async) _marshal_task_to_cs @ dotnet.js:5 _bound_js_INTERNAL_http_wasm_fetch @ _bound_js_INTERNAL_http_wasm_fetch:14 mono_wasm_invoke_bound_function @ dotnet.js:5 _mono_wasm_invoke_bound_function @ dotnet.js:6228 $aot_wrapper_System_dot_Runtime_dot_InteropServices_dot_JavaScript_System_dot_Runtime_dot_InteropServices_dot_JavaScript__Interop_sl_Runtime__InvokeJSFunction_pinvoke_void_iicl7_void_2a_void_iicl7_void_2a_ @ dotnet.wasm:0x27de96 $System_Net_Http_System_Net_Http_BrowserHttpInterop_Fetch_string_string___string___string___object___System_Runtime_InteropServices_JavaScript_JSObject_string @ dotnet.wasm:0x269b23 $System_Net_Http_System_Net_Http_BrowserHttpHandler__CallFetchd__53_MoveNext @ dotnet.wasm:0x26846c $aot_instances_aot_wrapper_gsharedvt_out_sig_void_this_ @ dotnet.wasm:0x28e984 $jit_call_cb @ dotnet.wasm:0x20aa8 $mono_llvm_cpp_catch_exception @ dotnet.wasm:0x2081b1 $do_jit_call @ dotnet.wasm:0x1f6a9 $interp_exec_method @ dotnet.wasm:0xfff5 $interp_entry @ dotnet.wasm:0x1eaaa $interp_entry_static_1 @ dotnet.wasm:0x21820 $aot_instances_aot_wrapper_gsharedvt_in_sig_void_bii @ dotnet.wasm:0x296a62 $System_Net_Http_System_Net_Http_BrowserHttpHandler_CallFetch_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_System_Nullable_1_bool @ dotnet.wasm:0x264a77 $System_Net_Http_System_Net_Http_BrowserHttpHandler___SendAsyncg__Impl_55_0d_MoveNext @ dotnet.wasm:0x2658cd $aot_instances_aot_wrapper_gsharedvt_out_sig_void_this_ @ dotnet.wasm:0x28e984 $jit_call_cb @ dotnet.wasm:0x20aa8 $mono_llvm_cpp_catch_exception @ dotnet.wasm:0x2081b1 $do_jit_call @ dotnet.wasm:0x1f6a9 $interp_exec_method @ dotnet.wasm:0xfff5 $interp_entry @ dotnet.wasm:0x1eaaa $interp_entry_static_1 @ dotnet.wasm:0x21820 $aot_instances_aot_wrapper_gsharedvt_in_sig_void_bii @ dotnet.wasm:0x296a62 $System_Net_Http_System_Net_Http_BrowserHttpHandler__SendAsyncg__Impl_55_0_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_System_Nullable_1_bool @ dotnet.wasm:0x2652f1 ``` ### Regression? Yes, .NET 6 does not have this issue. ### Known Workarounds _No response_ ### Configuration Tested on both: main: 9567f277c65 release/7.0: cd2d8379838 ### Other information _No response_
Author: jeromelaban
Assignees: -
Labels: `arch-wasm`, `area-System.Net.Http`, `untriaged`
Milestone: -
jeromelaban commented 2 years ago

@vargaz using profile filtering, I've narrowed the issue down to it being one of those AOTed:

System.Runtime.CompilerServices.*?AsyncTask.*

Still a large number of methods, but better than nothing.

jeromelaban commented 2 years ago

Also, using net8, the error comes up as follow:

Uncaught (in promise)    at System.Threading.Tasks.TaskCompletionSource`1[[System.Runtime.InteropServices.JavaScript.JSObject, System.Runtime.InteropServices.JavaScript, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].SetException(Exception exception)
   at System.Runtime.InteropServices.JavaScript.JavaScriptExports.CompleteTask(JSMarshalerArgument* arguments_buffer)
Error: TaskT_TransitionToFinal_AlreadyCompleted
    at marshal_exception_to_js (http://localhost:63077/package_b46cfbfd8bed26910f6c35b75520b2fe0b0313b2/dotnet.js:5:39184)
    at invoke_method_and_handle_exception (http://localhost:63077/package_b46cfbfd8bed26910f6c35b75520b2fe0b0313b2/dotnet.js:5:76021)
    at runtimeHelpers.javaScriptExports.complete_task (http://localhost:63077/package_b46cfbfd8bed26910f6c35b75520b2fe0b0313b2/dotnet.js:5:80449)
    at http://localhost:63077/package_b46cfbfd8bed26910f6c35b75520b2fe0b0313b2/dotnet.js:5:48587
vargaz commented 2 years ago

Would it be possible to create a testcase which uses the normal .net build ?

jeromelaban commented 2 years ago

I'm not able to reproduce with a normal .NET build, there must be an option that's doing something differently.

The exception above being raise is hiding this exception:

RuntimeError: null function or function signature mismatch
    at System_Net_Http_System_Runtime_CompilerServices_AsyncTaskMethodBuilder_Start_System_Net_Http_HttpContent__LoadIntoBufferAsyncCored__63_System_Net_Http_HttpContent__LoadIntoBufferAsyncCored__63_ (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[20015]:0x2a305a)
    at corlib_aot_wrapper_gsharedvt_out_sig_void_this_bii (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[18891]:0x27100f)
    at jit_call_cb (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[226]:0x2105d)
    at mono_llvm_cpp_catch_exception (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[16267]:0x208847)
    at do_jit_call (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[173]:0x1fc48)
    at interp_exec_method (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[99]:0x10593)
    at interp_entry (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[152]:0x1f049)
    at interp_entry_instance_ret_2 (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[251]:0x21acd)
    at System_Net_Http_aot_wrapper_gsharedvt_in_sig_obj_this_i8cl1d_Mono_dValueTuple_601_3cint_3e_ (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[20151]:0x2a954b)
    at System_Net_Http_System_Net_Http_HttpClient___SendAsyncg__Core_83_0d_MoveNext (http://localhost:63077/package_afe58f4d046c9c3dd66af47785a06dd201d4a38c/dotnet.wasm:wasm-function[19773]:0x28947c)
jeromelaban commented 2 years ago

Here's a repro with the vanilla .NET tooling. 77122-test-aot.zip

The issue only happens when HttpContent.LoadIntoBufferAsync is not AOTed (because of https://github.com/mono/mono/issues/19824, which is now fixed)

pavelsavara commented 4 months ago

cc @kotlarmilos