Skip to content

Correct Rust JSG's escapable scope docs and test its invariants - #8783

Merged
bartlomieju merged 2 commits into
mainfrom
biwanczuk/rust-jsg-escape-scope-docs
Nov 23, 2026
Merged

bartlomieju merged 2 commits into
mainfrom
biwanczuk/rust-jsg-escape-scope-docs

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Missed in #8725

The docs for Lock::with_escapable_handle_scope() compare it to a C++
js.runInHandleScope() callback, but jsg::Lock has no such function;
the C++ counterpart is js.withinHandleScope(). This fixes the name in
scope.rs, the jsg README and src/rust/AGENTS.md.

EscapableScope::escape() consumes its scope because a
v8::EscapableHandleScope has a single escape slot. V8 checks a second
escape only in debug builds; in a release build the second escape
overwrites the slot and so retargets the first escaped handle. The
SAFETY comment and the type's docs now say so, since this is what
makes the type's lack of Clone a soundness requirement rather than a
convenience. The escape-once compile test now also checks that the
scope can't be cloned, so deriving Clone on it fails a test.

escape() also accepts a handle of a scope nested in the escapable
one, because EscapableScope is covariant in its inner lifetime. That's
sound: V8 copies the value into a slot it reserved in the enclosing
scope when the escapable scope opened. The docs of escape() now state
it, and a new test escapes a handle created in a nested scope and
reads it after both scopes close.
@bartlomieju
bartlomieju marked this pull request as ready for review November 21, 2026 08:51
@bartlomieju
bartlomieju requested review from a team as code owners November 21, 2026 08:51
@bartlomieju
bartlomieju enabled auto-merge (squash) November 21, 2026 08:52

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 0babb64f.

This review, of 63aba2d5

💬 Approved with comments — The doc fix and the new tests are correct. One question remains about whether V8 checks a second escape only in debug builds, which the new text states as fact.

Findings: 1 question

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: ❓ 1 question

The rename and the new tests are correct; one question about the new statement on V8's release-build behaviour.

Recommendation: 💬 Optional Nits

  • [QUESTION] Please check against the pinned V8 version that a second EscapableHandleScope::Escape() is checked only in debug builds and overwrites the first escaped handle in release builds. Older V8 guarded this with an ApiCheck that stays on in release builds. The same claim appears in the SAFETY comment, the compile-test header and the README. (src/rust/jsg/scope.rs:157)
View full review

The rename to js.withinHandleScope() matches src/workerd/jsg/jsg.h. The nested-scope escape test covers the new doc statement that a handle from a nested scope can be escaped. The cloned compile-fail case checks that EscapableScope has no Clone.

The same claim now appears in four places: the EscapableScope doc, the SAFETY comment in escape, the compile-test header and the README. It says V8 only debug-checks a second Escape() and that a release build silently overwrites the first escaped handle. Older V8 guarded the escape slot with Utils::ApiCheck(..., "Escape value set twice"), and that check stays on in release builds. The V8 sources aren't in this snapshot, so I couldn't check which behaviour the pinned version has.


✅ All other checks passed

📝 Documentation: ✅ LGTM

The doc changes in scope.rs, the jsg README and src/rust/AGENTS.md now name the right C++ function.


Reviewed commit: 63aba2d5

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Approved with comments — The doc fix and the new tests are correct. One question remains about whether V8 checks a second escape only in debug builds, which the new text states as fact.

Findings: 1 question

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: ❓ 1 question

The rename and the new tests are correct; one question about the new statement on V8's release-build behaviour.

Recommendation: 💬 Optional Nits

  • [QUESTION] Please check against the pinned V8 version that a second EscapableHandleScope::Escape() is checked only in debug builds and overwrites the first escaped handle in release builds. Older V8 guarded this with an ApiCheck that stays on in release builds. The same claim appears in the SAFETY comment, the compile-test header and the README. (src/rust/jsg/scope.rs:157)
View full review

The rename to js.withinHandleScope() matches src/workerd/jsg/jsg.h. The nested-scope escape test covers the new doc statement that a handle from a nested scope can be escaped. The cloned compile-fail case checks that EscapableScope has no Clone.

The same claim now appears in four places: the EscapableScope doc, the SAFETY comment in escape, the compile-test header and the README. It says V8 only debug-checks a second Escape() and that a release build silently overwrites the first escaped handle. Older V8 guarded the escape slot with Utils::ApiCheck(..., "Escape value set twice"), and that check stays on in release builds. The V8 sources aren't in this snapshot, so I couldn't check which behaviour the pinned version has.


✅ All other checks passed

📝 Documentation: ✅ LGTM

The doc changes in scope.rs, the jsg README and src/rust/AGENTS.md now name the right C++ function.


Reviewed commit: 0babb64f — same changes as 63aba2d5, so this is that commit's review (no new review was run; line numbers are as of 63aba2d5)

@bartlomieju
bartlomieju merged commit 7c097d9 into main Nov 23, 2026
36 of 37 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-jsg-escape-scope-docs branch November 23, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants