Repository navigation
Description
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:
- 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::TaskRunnerfrom 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. - 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 afterawait WebAssembly.compile(...)runs in "global scope":setTimeout,fetch, etc. throw "Disallowed operation called within global scope", andDate.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:
- Wake-up.
jsg::V8PlatformWrapper::GetForegroundTaskRunnerreturns a wrappingTaskRunner. After it forwardsPostTask/PostNonNestableTask/PostDelayedTaskto the inner runner, it notifies the owningIsolateBasethrough a thread-safe hook that the embedder registers, e.g. akj::Executorplus 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 inIsolateBase's destructor beforeNotifyIsolateShutdown. - 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 aroundWebAssembly.compile/instantiate. - Pump inside the right IoContext. The woken pump has to run the task and the resulting microtasks inside that IoContext, rather than under the
SuppressIoContextScopeused 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 bufferconstructor/instantiate.any.js: Branding,/: BufferSource argument$/, Synchronous options handling: Buffer argument, Empty buffer, Invalid code, Changing the bufferconstructor/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
Serve WPT .wasm resources from bindings without a sidecar
[wpt] Enable wasm/jsapi tests (#7928)
guybedford commentedon Oct 31, 2026
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.
Run the wasm/jsapi tentative WPTs