Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT-LLVM] DotnetJS exception on marshalled c#-objects finalizer call #2625

Closed
maxkatz6 opened this issue Jul 2, 2024 · 2 comments
Closed
Labels
area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly)

Comments

@maxkatz6
Copy link

maxkatz6 commented Jul 2, 2024

If any C# object is passed to JSImport, JavaScript side of the code will register it in FinalizationRegistry. This way C# side can get notification, when object is no longer used by JS side.

This logic doesn't seem to work with DotnetJS in LLVM. After a while app will output error in the console:

dotnet.runtime.js:3 
 Uncaught 
Error: Missing wasm export '_System_Runtime_InteropServices_JavaScript_JavaScriptExports_ReleaseJSOwnedObjectByGCHandle' (for System.Runtime.InteropServices.JavaScript.JavaScriptExports.ReleaseJSOwnedObjectByGCHandle)
    at dotnet.runtime.js:3:23922
    at Zt (dotnet.runtime.js:3:23460)
    at dotnet.runtime.js:3:34991
    at vr (dotnet.runtime.js:3:35059)
    at Tr (dotnet.runtime.js:3:35231)
    at FinalizationRegistry.cleanupSome (<anonymous>)

Minimal repro:

public static async Task Main()
{
    while (true)
    {
        // Callback is expected to be hold in memory, until JS FinalizationRegistry notifies it's no longer used by JS code.
        SetTimeout(() =>
        {
            Console.WriteLine("From callback: " + DateTime.Now);
        }, 1000);
        await Task.Delay(1000);
    }
}
[JSImport("globalThis.setTimeout")]
public static partial int SetTimeout([JSMarshalAs<JSType.Function>] Action callback, int intervalMs);

Run it and wait, resizing window also helps to trigger JS FinalizationRegistry.cleanupSome.
Or run minimal repro:
naot-llvm-demo-finalizer.zip

@jkotas jkotas added the area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly) label Jul 2, 2024
@pavelsavara
Copy link
Member

cc @maraf

@maraf
Copy link
Member

maraf commented Jul 8, 2024

Thank you for the report. I have encountered it as well when running some tests and have already a patch. I'll send PR soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly)
Projects
None yet
Development

No branches or pull requests

4 participants