|
| 1 | +# In-browser `worker:` (SAB) transport |
| 2 | + |
| 3 | +Run a VGI worker **entirely in the browser** — in a Web Worker, exchanging Arrow batches with |
| 4 | +the DuckDB-WASM extension over a **SharedArrayBuffer duplex-ring channel** — with **no server**. |
| 5 | +This is the WASM-only sibling of the subprocess transport: same streaming `IFunctionConnection` |
| 6 | +paths, only the byte transport differs (SAB rings + `Atomics` instead of an OS pipe). |
| 7 | + |
| 8 | +```sql |
| 9 | +-- direct |
| 10 | +SELECT * FROM vgi_table_function('worker:/workers/example.js', 'count_to', [5]); |
| 11 | +-- catalog |
| 12 | +ATTACH 'worker:/workers/example.js' AS w (TYPE vgi); |
| 13 | +SELECT * FROM w.main.some_table; |
| 14 | +``` |
| 15 | + |
| 16 | +`LOCATION 'worker:<url>'` is any URL `new Worker(url)` accepts (same-origin by default; the |
| 17 | +host bridge gates which URLs SQL may spawn). The worker's JS entry is either a Rust |
| 18 | +`-pthread`/emscripten worker or a plain JS/TS worker that speaks the slot protocol. |
| 19 | + |
| 20 | +## How it works (one paragraph) |
| 21 | + |
| 22 | +The channel is `malloc`'d **inside DuckDB's own wasm linear memory** (a `SharedArrayBuffer` under |
| 23 | +`-pthread`), so every DuckDB scan pthread reaches it natively and the VGI worker gets a view of it |
| 24 | +via one `postMessage` at spawn. It holds a header + N **slots**; each slot is a duplex pair of |
| 25 | +SPSC byte rings (client→worker, worker→client) with blocking `Atomics.wait`/`notify` flow control |
| 26 | +— the ring *is* the chunker, so batches larger than a ring stream through in bounded pieces. A |
| 27 | +connection claims a slot for its lifetime; the worker runs one serve thread per slot. Full wire |
| 28 | +contract: [sab_transport_abi.md](sab_transport_abi.md). |
| 29 | + |
| 30 | +## Multithreading |
| 31 | + |
| 32 | +Parallel table-function scans work: a scan whose worker advertises `max_workers > 1` fans out |
| 33 | +across DuckDB scan threads, each acquiring its own slot + its own worker serve pthread, so N |
| 34 | +serves run **concurrently**. The browser E2E proves this — a single `parallel_probe(max_workers=4)` |
| 35 | +scan under `SET threads=4` reaches an observed **peak concurrency of 4** (a process-global guard in |
| 36 | +the worker's shared memory counts simultaneously-active serves). Note: multiple *separate* queries |
| 37 | +serialize at DuckDB-WASM's single-threaded `AsyncDuckDB` worker boundary — parallelism comes from |
| 38 | +*one* fanned-out scan, not concurrent connections. |
| 39 | + |
| 40 | +## Build |
| 41 | + |
| 42 | +Three independently-built repos against the shared ABI: |
| 43 | + |
| 44 | +- **Extension** (`vgi`): built into the DuckDB-WASM COI build — `build-wasm-coi.sh` (SAB files are |
| 45 | + `#if defined(__EMSCRIPTEN__)`). Native `[sab-conn]`/`[sab-e2e]` unit tests link the same code |
| 46 | + with `-DVGI_SAB_NATIVE_TEST` against a POSIX-shm backend. |
| 47 | +- **Host glue** (`haybarn-wasm`): `lib/js-stubs.js` (the `--js-library` ring stubs) + |
| 48 | + `packages/duckdb-wasm-app/src/lib/vgi-webworker-bridge.ts` (main-thread spawn bridge). Editing |
| 49 | + `js-stubs.js` needs an engine re-link (`rm build/.../duckdb_wasm.{wasm,js}` — CMake misses |
| 50 | + `--js-library` deps). |
| 51 | +- **Worker** (`vgi-rust` → `test/support/sabtable` + `test/support/wasm-worker/build.sh`): the Rust |
| 52 | + serve framework built for `wasm32-unknown-emscripten -pthread`. `-Z build-std` needs |
| 53 | + `RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals -C link-args=-pthread'` or |
| 54 | + wasm-ld rejects `--shared-memory`. |
| 55 | + |
| 56 | +## Testing |
| 57 | + |
| 58 | +- **Native (the fast gate):** `[sab-conn]`/`[sab-e2e]` in `vgi_unit_tests` cover the transport |
| 59 | + logic (bind→init→stream, multi-batch, worker error) over the in-process ring backend — but |
| 60 | + single-threaded, so they cannot exercise the browser threading/heap-view paths. |
| 61 | +- **Browser E2E** (`test/support/wasm-worker/browser-e2e/`): the only test of the real stack |
| 62 | + (engine + wasm extension + worker module + bridge + actual SAB/Atomics). Reliably green across |
| 63 | + fresh loads: `LOAD` → direct scan → multi-batch → ATTACH+discovery → concurrent → worker-error → |
| 64 | + parallel-serve proof (`maxConcurrency=4`). Headless Chromium under puppeteer can session-close |
| 65 | + under nested-worker load; MCP Playwright / a real browser is stable. `probe-*.mjs` are isolation |
| 66 | + harnesses (`VGI_ENTRY=probe-throw.mjs node serve.mjs`). |
| 67 | + |
| 68 | +## Reliability notes / known limitation |
| 69 | + |
| 70 | +The transport is reliable on the happy path (streaming, catalog, concurrent scans, parallel |
| 71 | +serve). Two emscripten-pthread footguns are handled and documented in |
| 72 | +[sab_transport_abi.md](sab_transport_abi.md) → *Browser implementation notes* (stale cached heap |
| 73 | +views → read `wasmMemory.buffer`; per-realm channel offset → re-publish before each ring op) — |
| 74 | +getting either wrong causes intermittent, scheduler-dependent hangs. |
| 75 | + |
| 76 | +**Known limitation:** when a worker **throws** under `threads > 1` amid the full suite's heavy |
| 77 | +concurrent load, DuckDB-WASM *occasionally* leaves the async query promise unsettled — a flaky |
| 78 | +DuckDB-WASM-side error-propagation race (Asyncify + pthread + C++ exception), **not** a transport |
| 79 | +bug: the transport tears down cleanly (all slots freed, rings closed, worker idle — verified), a |
| 80 | +worker throw propagates fine under `threads=4` in isolation, and a generic non-VGI parallel throw |
| 81 | +settles fine. The E2E runs the worker-error case under `threads=1` for determinism. |
0 commit comments