Skip to content

JS interop should support threading #76956

Closed

Description

@lambdageek

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

Activity

  1. ghost added
    untriaged
    on Oct 12, 2022
  2. added and removed
    untriaged
    on Oct 12, 2022
  3. added this to the 8.0.0 milestone on Oct 12, 2022
  4. lambdageek commented on Oct 20, 2022

    @lambdageek
    MemberAuthor

    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?

  5. lambdageek commented on Oct 20, 2022

    @lambdageek
    MemberAuthor

    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?
  6. imoldfella commented on Nov 30, 2022

    @imoldfella

    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

  7. lambdageek commented on Dec 1, 2022

    @lambdageek
    MemberAuthor

    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.

  8. imoldfella commented on Dec 1, 2022

    @imoldfella

    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?

  9. added
    os-browser
    arch-wasm
    on Nov 9, 2023
  10. modified the milestones: 8.0.0, 9.0.0 on Nov 9, 2023
  11. self-assigned this
    on Feb 5, 2024
  12. locked and limited conversation to collaborators on Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-VM-threading-monoos-browser

Type

No type

Projects

  • Status
    Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions