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

JS interop should support threading #76956

Closed
Tracked by #68162
lambdageek opened this issue Oct 12, 2022 · 7 comments
Closed
Tracked by #68162

JS interop should support threading #76956

lambdageek opened this issue Oct 12, 2022 · 7 comments
Assignees
Labels
arch-wasm WebAssembly architecture area-VM-threading-mono os-browser Browser variant of arch-wasm
Milestone

Comments

@lambdageek
Copy link
Member

lambdageek commented Oct 12, 2022

JavaScript interop: JS object references in managed need to be tied to a thread - there's no shared JS memory with WebWorkers

Tracking issue: #85592

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 12, 2022
@ghost
Copy link

ghost commented Oct 12, 2022

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

Issue Details

null

Author: lambdageek
Assignees: -
Labels:

area-System.Threading

Milestone: -

@lambdageek lambdageek added area-System.Runtime.InteropServices.JavaScript and removed area-System.Threading untriaged New issue has not been triaged by the area owner labels Oct 12, 2022
@lambdageek lambdageek added this to the 8.0.0 milestone Oct 12, 2022
@lambdageek
Copy link
Member Author

I think this is going to require some careful design work. Just thinking through how I would call 2 JS imports that share some JS state it seems like we will need some careful coordination - either transparent to the user (how?)

Ie suppose I exposed a JS dictionary:

[JSImport]
void AddProperty(string key, int val);

[JSImport]
int LookupProperty(string key);

how would I guarantee that AddProperty and LookupProperty are called from the same thread?

What if I'm in an async method?

What if I'm in a threadpool thread?

@lambdageek
Copy link
Member Author

It seems like we want to focus on a couple of key scenarios to help us hone in on a good design.

For example:

  1. "make fetch and C# HttpClient.GetAsync usable from any thread
  2. "make it possible to load a JS library into a Web Worker and usable from a single (non-threadpool) Thread"
  3. "make it possible to link the result of a JS call to a particular thread and make it possible to return to that thread for subsequent operations"

Some open questions:

  • what should happen if a worker is reused for a new thread? Shoudl the JS libraries that were loaded before be unloaded somehow?
  • What to do with JS libraries that expose "void void" functions that don't take any arguments or results but that need to be tied to a particular threads?
  • Should we allow returning to the same threadpool thread? how - by installing a SynchronizationContext transparently? require the user to set things up explicitly?

@imoldfella
Copy link

Maybe one more question - what happens when initial webworker is terminated? are the workers being used for multithreading terminated immediately, or eventually, or never? https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate-dev

@lambdageek
Copy link
Member Author

Maybe one more question - what happens when initial webworker is terminated? are the workers being used for multithreading terminated immediately, or eventually, or never? https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate-dev

@imoldfella what do you mean by "initial webworker"? Right now our deployment model is to run the .NET main thread on the main browser thread. So if you navigate away from a page, for example, the main thread (and with it, all the underlying Emscripten infrastructure) will be gone. Any webworkers will be terminated (as you linked above), but even if they weren't they will probably fail ungracefully since so much of the Emscripten threading support relies on being able to communicate with the main browser thread.

@imoldfella
Copy link

ah, thanks for pointing that out. Normally having webassembly in a worker is convenient because without threads it's difficult to cancel things other than with terminate, but what you described makes perfect sense. Should I be able to use a CancellationToken if I need to stop a task running on a worker thread?

@ghost
Copy link

ghost commented Nov 9, 2023

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

Issue Details

JavaScript interop: JS object references in managed need to be tied to a thread - there's no shared JS memory with WebWorkers

Tracking issue: #85592

Author: lambdageek
Assignees: -
Labels:

arch-wasm, area-System.Runtime.InteropServices.JavaScript, os-browser

Milestone: 8.0.0

@lewing lewing modified the milestones: 8.0.0, 9.0.0 Nov 9, 2023
@pavelsavara pavelsavara self-assigned this Feb 5, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2024
@github-project-automation github-project-automation bot moved this to Done in wasm-mt Aug 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-VM-threading-mono os-browser Browser variant of arch-wasm
Projects
Status: Done
Development

No branches or pull requests

4 participants