← Back to Pull requests

Require the isolate lock for jsg::Rc, jsg::Weak and jsg_cpp::Ref #8319

Closed
unknown closed this pull request without merging on Nov 11, 2026 22:08 UTC
View on GitHub
unknown unknown commented on Nov 09, 2026 22:44 UTC
Author

Only the isolate lock protects a resource's state: the reference counts of a jsg::Rc and its Wrappable, and the resource's Cells and RefCells. Today nothing enforces that. A jsg::Rc can be dereferenced and cloned without the lock, so a thread that holds one can read the resource or change its counts while another thread holds the lock. That's a data race, which is undefined behavior, and today safe code can cause it.

This PR makes the lock the only way to reach that state.

What changes

  • jsg::Rc loses Deref and Clone. Use get(lock), clone(lock) and downgrade(lock) instead. Weak::upgrade, Weak::clone, Weak::is_alive, to_js and attach_to_this take a Lock too.

  • jsg_cpp::Ref gets the same API, via a new jsg::LockGuarded<T>. Its drop no longer relies on C++'s deferral. Today, two refs to an unwrapped object dropped on two threads race on its count.

  • v8::WrappableRc and the jsg_cpp bridge become private to jsg, since they change counts without the lock.

  • ~IsolateBase now calls the Rust dispose hook just before Dispose() as well as after. C++ objects held by resources are destroyed before Dispose(); a ref kept past teardown is dropped afterwards, which #8315 makes safe.
  • Why this is safe

  • The compiler enforces that you hold a lock: Lock is neither Send nor Sync, and every operation except drop takes one.

  • A runtime check, in every build, enforces that it's the right lock. Each operation panics if the lock belongs to another isolate or the Realm is gone. Holding one isolate's lock doesn't exclude anyone from another's, so this check is part of the safety argument, not a debugging aid.

  • GC and detaching visits only change a reference under its own isolate's lock. A GC visit from another isolate leaves the reference alone: it leaks rather than racing.

  • Drops without the lock are queued without touching a count (#8111).

  • get() ties the &R to the lock's lifetime, so even a leaked Rc can't hand its resource to a 'static future.
  • As a result, jsg::Rc, jsg::Weak and jsg_cpp::Ref are Send for a Send resource: on a thread without the lock, all you can do with one is drop it. The argument is written up as safety rule 5 in src/rust/jsg/README.md.

    Tests

  • Compile tests reject deref, clone, downgrade and upgrade without a lock, and a &R from get() captured in a 'static future. They also check that WrappableRc and the jsg_cpp bridge are private.

  • A compile test accepts a jsg::Rc / jsg::Weak dropped on another thread.

  • jsg-tests:

  • - every lock-taking operation panics under another isolate's lock;
    - detaching a reference from another isolate panics, and another isolate's GC leaves it alone;
    - concurrent unlocked drops wait for the lock;
    - teardown destroys a held jsg_cpp::Ref's C++ object before Dispose(), and one kept past teardown can still be dropped.

    Diff: 350 lines (+262 / -88) across 4 files Reviewer: nathanwhit Review duration: 2844m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 350 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8319"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.8% · Safe to merge
    16.8%