Skip to content

Latest commit

 

History

History
368 lines (327 loc) · 30 KB

startup-snapshot.md

File metadata and controls

368 lines (327 loc) · 30 KB

Startup Snapshots

How workerd evaluates a Worker's top-level code once, in a throwaway isolate, and starts the Worker from a V8 startup snapshot of the result.

For the JSG reference material (the jsgSnapshotStateless / snapshotClone() contract, the SnapshotArtifact fields), see src/workerd/jsg/README.md §Startup Snapshot. For the declarations, see src/workerd/jsg/snapshot.h and the snapshot section of src/workerd/jsg/setup.h.

Overview

Starting a Worker normally means compiling its modules, evaluating the main module graph and running every top-level statement, inside the isolate that will serve requests. With a startup snapshot the same work happens in a zygote: a second, throwaway Worker whose isolate was created by a v8::SnapshotCreator. When the zygote's top level has run, its context is serialized into a blob, and the real Worker's isolate is created from that blob. Its context comes out of v8::Context::New() with the module graph already evaluated and the top-level state in place, so the real Worker skips evaluation entirely.

Everything the real Worker needs from the zygote travels in a jsg::SnapshotArtifact (src/workerd/jsg/snapshot.h): the V8 blob, the external-reference table it was built against, and the native side of what the blob cannot carry (see What is transferred).

The feature is gated and best-effort. Any failure in the zygote means the Worker starts the way it always has; see Failures.

When it runs

factory_new_worker() in src/workerd/server/factory/worker-factory.c++ runs the zygote when all of these hold:

  • the STARTUP_SNAPSHOT autogate is on (workerd-autogate-startup-snapshot in a config's top-level autogates = [...], or WORKERD_ALL_AUTOGATES=1);
  • no inspector is configured: neither --inspector-addr nor the enable_nodejs_inspector_local_dev flag. The inspector attaches to the Worker's isolate, and top-level code has to run there for DevTools to see its console output and stop at its breakpoints;
  • supportsStartupSnapshot() accepts the Worker: ES-modules syntax (not a service-worker script), no Wasm modules, not a Python Worker, and only bindings of kinds that transfer (WorkerdApi::isBindingTransferableThroughSnapshot(); see step 3 below). A dynamically loaded Worker (dynamicSource), whose env arrives as capabilities rather than channel numbers, never qualifies.

The zygote (PREPARE_SNAPSHOT)

jsg::IsolateBase::isPreparingSnapshot() is true in the zygote's isolate. In order:

  1. makeIsolate(<name>-snapshot, InspectorPolicy::DISALLOW, MutableSnapshot{artifact}). The IsolateBase constructor creates the isolate through a v8::SnapshotCreator, and every registerExternalReference() call from then on lands in artifact.externalReferences.

  2. newContext() (jsg::Isolate::newContextWithWrapper, setup.h) creates the context and calls installRestoreTimeGlobalsForZygote(): V8 leaves some properties out of a context built in a serializer-enabled isolate and adds them when the context is restored (Error.stackTraceLimit, WebAssembly, SharedArrayBuffer, the Iterator helpers behind shipped flags, Temporal and Date.prototype.toTemporalInstant). The zygote installs Error.stackTraceLimit as the data property V8 would, and the others as fail-closed accessors that throw and record the use, so top-level code sees a normal context and cannot bake a wrong feature test in, even when it catches the error. The Temporal names are fail-closed only in a Worker with the temporal flag; without it they are absent, as in a normal context. See the comment block above RESTORE_TIME_GLOBALS in setup.h.

  3. The zygote Worker::Script and Worker are built exactly as the real ones would be, with an ErrorReporter of their own, bindings included. A binding object is a description (a channel number and names) that every request resolves through its own IoContext, so creating it does no I/O, and it transfers with env like any other live object (snapshotClone() on Fetcher, KvNamespace, DurableObjectNamespace, ...). supportsStartupSnapshot() keeps a Worker out of the pipeline when one of its bindings is of a kind that cannot transfer (WorkerdApi::isBindingTransferableThroughSnapshot(): cryptoKey, memoryCache, wrapped).

  4. The module graph is evaluated: this is where top-level code runs. Console output it produces is held back (Worker::Isolate::deferConsoleLine()); see Console output. The env object is pinned into the snapshot right before (pinIntoSnapshot(), index BINDINGS_SCOPE_SNAPSHOT_INDEX), and the main module's namespace right after a successful evaluation (MAIN_MODULE_NAMESPACE_SNAPSHOT_INDEX).

  5. Still in the Worker constructor, before the context's handle scope closes:

    • settleFinalizationRegistriesForSnapshot() runs the FinalizationRegistry cleanups the top level left due, the way the event loop would after its job: microtask checkpoint, WeakRef kept objects cleared, a full GC, then every task V8 posted, round after round until a GC posts nothing more (a bounded number of rounds; beyond it the zygote fails). The callbacks run without an IoContext, as they do at request time, under the startup limits; an exception in one is reported as an uncaught exception and does not fail the zygote. This is the last JavaScript the zygote runs: everything recorded from here on could otherwise be changed behind the record's back, and the live-object record below keys wrappers by a cppgc shim address that is unique only while nothing allocates;
    • removeRestoreTimeGlobalsBeforeSnapshot() records what the top level left of Error.stackTraceLimit, and which fail-closed properties it deleted, and removes every restore-time property, so that V8 can add them back on restore. A fail-closed property the top level used (whether or not it caught the error), a property it defined in their place, or a holder it made non-extensible, fails the zygote here;
    • the global scope's event listeners are recorded (EventTarget::forEachSnapshotListener()) into artifact.listenerRecords: for each, its identity and the callback and receiver its registration derived are pinned, with its once and passive options;
    • the global scope's own state for its JavaScript, the process and Buffer values the top level resolved or assigned (ServiceWorkerGlobalScope::getSnapshotState()), is pinned (artifact.globalScopeStateIndex);
    • stashPerIsolateBootstrapForSnapshot() bundles the per-isolate bootstrap's JS-held state into one object and pins it (artifact.bootstrapStateIndex), the last of the record steps.

    Only a top level that ran to completion is recorded. If it failed (a parse error, an uncaught exception, a startup limit: impl->permanentException is set), none of the record steps run and the Worker constructor throws instead of going on to step 6: CreateBlob() on a half-built context would only risk a V8 fatal. The context is disposed with the script.

  6. The zygote's global scope is cleared, the script's module context handle dropped, and IsolateBase::createSnapshotBlob() runs. Its steps are numbered in setup.c++:

    1. bakeModuleRegistryIntoSnapshot(): every materialized module handle of the legacy registry is pinned and recorded in artifact.moduleRecords; the handles are reset. With the new module registry, modules::recordIsolateModuleRegistryForSnapshot() instead pins the v8::Module of each of the registry's resolutions (a context type and a specifier, query and fragment included) and records them in artifact.newModuleRegistryRecords.
    2. WrappablesSerializeMetadata::recordLiveWrappables(): every live JSG object is recorded while it still exists, by its tag alone for a stateless type, as a snapshotClone() otherwise (artifact.liveWrappables), keyed by its CppgcShim address. The metadata roots every recorded shim, so no shim allocated later can take a recorded address.
    3. HeapTracer::destroyLiveWrappableInstances(): every live JSG object is destroyed, which resets the member handles CreateBlob()'s global-handle check would otherwise reject. A full GC then collects what that released. Registered objects that only C++ handles reached die here, and CreateBlob()'s serializer rejects an isolate whose registries hold cleared cells, so the task runner is pumped under a DisallowJavascriptExecutionScope: V8's cleanup task pops each cell and the callback call throws instead of running. No JavaScript runs past step 2. Any such cell fails the zygote: those objects are C++-held state the snapshot does not carry, and their cleanups must not be dropped silently.
    4. Isolate-level handles are reset (the opaque template, workerEnvObj, workerExportsObj). The templates of Rust JSG resource types are first pinned at isolate level and recorded by TypeId in artifact.rustResourceTemplates (pinResourceTemplatesIntoSnapshot(), src/rust/jsg/ffi.h).
    5. bakeConstructorTemplatesIntoSnapshot(): every materialized JSG resource constructor template is pinned at isolate level and recorded, by position, in artifact.constructorTemplateIndices.
    6. Struct-type handles are reset.
    7. The default-context handle is reset. A heap that still holds a suspended generator, such as an async function the top level left awaiting a promise, fails the zygote: CreateBlob(kClear) discards the bytecode a generator resumes in, which is also why V8's bytecode flushing never discards it. The V8 patch Add-SnapshotCreator-HasSuspendedGenerators adds SnapshotCreator::HasSuspendedGenerators(), which looks for one among the objects CreateBlob() would serialize. Then SnapshotCreator::CreateBlob() runs. V8 calls serializeInternalFields() for each wrapper's tag field and serializeApiWrapperCallback() for each wrapper's shim pointer; the latter writes the object's liveWrappables index as the payload. Afterwards WrappablesSerializeMetadata::dropUnreachableWrappables() raises what the callbacks found (an unknown shim, a type with no transfer) and drops the clones of objects that were recorded but not reachable at CreateBlob().
  7. Back in factory_new_worker(): if the zygote threw or reported errors, the artifact is discarded. Otherwise extractSnapshotArtifact() hands it over, the deferred console lines are printed, and the real isolate is created from it.

The real Worker (START_FROM_SNAPSHOT)

jsg::IsolateBase::isStartingFromSnapshot() is true in the real Worker's isolate when a FinalizedSnapshot was passed to makeIsolate(). The kj::Arc<SnapshotArtifact> stays alive as long as the isolate does. In order:

  1. IsolateBase creates the isolate with CreateParams::snapshot_blob and the artifact's external references; the jsg::Isolate constructor then runs restoreConstructorTemplatesFromSnapshot(), pairing each recorded index with the template slot at the same position. The enumeration order is fixed at compile time, and the producer and the consumer are the same binary.
  2. newContext() runs prepareWrappableDeserializeMetadata(): for each liveWrappables slot it builds the object the restored wrapper will be attached to (a stateless type's constructor, or a second snapshotClone() of the stored clone) and pre-allocates its CppgcShim, since the deserializer callback runs under V8's DisallowGarbageCollection and may not allocate.
  3. v8::Context::New() deserializes the context. deserializeInternalFields() restores the wrapper-tag marker of every wrapper; deserializeApiWrapperCallback() links each wrapper to the shim pre-allocated for its payload's slot (HeapTracer::linkRestoredWrapper()). Then relinkReferences() hands each restored object that referred to other transferred objects (Wrappable::snapshotReferences(), such as Crypto's subtle) their restored instances (snapshotRelink()), releaseSlotsWithChecks() verifies every serialized slot was linked exactly once, and replayRestoreTimeGlobalsAfterLoad() re-applies the recorded Error.stackTraceLimit state and deletions on top of the defaults V8 just installed.
  4. The Worker::Script constructor rebuilds the per-isolate bootstrap's C++ state from the pinned object instead of running the bootstrap scripts (restoreBootstrapState()), hands the fresh global scope the recorded process/Buffer state (restoreSnapshotState()), re-registers each recorded listener in C++ (EventTarget::restoreSnapshotListener(), which runs no JavaScript and does not consult the global's possibly replaced addEventListener), and seeds the module registry: addSnapshotSeed() per moduleRecords entry, then compileModules(), which registers a bundle ES module the zygote instantiated around its baked instance (tryTakeSnapshotModule()) and still registers the lazily compilable builtins the zygote never touched, then applySnapshotSeeds(). Runtime import(), require() and the Buffer/process globals therefore resolve to the instances the top level evaluated against. With the new module registry, ModuleRegistry::attachToIsolate() instead rebuilds the isolate's resolution and instantiation tables from newModuleRegistryRecords (IsolateModuleRegistry::restoreFromSnapshot()), so that import() and import.meta find the modules the top level evaluated. Every synthetic module of the new registry is created with the same evaluation steps, which Worker::Isolate registers as an external reference (getSyntheticModuleEvaluationStepsRef()).
  5. The Worker constructor takes env from the snapshot (extractOnceFromSnapshot()), complete with its bindings: factory_new_worker() compiles no bindings for a Worker started from a snapshot, while ctx.exports is still built for it afterwards, by worker_set_ctx_exports(). It takes the main module's namespace from the snapshot instead of resolving the main module. No top-level code runs.

What is transferred

State Mechanism SnapshotArtifact field
The context: module graph, closures, objects V8 blob blob
C++ callback addresses the blob refers to registerExternalReference() table externalReferences
env with its bindings, main module namespace context-level AddData, fixed indices blob (indices in snapshot.h)
JSG resource constructor templates isolate-level AddData, by position constructorTemplateIndices
Rust JSG resource templates isolate-level AddData, by TypeId rustResourceTemplates
Live JSG objects' native side tag only (stateless) or snapshotClone(); wrapper payload = slot index liveWrappables
References between those objects slot indices, relinked after restore liveWrappables[].references
Per-isolate bootstrap state one pinned object bootstrapStateIndex
Error.stackTraceLimit as the top level left it, deleted fail-closed properties pinned record, replayed after restore restoreTimeGlobalsIndex
process/Buffer values of the global scope one pinned object globalScopeStateIndex
Materialized module-registry entries pinned handles, seeded into the new registry moduleRecords
New module registry's resolutions pinned v8::Modules, rebuilt into the restored registry newModuleRegistryRecords
Global-scope event listeners pinned identity, callback and receiver; re-registered in C++ listenerRecords

Not transferred, by design: ctx.exports (built for the real Worker after the zygote has run), the context global's own ServiceWorkerGlobalScope (newContext() attaches a fresh one; its listener map is what listenerRecords restores, and its process/Buffer values what globalScopeStateIndex restores), and wrappers unreachable at CreateBlob().

FinalizationRegistry cleanups the top level leaves due run in the zygote (step 5 above) and their effects are part of the snapshot, as if a collection had happened right after the top level. A registered object reachable only through a C++ object's handle is a different case: it is not carried by the snapshot, and rather than run or drop its cleanup, the zygote fails.

Making a type transferable

A JSG resource type whose instance can be alive when top-level evaluation ends needs one of:

  • static constexpr bool jsgSnapshotStateless = true; when any fresh instance is interchangeable with any other (SubtleCrypto, Navigator, TextEncoder, ...). The snapshot records only the wrapper's tag; the worker started from it attaches an instance built by the type's Lock& constructor, or its default constructor. The type must be final (a static_assert enforces it), since a subclass would inherit the flag.
  • an override of Wrappable::snapshotClone() returning a deep, isolate-independent copy (Headers, URL, TextDecoder, FileSystemModule, Performance): no V8 handles, no IoContext, and no jsg::Ref members except to a fresh object the copy owns, which has never been wrapped and so belongs to no isolate (the Fetcher a WrappedBinding copy holds, or the LoopbackDurableObjectClass of a loopback namespace copy). It is called in the zygote on the live original, and once more per started worker on the stored copy.
  • for a Rust JSG resource, an impl of jsg::SnapshotTransfer (src/rust/jsg/snapshot.rs); see below. #[jsg_resource(snapshot_stateless)] (UrlUtil, DnsUtil, InspectorModule) generates one, with an empty snapshot, for a type that implements Default. Every Rust resource shares one CppHeapPointerTag, so a Rust type is never recorded by tag alone: it goes through the Rust-side Wrappable's snapshotClone().

A type that holds a jsg::Ref to another transferred object whose identity JavaScript can see also overrides Wrappable::snapshotReferences() and snapshotRelink(), as Crypto does for subtle: the restored instance is built afresh, and would otherwise refer to a new object rather than to the one behind the restored wrapper.

jsg::SnapshotTransfer covers both for a Rust type: snapshot() and restore() stand for the two snapshotClone() calls, through an isolate-independent Snapshot value, and snapshot_references() and snapshot_relink() for their C++ counterparts. A Worker started from the snapshot restores the Rust resource templates into its new Realm (restoreResourceTemplatesFromSnapshot()) before anything is wrapped, so that its new wrappers of a type and the restored ones share a template. relinkReferences() runs with the new context entered, for the Lock of snapshot_relink().

A type whose state lives on the context, and so does not exist yet when snapshotClone() runs for the started worker, also overrides Wrappable::snapshotContextReady(). The file system handles bind to the worker's VFS there, when the Worker constructor, having installed the VFS on the context, enters it and calls IsolateBase::notifySnapshotContextReady().

A jsg::Function the top level keeps (the function createRequire() returns, or the new module registry's import.meta.resolve, for example) transfers if its functor provides Impl clone() const; the V8 patch snapshot-Keep-functions-created-by-v8-Function-New-in-the-context-snapshot keeps functions created by v8::Function::New() in the context snapshot so that their wrapper goes through the same path.

Neither may throw: see the contracts on Object::jsgSnapshotStateless (jsg.h) and Wrappable::snapshotClone() (wrappable.h). A type with neither makes every Worker whose top level leaves such an object reachable fall back; dropUnreachableWrappables() names the type in the zygote's error (live wrapper types that neither set jsgSnapshotStateless nor implement snapshotClone() ...).

A binding type's snapshotClone() copies the binding's description (its channel number and names) and returns none when the object is bound to one IoContext instead (a live channel or an IoContext-bound factory, a stub minted per request) or has registered a connect override on the zygote's global scope (ctx.mapVirtualHost(), or reading an ExtendedFetcher's host), which the restored Worker's fresh global scope would lack. A Fetcher on a CrossContextOutgoingFactory, which is not tied to one IoContext, transfers if the factory copies itself (CrossContextOutgoingFactory::cloneForSnapshot(); none by default). An embedder's loopback Durable Object namespace in env transfers as its channel numbers and a copy of its ID factory, and a WrappedBinding with a copy of its Fetcher (Fetcher::cloneForSnapshot()). A subclass that does not override snapshotClone() fails the zygote rather than transferring as its base type.

Failures

Two outcomes are possible for a Worker that qualifies:

  • The Worker does not qualify. supportsStartupSnapshot() says no: an inspector, a module fallback service, a service-worker script, Wasm modules, Python, or a binding of a kind that cannot transfer (cryptoKey, memoryCache, wrapped, logged at INFO as startup snapshot skipped: a binding of this kind cannot be transferred), or a dynamically loaded Worker. No zygote runs.
  • The zygote fails. A KJ exception while compiling, evaluating, recording or serializing: a top-level exception or parse error (the Worker constructor throws before anything is recorded), an unclonable type reachable from the context such as an aborted AbortSignal, an RPC method taken off a stub (const { ping } = env.RPC) or a C++ promise continuation that is still pending, such as a C++ stream's pull() or cancel() awaiting a promise the top level left unsettled (jsg::Promise::thenImpl() and PromiseWrapper::wrap()/tryUnwrap() register their callbacks as external references while a snapshot is prepared, so that the serializer reports the continuation's opaque data instead of aborting on an unknown address), a restore-time global the top level redefined, a listener registered with a signal option, FinalizationRegistry cleanups that keep dooming new objects or that become due only once the zygote's objects are destroyed, ... Or an error reported to the zygote's ErrorReporter without an exception (a module that does not transpile, for example). factory_new_worker() logs it at INFO (startup snapshot skipped: the zygote Worker threw, with the exception's description, or ... failed to start, with the reported errors) and the Worker starts without a snapshot, evaluating its top level itself. This is the common case for a Worker the pipeline does not yet cover and costs one wasted evaluation.
  • The restore fails. A check in the START_FROM_SNAPSHOT path (an artifact slot never linked, a pinned module with no registry entry, a stateless constructor or snapshotClone() that throws) surfaces as the Worker's own startup failure; there is no second attempt without the snapshot. These checks guard invariants the zygote established, so a failure here is a bug in the pipeline rather than in the Worker.

V8 fatal errors (CHECK failures) abort the process and cannot be caught at either stage. The one known way for a Worker's top level to cause one, a duplicate of a property V8 adds on restore, is what the restore-time-globals handling exists to prevent.

A typed array that keeps its bytes off the V8 heap, over a zero-length ArrayBuffer that is not detached (new Uint8Array(new ArrayBuffer(0))), neither fails the zygote nor restores soundly: V8 serializes its backing store although it holds no bytes, and on restore copies the zero bytes to the null start of a zero-length allocation. That is undefined behavior, which UBSan aborts on. The zygote cannot detect it in every build: the internal-field serializer that would see the view is not called for ArrayBuffer views when V8 is built with v8_array_buffer_view_internal_field_count = 0. workerd's own modules avoid leaving such a view reachable at their top level (node:zlib's flush buffers are Buffer.alloc(0), which V8 keeps on its heap).

Console output

Under ConsoleMode::STDOUT the zygote's top-level console.* calls and runtime warnings do not reach stdout/stderr directly: handleLog() and Worker::Isolate::logWarning() hand them to deferConsoleLine() while isPreparingSnapshot(). When the snapshot is used, flushDeferredConsoleOutput() prints them in order; when the Worker falls back, they are dropped with the zygote's isolate and the Worker's own evaluation prints them. Either way each line appears once.

Debugging

  • The INFO log startup snapshot skipped names the Worker and the reason. workerd test prints INFO logs; workerd serve needs --verbose.
  • Unknown external reference from CreateBlob() means the context reaches a C++ callback whose address was not passed to registerExternalReference(). JSG registers its own callbacks; a new v8::Function::New/FunctionTemplate::New call site with a native callback needs to register it too.
  • Without multiple pointer-compression cages every isolate shares one read-only heap, built from the read-only snapshot of the isolate the group starts with. A zygote's read-only snapshot differs from the built-in one only in the SharedFunctionInfo id it records; the V8 patch Leave-the-SharedFunctionInfo-id-out-of-the-read-only-heap-checksum leaves that id out of the debug build's read-only-heap checksum and has a restored isolate continue past it, so that isolates of both kinds can share the heap.

Tests

Test Covers
src/workerd/jsg/snapshot-test.c++ restore-time globals: install, removal, rejection cases; live-wrappable round trip (stateless, cloned, dropped and untransferable types), dropUnreachableWrappables() and releaseSlotsWithChecks() findings; FinalizationRegistry cleanups settled before the record, rejected after it
src/rust/jsg-test/tests/startup_snapshot.rs Rust resources: state, shared templates, relinked references, and a type without SnapshotTransfer
src/workerd/api/tests/startup-snapshot-globals-*-test top-level code touching Error.stackTraceLimit, WebAssembly, Iterator, Temporal (with and without the flag), a frozen Date.prototype, deleted restore-time globals, the process/Buffer globals, and the lazy MessageChannel/MessagePort globals, read and unread at the top level
src/workerd/api/tests/startup-snapshot-listeners-test top-level addEventListener() across the snapshot: once, passive, an object listener's handleEvent, a replaced global addEventListener; signal fallback
src/workerd/api/tests/startup-snapshot-functions-test a top-level createRequire() function across the snapshot; an AsyncLocalStorage.bind() function falls back
src/workerd/api/tests/startup-snapshot-modules-test module identity across the snapshot (ESM, JSON, CJS, node:buffer)
src/workerd/api/tests/startup-snapshot-new-module-registry-test the same with the new module registry: re-importing modules the top level imported (also under a query or a fragment, and import() from inside those) and builtins, importing one it never loaded, and import.meta, including an import.meta.resolve the top level kept
src/workerd/api/tests/startup-snapshot-node-test Node.js API objects (AsyncLocalStorage, a root-frame AsyncResource, node:diagnostics_channel, and the Rust resources behind node:url and node:dns) across the snapshot; an AsyncLocalStorage with a default value and an AsyncResource created inside a frame fall back
src/workerd/api/tests/startup-snapshot-wrappables-test live objects (Headers, URL, TextEncoder, TextDecoder, crypto, navigator, performance, tracing spans started outside a request, file system handles to a /tmp file and to a bundle file, node:fs, bindings) and the identity of crypto, crypto.subtle and navigator, GC and a FinalizationRegistry cleanup across the snapshot; unclonable, URLSearchParams, a mid-stream TextDecoder, performance with a recorded entry and C++ streams left awaiting a pull() or cancel() fall back; a Worker on C++ streams otherwise transfers
src/workerd/api/tests/startup-snapshot-bindings-test bindings captured at module scope (service stub, Durable Object namespace and ID) used at request time; an RPC method taken off a stub, wrapped and cryptoKey fall back
src/workerd/server/tests/startup-snapshot-console console output printed once; no zygote under an inspector; a top level that throws gets no snapshot and its startup error is reported once

Each wd_test runs the pipeline in its @all-autogates variant and the plain Worker in the @ variant, and asserts the same observable behavior for both. A case Worker learns which path it took at request time from startedFromStartupSnapshot() on workerd:unsafe; asked at module scope, the answer would be the zygote's and be baked into the snapshot.