Skip to content

WebAssembly.compile()/instantiate() of bytes never settle unless other I/O wakes the isolate #7930

Open

Description

@npaun

Problem

When Wasm code generation is allowed (env.unsafe.eval, or the allow_insecure_inefficient_logged_eval flag), a promise from WebAssembly.compile(bytes) or WebAssembly.instantiate(bytes) does not settle on its own. Two things go wrong:

  1. The promise settles only if something else wakes the isolate. V8 decodes and compiles on a background thread, then finishes the job with a foreground task, which it posts to the isolate's v8::TaskRunner from that background thread. workerd drains that queue (v8::platform::PumpMessageLoop(..., kDoNotWait)) only after it has run JavaScript for some other reason (io-context.c++:1725-1726). Posting the task does not wake the KJ event loop. If nothing else is pending, the request is cancelled as hung.
  2. The continuation runs without an IoContext. When some other event does trigger the pump, the pump runs under SuppressIoContextScope, along with the microtasks it queues. That code was written for FinalizationRegistry cleanup. So code after await WebAssembly.compile(...) runs in "global scope": setTimeout, fetch, etc. throw "Disallowed operation called within global scope", and Date.now() returns 0.

Neither jsg::V8PlatformWrapper::GetForegroundTaskRunner (v8-platform-wrapper.h:25-27) nor server::WorkerdPlatform (v8-platform-impl.h:39-41) wraps the task runner. They return the default platform's runner unchanged.

Disabling async compilation doesn't help. The V8 that workerd builds no longer has --wasm-async-compilation, and SetFlagsFromString silently ignores the flag. WasmEngine::AsyncCompile compiles synchronously only under --wasm-jitless.

Repro

wd-test with an unsafeEval binding named unsafe:

const EMPTY_MODULE = 'new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0])';

// Hangs: "The Workers runtime canceled this request because it detected that your Worker's code
// had hung". The same happens for invalid bytes and for WebAssembly.instantiate(bytes).
export const compileBytes = {
  async test(ctx, env) {
    await env.unsafe.eval(`WebAssembly.compile(${EMPTY_MODULE})`);
  },
};

// The promise settles only once the 2 s timer fires, and the continuation has no IoContext:
// Date.now() - start is about -1.79e12, and setTimeout() throws "Disallowed operation called
// within global scope".
export const compileWithTimerPending = {
  async test(ctx, env) {
    const start = Date.now();
    const keepAlive = new Promise((r) => setTimeout(r, 2000));
    await env.unsafe.eval(`WebAssembly.compile(${EMPTY_MODULE})`);
    console.log(Date.now() - start);
    setTimeout(() => {}, 1);
    await keepAlive;
  },
};

Fix sketch

A complete fix needs three pieces:

  1. Wake-up. jsg::V8PlatformWrapper::GetForegroundTaskRunner returns a wrapping TaskRunner. After it forwards PostTask/PostNonNestableTask/PostDelayedTask to the inner runner, it notifies the owning IsolateBase through a thread-safe hook that the embedder registers, e.g. a kj::Executor plus a refcounted, atomically detachable target. The hook has to tolerate tasks posted after the isolate, or the context that started the job, is gone. V8 can still post while it tears down a job, so the target must be detached in IsolateBase's destructor before NotifyIsolateShutdown.
  2. Keep the request alive. While a Wasm async compile is in flight, the IoContext that started it needs a pending event (registerPendingEvent()), so hang detection doesn't cancel it. Nothing in workerd sees the compile start today, so this needs a hook, for example in the API layer or an embedder callback around WebAssembly.compile/instantiate.
  3. Pump inside the right IoContext. The woken pump has to run the task and the resulting microtasks inside that IoContext, rather than under the SuppressIoContextScope used for FinalizationRegistry cleanup. It must not reintroduce IoContext leakage into FinalizationRegistry callbacks.

In edgeworker, isolates can be locked from different threads over their lifetime, so "the isolate's thread" isn't fixed. The wake-up has to target whichever thread next runs the waiting request. edgeworker's EdgeWorkerPlatform (src/edgeworker/server/v8-platform-impl.h) also forwards GetForegroundTaskRunner to the default platform unchanged, but jsg wraps every platform in V8PlatformWrapper, so a wrapper-level fix would cover it. In production this path can't be reached today, because Wasm code generation is rejected before compilation starts.

WPT subtests blocked

In src/wpt/wasm/jsapi-test.ts (branch npaun/wpt-wasm-jsapi), these are disabled because they hang:

  • constructor/compile.any.js: Branding, Empty buffer, Invalid code, Result type, Stray argument, Changing the buffer
  • constructor/instantiate.any.js: Branding, /: BufferSource argument$/, Synchronous options handling: Buffer argument, Empty buffer, Invalid code, Changing the buffer
  • constructor/instantiate-bad-imports.any.js: /^WebAssembly\.instantiate\(buffer\): /
  • constructor/multi-value.any.js: all

This also blocks idlharness.any.js "idl_test setup" (and with it the Memory/Module/Instance object subtests it generates). Its createWasmModule() compiles in a .then. That continuation is outside env.unsafe.eval, so it also needs code generation to be allowed there.

Activity

  1. guybedford commented on Oct 31, 2026

    @guybedford
    Member

    I think this will resolve naturally when we fully support dynamic Wasm compilation is resolved in due course. If we want to enable some kind of flag in the mean time for this prior to a full release that could be an option too.

  2. added a commit that references this issue on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions