Repository navigation
Carry Rust JSG resources through the startup snapshot - #8521
Conversation
There was a problem hiding this comment.
Superseded by the review of
9b543dda.
This review, of 8d4628d2
💬 Approved with comments — No blocking or production-impact findings; one optional suggestion to add Send to the Snapshot bound and one documentation wording note.
Findings: 1 info · 1 suggestion (2 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 suggestion
Sound change; the Snapshot associated type requires Sync but not Send, although its docs say both.
Recommendation: 💬 Optional Nits
- [SUGGESTION]
SnapshotTransfer::Snapshotis bounded bySyncbut notSend, while the docs say it is both (src/rust/jsg/snapshot.rs:55)
View full review
src/rust/jsg/snapshot.rs:55: the trait docs at lines 37-39 say the stored snapshot "isSendandSync", but the bound isHoldsNoGcReferences + Sync + 'static, andSyncdoes not implySend. The stored value is created on the zygote thread and shared by every Worker restored from the snapshot. It is dropped on whichever thread releases the last reference, so in practice it needsSendtoo. AddingSendwould make the bound match the documented contract. The expected diagnostics insrc/rust/jsg-compile-tests/snapshot-holds-no-gc-references.rsmay then need updating.
📝 Documentation: 💬 1 info
One README sentence is ambiguous about who calls restore().
Recommendation: 💬 Optional Nits
- [INFO] Clarify that each Worker started from the snapshot, not each thread, calls
restore()(src/rust/jsg/README.md:729)
View full review
src/rust/jsg/README.md:729: "Each of those" reads as if it refers to the threads named just before it, so it suggests that each thread callsrestore(). The call happens once per Worker.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found in this change.
📖 AGENTS.md: ✅ LGTM
No issues found on the changed lines.
⚡ Performance: ✅ LGTM
No performance issues found in this change.
Reviewed commit: 8d4628d2
8d4628d to
9b543dd
Compare
There was a problem hiding this comment.
Superseded by the review of
57caf083.
This review, of 9b543dda
💬 Approved with comments — No blocking findings and no production impact. One WARNING: the SnapshotTransfer doc comment says a panic makes the Worker fall back or fail to start, but the code aborts the process.
Findings: 1 warning (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #8521.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Code Quality] `SnapshotTransfer` docs say a panic falls back to a plain start or fails one Worker, but the FFI entry points have no `catch_unwind`, so a panic aborts the process.
- **File:** src/rust/jsg/snapshot.rs:50
📋 Code Quality: ⚠️ 1 warning
The earlier Send bound note is fixed. The SnapshotTransfer doc comment describes a panic outcome the code does not produce.
Recommendation: 📝 Minor Comments
- [WARNING]
SnapshotTransferdocs say a panic falls back to a plain start or fails one Worker, but the FFI entry points have nocatch_unwind, so a panic aborts the process. (src/rust/jsg/snapshot.rs:50)
View full review
The earlier note about the Send bound is fixed: src/rust/jsg/snapshot.rs:55 now reads HoldsNoGcReferences + Send + Sync + 'static.
What the doc comment says a panic does is not what happens (src/rust/jsg/snapshot.rs:50-52)
The doc comment says a panic in the zygote makes the Worker start without the snapshot. It says a panic in a restored Worker makes that Worker fail to start.
The code does neither:
wrappable_invoke_snapshot_clone,wrappable_invoke_snapshot_referencesandwrappable_invoke_snapshot_relink(src/rust/jsg/v8.rs:4987-5016) are plain cxxextern "Rust"functions with nocatch_unwind.restore()also runs throughwrappable_invoke_snapshot_clone, viasnapshot_copy(src/rust/jsg/snapshot.rs:180).
So a panic in snapshot(), restore(), snapshot_references() or snapshot_relink() reaches the cxx shim and aborts the process.
No resource in this pull request panics.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found in this change.
📖 AGENTS.md: ✅ LGTM
No issues found on the changed lines.
⚡ Performance: ✅ LGTM
The new work runs once when the snapshot is built and once per Worker start, not on any request path. Pinning and restoring the templates takes time linear in the number of templates.
📝 Documentation: ✅ LGTM
The README now says that each Worker started from the snapshot calls restore() (src/rust/jsg/README.md:736).
Reviewed commit: 9b543dda
9b543dd to
57caf08
Compare
There was a problem hiding this comment.
Superseded by the review of
f73465ce.
This review, of 57caf083
✅ Approved — The earlier WARNING about the panic behaviour in the SnapshotTransfer docs is fixed, and the change to snapshot.rs raises no new issues.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found in this change.
📖 AGENTS.md: ✅ LGTM
No issues found on the changed lines.
📋 Code Quality: ✅ LGTM
src/rust/jsg/snapshot.rs:48-50 now says that none of the SnapshotTransfer methods may panic and that a panic aborts the process. That matches wrappable_invoke_snapshot_clone, wrappable_invoke_snapshot_references and wrappable_invoke_snapshot_relink in src/rust/jsg/v8.rs, which are cxx extern "Rust" functions with no catch_unwind.
⚡ Performance: ✅ LGTM
The new work in src/rust/jsg/snapshot.rs runs once when the zygote builds the snapshot and once per Worker started from it, not on any request path. Its cost is linear in the number of resources carried.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 57caf083
A Worker's top level that imports `node:url`, `node:dns` or `node:fs`, or touches `process.stdout`, leaves a Rust JSG resource such as `UrlUtil` reachable. Rust resources had no `snapshotClone()`, so the zygote rejected the snapshot and these Workers always started without one. The zygote also dropped the Rust resource templates before building the blob, so a carried resource would have had a different template from the new wrappers of its type. In this patch: - `jsg::SnapshotTransfer` is the Rust counterpart of `snapshotClone()`, `snapshotReferences()` and `snapshotRelink()`. The zygote stores an isolate-independent `Snapshot` of the resource, and each Worker started from the snapshot rebuilds the resource from it with `restore()` and takes back the restored resources it refers to. `UrlUtil`, `DnsUtil` and `InspectorModule` implement it. - The zygote pins the Rust resource templates into the snapshot by `TypeId`, and a restored isolate fills its new `Realm` with them, so restored and new wrappers share their class. - `startup-snapshot-node-test` covers `node:url` and `node:dns`, and the `node:fs` and `process.stdout` cases now start from the snapshot. The node test reads `startedFromStartupSnapshot()` at request time, since its `typeof env.SELF` check was false whether or not the Worker was restored.
57caf08 to
f73465c
Compare
There was a problem hiding this comment.
Superseded by the review of
d5cfdf1b.
This review, of f73465ce
✅ Approved — No issues found. The only change since the last approved review is a compile-fail test, and it checks the intended HoldsNoGcReferences bound.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The only change since the last review is a compile-fail test, and it has no security impact.
📖 AGENTS.md: ✅ LGTM
No issues found on the changed lines.
📋 Code Quality: ✅ LGTM
src/rust/jsg-compile-tests/snapshot-holds-no-gc-references.rs expects one E0277 on the type Snapshot line, where rustc reports the unmet HoldsNoGcReferences bound. MaybeHoldsReferences is Send and Sync, so the test fails for the intended reason. The panic wording fixed in src/rust/jsg/snapshot.rs is still correct.
⚡ Performance: ✅ LGTM
The changed file is a compile-time fixture and never runs. The snapshot work runs once when the zygote builds the snapshot and once per restored Worker. It is not on the request path.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: f73465ce
There was a problem hiding this comment.
Superseded by the review of
cb73f6de.
This review, of d5cfdf1b
✅ Approved — No issues found. The only change since the last approved review is a compile-fail test, and it checks the intended HoldsNoGcReferences bound.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The only change since the last review is a compile-fail test, and it has no security impact.
📖 AGENTS.md: ✅ LGTM
No issues found on the changed lines.
📋 Code Quality: ✅ LGTM
src/rust/jsg-compile-tests/snapshot-holds-no-gc-references.rs expects one E0277 on the type Snapshot line, where rustc reports the unmet HoldsNoGcReferences bound. MaybeHoldsReferences is Send and Sync, so the test fails for the intended reason. The panic wording fixed in src/rust/jsg/snapshot.rs is still correct.
⚡ Performance: ✅ LGTM
The changed file is a compile-time fixture and never runs. The snapshot work runs once when the zygote builds the snapshot and once per restored Worker. It is not on the request path.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: d5cfdf1b — same changes as f73465ce, so this is that commit's review (no new review was run; line numbers are as of f73465ce)
|
Looks ok to me but I'm not yet 100% up to speed on the snapshot work (still reviewing it all myself) so let's get either @dcarney-cf or @erikcorry to stamp? |
guybedford
left a comment
There was a problem hiding this comment.
Approving, conditional on making ErasedSnapshotTransfer::snapshot_relink an unsafe fn (inline).
ErasedSnapshotTransfer::snapshot_relink reads each entry of `restored` as the address of a Wrappable, so safe code could call it with any address and crash the process. In this patch it is an `unsafe fn` whose caller guarantees each entry is 0 or a live Wrappable of the lock's isolate, and a compile test checks that safe code can't call it.
There was a problem hiding this comment.
Superseded by the review of
1f5fafa5.
This review, of cb73f6de
✅ Approved — No issues found. The only change since the last approved review makes snapshot_relink an unsafe fn and adds a compile-fail test for it, as the approver asked.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Safe code outside the crate can no longer reach the raw Wrappable dereference in RestoredReferences::next. The unsafe API problem raised on the pull request is fixed.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
snapshot_relink is now an unsafe fn with a # Safety section, and its only caller in v8.rs calls it inside an unsafe block with a justification. The new compile-fail test expects E0133, the error for calling an unsafe fn outside unsafe.
⚡ Performance: ✅ LGTM
The snapshot work runs once in the zygote and once per restored Worker, so it is not on the request path.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: cb73f6de
…-audit-rjsg-autogates # Conflicts: # src/rust/jsg/ffi.h
There was a problem hiding this comment.
Superseded by the review of
2677ef5e.
This review, of 1f5fafa5
✅ Approved — No issues found. The only changes since the last approved review are the merge from main in ffi.h and v8.rs, and the unsafe fn change the approver asked for is still in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Safe code still cannot reach the raw Wrappable dereference, because snapshot_relink is an unsafe fn with a # Safety contract.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
No issues in src/rust/jsg/ffi.h or src/rust/jsg/v8.rs:
- The three
Wrappablesnapshot overrides inffi.hmatch the bridge declarations inv8.rs. - The
impl Vec<Global> {}workaround could be removed becauserealm_take_resource_templatesnow returnsVec<ResourceTemplate>. - The
transmutecalls insplit_type_idandjoin_type_idare kept in one place, withSAFETYcomments. wrappable_invoke_snapshot_relinkcalls theunsafe fn snapshot_relinkinside anunsafeblock with a justification comment.
⚡ Performance: ✅ LGTM
The snapshot work runs once in the zygote and once per restored Worker, so none of it is on the request path. The latest changes add no cost to hot paths.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 1f5fafa5
…-audit-rjsg-autogates # Conflicts: # docs/startup-snapshot.md # src/rust/jsg-macros/resource.rs # src/rust/jsg/ffi.c++ # src/rust/jsg/lib.rs # src/workerd/api/tests/startup-snapshot-globals-test.js # src/workerd/api/tests/startup-snapshot-node-case-test.js # src/workerd/api/tests/startup-snapshot-node-test.js # src/workerd/api/tests/startup-snapshot-node-test.wd-test # src/workerd/api/tests/startup-snapshot-wrappables-test.js # src/workerd/jsg/setup.h
There was a problem hiding this comment.
Superseded by the review of
2c53a30b.
This review, of 2677ef5e
✅ Approved — No issues found. The changes merged from main keep the unsafe fn contract on snapshot_relink and leave the template restore and relink paths sound.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Safe code still cannot reach the raw
Wrappabledereference, becausesnapshot_relinkis anunsafe fnwith a# Safetycontract. Snapshotis bounded byHoldsNoGcReferences + Send + Sync + 'static, so it cannot hold a GC reference or av8::Global.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
No correctness, test-coverage or convention problems.
snapshot_relinkis still anunsafe fn, and a compile-fail test guards it.- Template restore runs right after
realm_create, andrestore_templatesasserts that the template cache is empty. - The new
startup_snapshot.rstests cover state, shared templates, relinked references and a type withoutSnapshotTransfer. Therust-modulesWorker instartup-snapshot-node-testcoversnode:urlandnode:dns.
⚡ Performance: ✅ LGTM
The snapshot work runs once in the zygote and once per restored Worker, so none of it is on the request path. Restoring templates costs time linear in the number of Rust resource types.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 2677ef5e
There was a problem hiding this comment.
Superseded by the review of
cf5eed76.
This review, of 2c53a30b
✅ Approved — No issues found. The only change since the last review formats the case list in startup-snapshot-node-test.js. snapshot_relink is still an unsafe fn, as requested in the approving review.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Safe code cannot reach the raw
Wrappabledereference.snapshot_relinkis anunsafe fn, and it is called only from C++ with Wrappables from the same isolate. - The snapshot data is produced inside the process and never comes from untrusted input.
Snapshotis bounded byHoldsNoGcReferences, so it cannot hold a GC reference.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
The only change since the last review formats the case list in startup-snapshot-node-test.js.
ErasedSnapshotTransfer::snapshot_relinkis still anunsafe fnwith a# Safetysection.- The compile-fail test
erased-snapshot-relink-is-unsafe.rsguards that contract. The compile-tests target picks it up throughglob(["*.rs"]). - Templates are restored right after
realm_create. Pinning and restoring pair each template with itsTypeIdby slot index, so the two sides match. - No references to the removed
snapshot_recreateAPI remain. - The tests cover the change:
startup_snapshot.rscovers state, shared templates, relinked references, a type withoutSnapshotTransfer, and an unreachable resource.startup-snapshot-node-testcoversnode:urlandnode:dnsin a Worker started from the snapshot.
⚡ Performance: ✅ LGTM
The snapshot work runs once in the zygote and once per Worker started from the snapshot, so none of it is on the request path. Restoring templates takes time linear in the number of Rust resource types.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 2c53a30b
There was a problem hiding this comment.
✅ Approved — No issues found. The only change since the last review formats the case list in startup-snapshot-node-test.js. snapshot_relink is still an unsafe fn, as requested in the approving review.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Safe code cannot reach the raw
Wrappabledereference.snapshot_relinkis anunsafe fn, and it is called only from C++ with Wrappables from the same isolate. - The snapshot data is produced inside the process and never comes from untrusted input.
Snapshotis bounded byHoldsNoGcReferences, so it cannot hold a GC reference.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
The only change since the last review formats the case list in startup-snapshot-node-test.js.
ErasedSnapshotTransfer::snapshot_relinkis still anunsafe fnwith a# Safetysection.- The compile-fail test
erased-snapshot-relink-is-unsafe.rsguards that contract. The compile-tests target picks it up throughglob(["*.rs"]). - Templates are restored right after
realm_create. Pinning and restoring pair each template with itsTypeIdby slot index, so the two sides match. - No references to the removed
snapshot_recreateAPI remain. - The tests cover the change:
startup_snapshot.rscovers state, shared templates, relinked references, a type withoutSnapshotTransfer, and an unreachable resource.startup-snapshot-node-testcoversnode:urlandnode:dnsin a Worker started from the snapshot.
⚡ Performance: ✅ LGTM
The snapshot work runs once in the zygote and once per Worker started from the snapshot, so none of it is on the request path. Restoring templates takes time linear in the number of Rust resource types.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: cf5eed76 — same changes as 2c53a30b, so this is that commit's review (no new review was run; line numbers are as of 2c53a30b)
A Rust JSG resource could cross the startup snapshot only as a stateless type (
#[jsg_resource(snapshot_stateless)]), recreated through its own clone hook. A resource with state, or one holding ajsg::Rcto another resource whose identity JavaScript can see, had no way through, so a Worker whose top level kept one started without the snapshot. The zygote also dropped the Rust resource templates before building the blob, so a restored resource had a different template from the new wrappers of its type.In this patch:
jsg::SnapshotTransferis the Rust counterpart ofsnapshotClone(),snapshotReferences()andsnapshotRelink(). The zygote stores an isolate-independentSnapshotof the resource, and each Worker started from the snapshot rebuilds the resource from it withrestore()and takes back the restored resources it refers to.snapshot_statelessnow generates aSnapshotTransferwith an empty snapshot, so stateless and stateful types go through onesnapshotClone()path, andGarbageCollected::snapshot_recreateis gone.TypeId, and a restored isolate fills its newRealmwith them, so restored and new wrappers share their class.snapshotRelink()runs with the new context entered, as a RustLockneeds one.SnapshotTransfer, andstartup-snapshot-node-testcoversnode:urlandnode:dnsin a Worker started from the snapshot.