Skip to main content
Back to blogCopy Markdown
·9 min read
·turbopackwebassemblyemscriptenjavascriptwebdev

Turbopack build hangs: the WASM worker that spawns itself

My Next.js 16 build sat waiting forever, no error, because a LibRaw WASM build used -pthread. The single-threaded fork and four more quiet failures. →

Óscar Gallego

Software engineer · Málaga

A worker pointing back at its own file in a closed loop, next to a stalled build progress bar
On this page

Short answer: my Turbopack build didn’t fail. It waited. LibRaw, the library I use to decode RAW files in the browser, shipped an Emscripten build compiled with -pthread, and that generated code spawns workers of its own module from inside a worker. The fix was dropping -pthread in a fork, libraw-wasm-nothread.

None of the five failures in this story threw an error. The build just sat there. A Promise.all never settled. Every photo claimed it was taken in January 1970. SnapCompress, my in-browser image compressor, leaked memory because of something the README never mentioned. And the release workflow kept reporting “already published” for a version that wasn’t on npm.

When there’s no stack trace to search for, what helps is knowing what each kind of silence looks like. That’s what this post is about, along with what changed in each case.

The build doesn’t crash: it waits on a worker that spawns itself

By September 2026, Turbopack was already the default bundler in Next.js 16. The Turbopack docs say so plainly, and no configuration is needed to use it. So my build went through Turbopack whether I’d picked it or not, and with the original package, libraw-wasm, it hung with no error.

The cause is how Emscripten does threads. Per its pthreads docs, it implements them with SharedArrayBuffer and Web Workers, and you turn them on by passing -pthread at compile and link time. The generated runtime keeps a pool of workers for those threads. In the libraw.js that the original package shipped, this is the line that creates each pool worker in the default case (reformatted for the post; the file itself is minified):

worker = new Worker(new URL("libraw.js", import.meta.url), {
  type: "module",
  name: "em-pthread",
});

Look at the file it loads: the same libraw.js that line lives in. The full graph: index.js starts worker.js, worker.js imports libraw.js, and libraw.js starts workers running libraw.js. Turbopack treats new Worker() expressions as dependencies to resolve at build time, and this one points back at its own module, from inside a worker. The bundler tries to resolve that self-referential graph and never gets out.

In my case it was an Emscripten module built with -pthread. I don’t know how many other dependencies do the same, but if a build of mine stalls with no message, that’s the first thing I’d look for.

LibRaw didn’t need threads to work: dropping -pthread broke the loop

LibRaw works without threads. Working isn’t the same as running just as fast: I didn’t benchmark with and without threads, so I don’t know how much speed I gave up.

The September commit that created the fork removed -pthread and -s USE_PTHREADS=1 from the final link, took -lpthread and OpenMP out of LibRaw’s configure step, and bumped the Emscripten version in the CI workflows while it was at it. That’s where the name libraw-wasm-nothread comes from. And since the same commit changed both the flags and the Emscripten version, it isn’t a single-variable experiment.

Deleting the flag wasn’t quite enough. According to the commit that fixed it the next day, the final link failed with undefined symbol: operator new, and it suspects the C++ runtime the wrapper needs had been coming in as a side effect of -pthread. Either way, it switched the link from emcc to em++, the right driver for C++.

The wrapper code didn’t change. The shape of the graph did: the libraw.js the fork publishes has no new Worker at all, which leaves a single worker, the one index.js creates for worker.js. An agent checked this against the published npm package for this post.

Emscripten’s docs explain why there’s no middle ground: “It is not possible to build one binary that would be able to leverage multithreading when available and fall back to single threaded when not”. You build with threads or without them. And dropping -pthread gets rid of something else the same page warns about: pthreads code needs COOP and COEP headers in production.

Two calls at once, one slot for the reply

The next two failures predate the fork. They’re from June 2026, and the fork inherited them already fixed. They’re here because they failed just as quietly.

Promise.all([raw.metadata(), raw.imageData()]) hung forever with no error. The worker client kept a single slot for the pending reply. The metadata reply arrived first, resolved the imageData promise with the wrong payload, and the metadata promise never resolved at all.

The June fix, which is in the repo’s public history, gives each call an id and keeps pending calls in a map. Here’s the pattern, adapted from the fork’s index.js with the per-instance queue left out:

class WorkerClient {
  constructor(url) {
    this.worker = new Worker(url, { type: "module" });
    this.pending = new Map(); // id -> { resolve, reject }
    this.nextId = 0;
    this.worker.onmessage = ({ data }) => {
      const slot = this.pending.get(data?.id);
      if (!slot) return; // unknown or already settled reply: ignore it
      this.pending.delete(data.id);
      if (data.error) slot.reject(new Error(data.error));
      else slot.resolve(data.out);
    };
  }

  call(fn, ...args) {
    return new Promise((resolve, reject) => {
      const id = this.nextId++;
      this.pending.set(id, { resolve, reject });
      this.worker.postMessage({ id, fn, args });
    });
  }

  dispose() {
    this.worker.terminate();
    for (const { reject } of this.pending.values()) reject(new Error("disposed"));
    this.pending.clear();
  }
}

The worker has to send back the id it received; in the fork that’s self.postMessage({id, out}). The real code also chains each instance’s calls through a queue, because LibRaw is stateful: you open a file, then ask for that file’s metadata or pixels. The map keeps a reply from resolving the wrong promise. The queue keeps two calls from stepping on the same state.

How to spot it: each call works on its own, and together they hang. If that happens with any worker client, count how many resolvers it holds at once.

Every photo was from January 1970

Also in June 2026, every capture date came out as January 1970. LibRaw returns the timestamp in seconds. new Date() expects milliseconds: MDN defines it as the number of milliseconds since January 1, 1970, UTC. Hand it seconds and any recent date lands in the first few weeks of 1970.

With a Sony ARW file, the date went from 1970-01-20 to 2023-04-29 after one line changed to new Date(metadata.timestamp * 1000). That was checked against the real file.

This is the sneakiest of the five, because it returns data that looks fine. A date is a date. If a pile of your photos cluster in January 1970, someone is most likely handing you seconds.

The leak was in the README

Each LibRaw instance owns its own worker and its own WASM heap, and neither is freed until you call dispose(). The original README didn’t say so. SnapCompress leaked memory because of it.

The code had dispose() since June; the README just never told you to call it. The September commit added a note to the fork’s README: each instance owns a worker, and you should call dispose() in a finally so it still runs if open() or imageData() throws.

If you create one instance per photo, count the live workers after processing a few. If the number doesn’t go down, you have this problem, in this library or in any other that hides a worker behind a new.

The release was idempotent for the wrong package

The fork inherited the original’s release workflow. Before publishing, it checked npm for whether the version already existed, so it wouldn’t fail when a tag push and a manual run fired the same release. But it checked libraw-wasm@<version>, the original package, not the fork.

The release never published: the step said “already published” and npm publish never ran. The September commit changed the name in the check to libraw-wasm-nothread.

An idempotent step that answers “already done” is the hardest kind to catch, because the log stays green. When you fork something, grep for the old name in .github/ before your first release.

What the fork costs me, and what I don’t know

A fork is maintenance I didn’t have before. Whatever changes in the original package, I have to bring over myself, and the release workflow is now mine, which has already bitten me once.

What it costs me in speed is still unmeasured. The fork’s README says LibRaw’s demosaic step wasn’t using threads in the original build anyway, but it bases that on an issue filed against the original package, not on a measurement of mine. What the published package does show is that the recursive worker is gone.

The June 2026 fixes are also in the latest published version of the original package: for this post, an agent found the pending-call map and the timestamp multiplication in its code. But they date from June, and nothing stops that code from changing in a future release of the original.

If your Turbopack build hangs with no message

First, confirm it’s Turbopack. The Next.js 16 docs tell you how to opt out: next build --webpack. If webpack finishes and Turbopack doesn’t, you’ve narrowed it down: something Turbopack is trying to resolve in your graph.

Next, look for Emscripten pthread workers in your dependencies. The runtime names them em-pthread:

grep -rl --include='*.js' 'em-pthread' node_modules/

For this post, an agent ran that command in a clean folder with both packages installed. It returned the libraw.js and worker.js from libraw-wasm, and nothing from libraw-wasm-nothread.

If a dependency shows up and you control its build, check whether it really needs threads and try compiling it without -pthread. If you don’t control it, look for a single-threaded variant or fork it. Don’t count on turbopackIgnore: Turbopack documents that comment for new Worker() expressions, but the fork’s README says it was tried first and wasn’t enough.

Sources

  • Emscripten, Pthreads support, read November 15, 2026: -pthread, the worker pool and the COOP/COEP headers.
  • Next.js, Turbopack and the Next.js 16 announcement, read November 15, 2026: default bundler, --webpack and magic comments.
  • MDN, Date() constructor, read November 15, 2026.
  • The fork’s public repo, libraw-wasm-nothread, and the npm packages. For this post, an agent read the public history, the June and September commits and the published code; my side of the story comes from my notes on each failure.

Related reading: another failure that only showed up off my machine: tests that pass locally and fail on Vercel.


P.S. Ever had a build that just sits there without saying a word? Tell me what it turned out to be on LinkedIn.