Skip to content

Instantiate the browser module owner before warming its thread pool - #19

Merged
TJKoury merged 4 commits into
mainfrom
fix-browser-host-owner-first
Nov 16, 2026
Merged

TJKoury merged 4 commits into
mainfrom
fix-browser-host-owner-first

Conversation

@TJKoury

@TJKoury TJKoury commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

What was wrong

The browser host awaited its warm thread pool before it instantiated the owning instance. A wasi-threads module with the stack-first layout sets an instance's thread-local base only in the instance that wins the shared-memory initialization; every later instance skips that assignment. With a pool instance first, the owner's thread-local base stayed at zero instead of 65,536, so its thread-local block sat on top of its own main stack (0 to 65,536). Ordinary stack writes then changed thread-local data, and results came out wrong without any error.

Node was never affected: it creates pool instances lazily, after the owner. The fault needs a browser host with the thread option on (enableBrowserWasiThreads, or the __SDM_ENABLE_BROWSER_WASI_THREADS__ global); with the option off there is no pool and no fault. Every released version up to and including 0.10.1 has it.

How it was found

The propagator/sdn-ap module returned different positions in Chromium with a prewarmed pool than on one thread, while matching in Node. In released sdn-ap 0.6.63 the thread-local block is 343,988 bytes: it spanned 0 to 343,988 with the pool first, 65,536 to 409,524 with the owner first. Its frame output differs from the one-thread result at 2, 4 and 8 total threads; a sampled sweep of direct states agreed, so whether a given workload shows it depends on how deep the owner's stack goes.

Shown both ways: forcing the pool-first order in Node reproduces the corruption, and owner-first in Chromium removes it.

The change

src/host/browserModuleHarness.js: the owner is instantiated first, then the pool is created, then constructors run. The wasi.thread-spawn import closes over the thread host, so constructors can still spawn threads. No change to guest code, pool sizing, the Node worker path or the WasmEdge path.

The regression test

test/browser-owner-tls.test.js compiles a small fixture (a thread-local array and a stack array, no other content) through the SDK with the stack-first layout. In Node, Chromium, Firefox and WebKit it requires the owner's thread-local data to lie outside its stack, a canary to survive stack writes and a child thread, and a constructor to be able to spawn. The pool-first order in Node is the negative control and must corrupt the canary. No binary fixture is committed. The test is part of test:host-surfaces.

What was run

  • The regression: 5 of 5 pass.
  • Host and thread tests: 114 passed, 0 failed, 2 skipped for a missing prerequisite; the three-runtime thread parity test passes in a browser, native WasmEdge 0.16.4 and Docker WasmEdge.
  • npm test and check:compliance pass.
  • With this host, sdn-ap's frame output matches its one-thread result in 32 cases: Node, Chromium, Firefox and WebKit, at 1, 2, 4 and 8 total threads, on 16,755 and 100,000 records.

For consumers

Take a release that contains this change. Compiled modules do not need a rebuild: the change is host JavaScript only. A consumer that bundles or vendors the SDK's host code rebuilds and redeploys that bundle. Until then, leave the browser thread option off.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PT7jJdf1uRvH9udhT59VMh

TJKoury and others added 4 commits November 16, 2026 10:05
The first WASI threads instance initializes the owner's TLS base. Warming
secondary instances first leaves the owner's TLS overlapping its stack.
Instantiate the owner first and defer constructors until the pool is ready.

Co-Authored-By: Codex <noreply@openai.com>
Compile a synthetic TLS and stack fixture through the SDK. Require independent
owner and child TLS with constructor spawning in Chromium, Firefox and WebKit.
Use secondary-first Node instantiation as the corruption negative control.

Co-Authored-By: Codex <noreply@openai.com>
Use a test-local compiler wrapper to request the linker layout whose
uninitialized owner TLS overlaps its stack, independent of toolchain defaults.

Co-Authored-By: Codex <noreply@openai.com>
Co-Authored-By: Codex <noreply@openai.com>
@TJKoury
TJKoury merged commit 44469e6 into main Nov 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant