Skip to content

fix: await worker-thread termination on stop/close (fixes in-process WASM teardown crash)#11

Merged
wmadden merged 1 commit into
mainfrom
fix/await-worker-termination-on-close
Jun 9, 2026
Merged

fix: await worker-thread termination on stop/close (fixes in-process WASM teardown crash)#11
wmadden merged 1 commit into
mainfrom
fix/await-worker-termination-on-close

Conversation

@wmadden-electric

Copy link
Copy Markdown

Problem

Both worker pools tear down like this:

for (const w of this.workers) {
  w.worker.postMessage({ type: "stop" });
  w.worker.terminate();   // promise never awaited
}

worker.terminate() returns a promise that isn't awaited, so stop() — and the local-server close() path that calls it — resolves while the worker threads are still tearing down.

That's invisible for the standalone server (its own process), but @prisma/streams-local also runs in-process inside @prisma/dev. There, await localServer.close() returns while the touch/segmenter worker threads are still alive, and @prisma/dev immediately tears down PGlite (Postgres compiled to WebAssembly). A worker thread terminating at the same moment another thread frees PGlite's WASM JIT pages races V8's process-global JIT bookkeeping (PKU / ThreadIsolation) and aborts the whole process on Linux:

# Check failed: jit_page_->allocations_.erase(addr) == 1

Every operation succeeds; the process just dies during teardown. Downstream (prisma-next) it surfaced as a flaky vitest "Worker exited unexpectedly" with all tests passing — frequent in high-churn suites that create and close many dev servers in one process, rare in low-churn ones.

Fix

Await termination, and thread that await all the way up the close path so a resolved close() guarantees the worker threads are gone:

  • TouchProcessorWorkerPool.stop() / restart() → async, await Promise.all(workers.map(w => w.terminate()))
  • SegmenterWorkerPool.stop() → same
  • TouchProcessorManager.stop() → async, awaits pool.stop() (and the in-loop pool.restart())
  • app_core close() → async, awaits the worker-pool stops before the rest
  • local/server.ts and server.tsawait app.close()

Notes

  • DurableStreamsLocalServer.close() was already Promise<void>, so this is backward-compatible for callers that await it (@prisma/dev does).
  • Verified: tsc --noEmit clean; local_server, segmenter_behavior, and touch_memory_journal suites pass. The crash itself only reproduces on Linux with an in-process WASM host (PGlite), which this repo doesn't have, so there's no repro test here — the prisma-next e2e suite is the end-to-end guard once a new @prisma/streams-local is published.

🤖 Generated with Claude Code

The touch and segmenter worker pools called worker.terminate() without
awaiting it, so stop() (and the local server close path that calls it)
resolved while the worker threads were still tearing down.

When @prisma/streams-local runs in-process inside @prisma/dev, those lingering
worker threads outlive the server close and race the host process freeing
PGlite (WebAssembly) JIT pages. That trips V8 process-global JIT bookkeeping
(PKU/ThreadIsolation) and aborts the process on Linux:

    # Check failed: jit_page_->allocations_.erase(addr) == 1

Make the pool stop() await every terminate(), and thread that await up through
TouchProcessorManager.stop, the app_core close, and the local server close so a
resolved close guarantees the worker threads are gone.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Will Madden <madden@prisma.io>
@wmadden wmadden merged commit f0ef2d2 into main Jun 9, 2026
7 of 8 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.

2 participants