microsoft / node-api-dotnet

Advanced interoperability between .NET and JavaScript in the same process.
MIT License
484 stars 52 forks source link

Cannot create multiple NodeJSEnvironment for one NodeJSPlatform #348

Open camnewnham opened 1 month ago

camnewnham commented 1 month ago

The docs suggest that multiple environments can exist concurrently for the sample platform.

I get an assertion failure from this simple console application:

using Microsoft.JavaScript.NodeApi.Runtime;
NodejsPlatform platform = new NodejsPlatform("libnode.dll");
NodejsEnvironment env1 = platform.CreateEnvironment();
NodejsEnvironment env2 = platform.CreateEnvironment();
C:\Users\me\Desktop\libnode\src\inspector_agent.cc:704: Assertion `(start_io_thread_async_initialized.exchange(true)) == (false)' failed.
 1: 00007FF961841CAF v8_inspector::V8StackTrace::V8StackTrace+649598
...

libnode was built with the instructions and is using the suggested napi-libnode-v20.9.0 branch.

jasongin commented 1 month ago

I can reproduce this. While destroying one environment and then creating another will work, creating a second concurrent environment crashes.

@vmoroz It should be supported, right? Can you try adding a test case for this in the libnode PR?

vmoroz commented 3 days ago

@camnewnham , I am currently working on the new version of the libnode API and I have faced the same issue. The root cause is that the Inspector (JS debugger) can be associated only with a single root environment. Using multiple environment requires to explicitly disable the Inspector. There is a test/embedding/embedtest_concurrent_node_api.cc file to test the multiple runtimes in the PR.

The issue should be addressed in the new version of the API. The plan is adopt it in the node-api-dotnet project as soon as we complete the key outstanding TODOs. I.e. maybe even before the PR is merged. This is to be sure sure that the new API can be integrated well with .Net.

vmoroz commented 3 days ago

@camnewnham , I am also looking for real life scenarios for the libnode API to make sure that it addresses the real needs.
I wonder if you could share high level ideas about what you would like to get from that API.