Repository navigation
Commit 593054a
committed
Require the isolate lock for jsg::Rc, jsg::Weak and jsg_cpp::Ref
Rust JSG shares state with no synchronization of its own: the reference counts of a jsg::Rc and
its Wrappable, and the Cells and RefCells of resources. Only the isolate lock keeps two threads
from using that state at once, which would be a data race. Today a jsg::Rc can be dereferenced
and cloned without the lock, so a thread that holds one but not the lock can read the resource or
change its counts while the lock's holder does. This change makes every reference reach that
state only under its isolate's lock.
- jsg::Rc and jsg::Weak touch their counts and their resource only under the lock of the
resource's isolate, which makes them Send, for R: Send:
- Rc loses Deref and Clone for get(lock), clone(lock) and downgrade(lock), and Weak::upgrade,
Weak::clone, Weak::is_alive and Rc::attach_to_this take a Lock too. Each asserts, in every
build, that the lock is of the resource's isolate and that its Realm is alive: holding one
isolate's lock excludes no thread from another's. Lock is neither Send nor Sync. get()
bounds the &R by the lock's brand 's, so even a leaked Rc can't lend its resource to a
'static future.
- GC and detaching visits change a jsg::Rc only under its isolate's lock. A detaching visit
(jsg::Cell::take, say) under another isolate's lock panics; a GC visit on behalf of another
isolate leaves it as it is, so a reference that one isolate's resource holds to another's
leaks rather than racing, as TracedGlobal's visit already does. Debug builds assert against
such a visit.
- A drop without the lock is queued without touching a count (#8111). A drop under the lock
releases at once, now also while the isolate is torn down, until it is about to be
disposed: a jsg::Rc that teardown dropped used to be kept until after Dispose, and with it
any resource reachable only through it.
- Weak loses its unused GarbageCollected impl, whose memory_name upgraded it without a lock.
- jsg_cpp::Ref gets the same treatment through a new jsg::LockGuarded<T>: a value that only its
isolate's lock guards, which it lends only under that lock (get, and get_for_visit for GC and
detaching visits, which follow jsg::Rc's rules) and drops as a jsg::Rc drops its parts. Its
constructor is unsafe: the value must be bound to no thread but through the lock. Ref's Deref
becomes get(lock) and its Clone clone(lock), and to_js and as_cpp check the lock too. A heap
snapshot reports it through LockGuarded::get_for_memory_info, which, as for a jsg::Rc, lends
the value only to a snapshot of its own isolate, which holds that isolate's lock. Its drop
no longer relies on C++'s deferral, which destroys an object that has no wrapper at once, with
or without the lock: two references to such an object, dropped on two threads, raced on its
count. While the isolate is torn down, a drop under the lock still drops the value then, until
the isolate is about to be disposed, so the C++ objects that the resources teardown drops hold
are destroyed before Dispose, as before; after it, a destructor that resets a raw v8::Global
would use the freed isolate. ~IsolateBase now also calls its dispose hook just before Dispose,
after which Rust no longer asks V8 whether a thread holds the lock. A jsg_cpp::Ref kept past
teardown is dropped after Dispose, which the parent commit ("Unbind every Wrappable from its
isolate at teardown") makes safe.
- v8::WrappableRc becomes private to jsg, since its Clone and Drop change the Wrappable's count
without the lock, and so does the jsg_cpp bridge, whose functions clone and detach a C++
reference without one. The macros' receiver helpers move to jsg::v8::resolve_receiver and
resolve_method_receiver.
The argument also rests on GC running on the thread that holds the lock (--single-threaded-gc),
and on nothing releasing the lock while a Lock exists (workerd has no v8::Unlocker).
await_io keeps its Send bounds. A future may now hold a jsg::Rc, but only to drop it.
Resource state itself can still be anything, std::rc::Rc included; the next commit requires it
to be Send.
Tests: compile tests that reject deref, clone, downgrade and upgrade without a lock, and a &R
from get() captured in a 'static future, and one that accepts a resource with a
jsg::RefCell<Vec<jsg::Rc<_>>> field whose jsg::Rc and jsg::Weak are dropped on another thread.
jsg-tests check that each lock-taking operation, jsg_cpp::Ref's included, panics under another
isolate's lock, that detaching a jsg::Rc or jsg_cpp::Ref of another isolate panics, that another
isolate's GC leaves a jsg::Rc alone (in release builds), and that references to an object with
no wrapper, dropped on two threads without the lock, wait for it. They also check that teardown
destroys the C++ object of a jsg_cpp::Ref that a resource holds before the isolate is disposed,
including one reached through a jsg::Rc, that a jsg::Rc dropped under the lock during teardown
is released at once, and that a jsg_cpp::Ref kept past teardown can still be dropped. Compile tests check that WrappableRc and
the jsg_cpp bridge are private. Tests that used a resource after leaving its lock scope now read
it inside one.
src/rust/jsg/README.md gains safety rule 5, which states the argument, and src/rust/AGENTS.md
describes the new API.1 parent cda006c commit 593054a
62 files changed
Lines changed: 1684 additions & 679 deletions
File tree
- docs/reference
- src
- rust
- api
- jsg-bench
- jsg-compile-tests
- jsg-cpp
- jsg-macros
- jsg-test
- tests
- jsg
- workerd
- io/io-context
- compile-tests
- jsg
- types/rtti-fixture
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
| 97 | + | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| |||
247 | 248 | | |
248 | 249 | | |
249 | 250 | | |
250 | | - | |
251 | | - | |
| 251 | + | |
| 252 | + | |
252 | 253 | | |
253 | 254 | | |
254 | 255 | | |
255 | | - | |
256 | | - | |
| 256 | + | |
| 257 | + | |
257 | 258 | | |
258 | 259 | | |
259 | 260 | | |
| |||
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
326 | | - | |
| 327 | + | |
327 | 328 | | |
328 | 329 | | |
329 | | - | |
| 330 | + | |
330 | 331 | | |
331 | 332 | | |
332 | 333 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | | - | |
30 | | - | |
| 30 | + | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | | - | |
40 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | | - | |
50 | | - | |
| 51 | + | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| |||
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
59 | | - | |
60 | | - | |
| 61 | + | |
| 62 | + | |
61 | 63 | | |
62 | 64 | | |
63 | 65 | | |
64 | | - | |
65 | | - | |
| 66 | + | |
| 67 | + | |
66 | 68 | | |
67 | 69 | | |
68 | 70 | | |
69 | | - | |
70 | | - | |
| 71 | + | |
| 72 | + | |
71 | 73 | | |
72 | 74 | | |
73 | 75 | | |
74 | | - | |
75 | | - | |
| 76 | + | |
| 77 | + | |
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
85 | 88 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
91 | | - | |
92 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
93 | 93 | | |
94 | | - | |
95 | | - | |
| 94 | + | |
| 95 | + | |
96 | 96 | | |
0 commit comments