microsoft / vscode-wasm

A WASI implementation that uses VS Code's extension host as the implementing API
MIT License
383 stars 29 forks source link

Sync-Api doesn't work with multiple workers #23

Open rchiodo opened 2 years ago

rchiodo commented 2 years ago

Suppose you have something like so:

Main Thread - Extension code Worker 1 - Extension sub piece, which starts Worker 2 Worker 2 - Needs to use the sync api

Worker 2 cannot communicate with the Main Thread because its MessageChannel is only with Worker 1.

For CPython I have this exact situation when trying to run the debugger:

Worker 2 and 3 cannot send messages to the Main Thread.

rchiodo commented 2 years ago

I believe there's a solution to this, use a BroadcastChannel instead. (supported by node and web browsers).

Essentially every worker uses the BroadcastChannel instead. When sending messages, a target could be set, something like so:

{
    "dest": "main",
    "src": "worker-<threadid>"
}

Main thread would listen on the same broadcast channel and handle messages with "dest" : "main". Worker threads would listen on the same broadcast channel and handle messages with "dest": "worker-<threadid>" where the thread id matches their own.

I believe that would let messages to go to and from an arbitrary worker to the main thread.