Skip to content

module: DeserializeWasmModule should not assume synchronous WasmModuleCompilation::Finish #67822

Backlog

Description

@backes

Problem

DeserializeWasmModule in src/node_modules.cc (introduced in #66191) deserializes cached WebAssembly modules via v8::WasmModuleCompilation:

WasmModuleCompilation compilation(options);
compilation.SetHasCompiledModuleBytes();
compilation.OnBytesReceived(wire.data(), wire.size());
compilation.Finish(
    isolate,
    [&](WasmStreaming::ModuleCachingInterface& cache) {
      state->accepted = cache.SetCachedCompiledModuleBytes(code);
    },
    [state, isolate](std::variant<Local<WasmModuleObject>, Local<Value>> result) {
      // When the cached code is accepted, V8 resolves synchronously within
      // Finish(). Otherwise V8 falls back to compiling the wire bytes
      // asynchronously; that result arrives later and is unused.
      if (!state->in_finish) return;
      if (auto* module = std::get_if<Local<WasmModuleObject>>(&result)) {
        state->module.Reset(isolate, *module);
      }
    });
state->in_finish = false;
if (!state->accepted || state->module.IsEmpty()) return {};
return state->module.Get(isolate);

This currently relies on an undocumented implementation detail: v8::WasmModuleCompilation::Finish is documented in V8's include/v8-wasm.h as asynchronous ("This callback will also be called in foreground, but not necessarily within this call").

On the V8 side, we want to unconditionally defer all streaming/async completion callbacks from Finish() to a non-nestable foreground task rather than executing completion synchronously on the Finish() call stack. Doing so currently breaks parallel/test-compile-cache-wasm on V8's node-ci integration bot because DeserializeWasmModule assumes resolution_callback runs synchronously inside Finish().

As a secondary issue, using WasmModuleCompilation here also triggers a wasted asynchronous compilation of the wire bytes on every cache miss/rejection (whose result is later discarded when state->in_finish is false).

Proposed solutions

Since translators.get('wasm') in lib/internal/modules/esm/translators.js calls getWasmCompileCacheEntry(...) synchronously and falls back to synchronous new WebAssembly.Module(...), there are two clean ways forward:

  1. Add a dedicated synchronous deserialization API in V8 (e.g. v8::WasmModuleObject::Deserialize(Isolate*, std::span<const uint8_t> wire_bytes, std::span<const uint8_t> serialized_bytes, const CompileOptions& options = {}), symmetric with WasmModuleObject::Compile and CompiledWasmModule::Serialize) and switch DeserializeWasmModule in src/node_modules.cc to use it. This keeps Node's ESM Wasm translator synchronous, eliminates the wasted background compilation on cache rejection, and allows V8 to unconditionally defer WasmModuleCompilation::Finish.
  2. Consume WasmModuleCompilation::Finish asynchronously in Node (if the Wasm compile-cache lookup/translation path is changed to be async).

Option 1 looks simplest and cleanest for both Node.js and V8. Please let us know if that API shape works for Node.js, and we can add it to V8 first and then migrate v8/node + upstream nodejs/node.

Activity

  1. anujsonkar1526-coder commented on Dec 1, 2026

    @anujsonkar1526-coder

    Hi @backes,
    I’m interested in contributing to this issue. I’ve reviewed the problem and understand that the current implementation assumes WasmModuleCompilation::Finish() invokes its callback synchronously.

    I’d like to work on this, particularly the proposed synchronous deserialization API approach.

    Could you please confirm whether this approach is acceptable to the Node.js maintainers and whether I can take up this issue?

    I’ll wait for your guidance before starting implementation to avoid duplicate work or going in the wrong direction.

    Thank you!

  2. added
    module
    v8 engine
    wasm
    on Dec 3, 2026
  3. backes commented on Dec 4, 2026

    @backes
    ContributorAuthor

    Hi @anujsonkar1526-coder, thanks for volunteering!

    I have the V8-side CL in flight at https://crrev.com/c/8888870, which adds a synchronous v8::WasmModuleObject::Deserialize(isolate, wire_bytes, serialized_bytes[, options]) API. Once that lands in V8, you are very welcome to take on the Node.js side (DeserializeWasmModule in src/node_modules.cc).

    Note that the new API won't be present in nodejs/node's deps/v8 until the next V8 update (or it can be targeted at v8/node first for V8's node-ci integration, or guarded by V8 version if Node.js maintainers prefer — I'll defer to the Node.js maintainers on the preferred landing order here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    modulev8 enginewasm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions