module: DeserializeWasmModule should not assume synchronous WasmModuleCompilation::Finish

Ouverte
#67,822 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
25/100
Type d'issue
Fonctionnalité
Clarté
Plutôt claire
Activité
Active
Stack technique
javascript
Domaine
backend

Piste de recherche

Start with DeserializeWasmModule in src/node_modules.cc and the Wasm translator in lib/internal/modules/esm/translators.js. Read the V8 Wasm API contract in include/v8-wasm.h and assess the proposed synchronous deserialization API against the async translation alternative. Done means Node and V8 agree on an approach that preserves synchronous ESM translation and handles cache misses without wasted compilation.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
JavaScript
Étoiles
124k
Forks
38k
Merge moyen
4 j 2 h
PR mergées (30 j)
266

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de nodejs/node

Toutes les issues de nodejs/node

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.