Repository navigation
Commit f0f60fd
authored
[JSC] Atomics.wait / memory.atomic.wait: wake up for a termination requested from another thread (#432)
* [JSC] DeferTermination: preserve a TerminationException that is pending outside any VMEntryScope
VMTraps::deferTerminationSlow() suspends a pending TerminationException and
relies on VM::hasTerminationRequest() staying set so that
undoDeferTermination() re-throws it. That holds while a VMEntryScope is on
the stack, but the outermost VMEntryScope's exit resets the request. A client
that deliberately leaves the TerminationException pending past that exit —
so that nothing re-enters the VM until it tears it down — can still reach a
DeferTermination scope from host code (a LazyProperty / LazyClassStructure
initializer, JSObject::putDirect...) before then. In debug that failed
ASSERT(vm.hasTerminationRequest()); in release the exception was cleared,
undoDeferTermination() saw no request and never re-threw it, so the pending
termination was silently dropped (and m_suspendedTerminationException left
stale).
When the request has already been reset, re-establish it for the duration of
the deferral and reset it again once the exception has been re-thrown, so the
scope is transparent in that state. Behaviour with a VMEntryScope on the
stack, or without a pending TerminationException, is unchanged.
* [JSC] Atomics.wait / memory.atomic.wait: wake up for a termination requested from another thread
WaiterListManager::waitSyncImpl() parks the thread until it is dequeued by a
notify, times out, or `vm.hasTerminationRequest()`. Since 305643@main
("Fix a race condition with the setting of VM::m_hasTerminationRequest")
that request is only established by the mutator thread itself when it handles
the NeedTermination trap — which it cannot do while parked — and
VM::notifyNeedTermination() from another thread merely fires the trap. So a
worker blocked in Atomics.wait() with no timeout could no longer be stopped:
VMTraps woke its condition, the predicate was still false, and it went back to
sleep for good (worker.terminate() never completing; the sync-over-async
worker pools that park in Atomics.wait leak their thread).
- The wait predicate also honours a pending NeedTermination trap, and on that
exit waitSyncImpl() handles the trap itself (establishing the request and
throwing the TerminationException) before returning Terminated, so both
callers just propagate.
- The wake-up moves from requestThreadStopIfNeeded() — which only runs when
no other async event already has a stop requested — to fireTrap() for
NeedTermination, and is delivered by Waiter::notifyOfTerminationRequest()
under the lock of the list the waiter is parked on, so it cannot fall
between the waiter's predicate check and its park. That matters where traps
are polled rather than signalled (no SignalSender re-notifying every
millisecond), e.g. on Windows.
* Review follow-ups: drop the entryScope assertion; establish the termination in waitSyncImpl without handleTraps()
- deferTerminationSlow(): re-establishing the request does not depend on
being outside a VMEntryScope, so don't assert it.
- waitSyncImpl(): consume the NeedTermination trap and establish/throw the
termination directly instead of calling VMTraps::handleTraps(), whose trap
check also walks vm.topCallFrame to jettison optimized code — not needed to
throw here, and best avoided from wasm's memory.atomic.wait operation.
* Address review: survive a VM entry inside the deferral; don't interrupt a wait while termination is deferred
DeferTermination (commit 1):
- undoDeferTermination() takes its slow path when a TerminationException
was suspended even if the request has meanwhile been reset - which
happens when the deferred code itself enters the VM (a LazyProperty
initializer may) and that outermost VMEntryScope exits. The slow path
re-establishes the request for the re-throw if needed and then leaves it
as it was before the deferral, so neither the exception nor the flags go
stale in that case.
Atomics.wait (commit 2):
- While termination is being deferred, a termination request does not cut
the wait short (it takes effect when the deferral ends); previously that
corner returned Terminated without an exception thrown, handing
Atomics.wait an empty value / wasm a bogus trap. The tail now always
throws and both callers assert a pending exception.
- Waiter::setParkedList() accessor instead of befriending
WaiterListManager; CONCURRENT_SAFE on the notifySyncWaiterOfTermination
definition; comment fixes (handleTraps() jettisons trap-breakpointed code
blocks rather than walking the stack; ASCII).
* Drop the DeferTermination change; keep only the Atomics.wait termination wake-up
The state the DeferTermination change handled (TerminationException left
pending outside any VMEntryScope while host code keeps initialising lazy
structures) is one WebKit's own clients never produce: WebCore either
clears the exception at the JS entry (NakedPtr<Exception> calls) or, in
WorkerDedicatedRunLoop::Task::performTask, leaves it pending but then runs
no further non-cleanup task. It is the embedder's job to keep
"TerminationException pending => hasTerminationRequest()" true if it keeps
the exception; Bun will do that on its side. Restores deferTerminationSlow(),
undoDeferTerminationSlow(), undoDeferTermination() and VMTraps' members to
origin/main.1 parent e2f13c6 commit f0f60fd
6 files changed
Lines changed: 81 additions & 10 deletions
File tree
- Source/JavaScriptCore
- runtime
- wasm
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
470 | 470 | | |
471 | 471 | | |
472 | 472 | | |
473 | | - | |
| 473 | + | |
474 | 474 | | |
475 | 475 | | |
476 | 476 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
330 | | - | |
| 330 | + | |
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
| |||
398 | 398 | | |
399 | 399 | | |
400 | 400 | | |
401 | | - | |
402 | 401 | | |
403 | 402 | | |
404 | 403 | | |
| |||
409 | 408 | | |
410 | 409 | | |
411 | 410 | | |
412 | | - | |
| 411 | + | |
413 | 412 | | |
414 | 413 | | |
415 | 414 | | |
416 | 415 | | |
417 | 416 | | |
418 | 417 | | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | 418 | | |
423 | 419 | | |
424 | 420 | | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
425 | 426 | | |
426 | 427 | | |
427 | 428 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
252 | 255 | | |
253 | 256 | | |
| 257 | + | |
| 258 | + | |
254 | 259 | | |
255 | 260 | | |
256 | 261 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
65 | 103 | | |
66 | 104 | | |
67 | 105 | | |
| |||
91 | 129 | | |
92 | 130 | | |
93 | 131 | | |
| 132 | + | |
94 | 133 | | |
95 | | - | |
| 134 | + | |
96 | 135 | | |
97 | 136 | | |
| 137 | + | |
| 138 | + | |
98 | 139 | | |
99 | 140 | | |
100 | 141 | | |
101 | 142 | | |
102 | 143 | | |
103 | 144 | | |
104 | 145 | | |
105 | | - | |
| 146 | + | |
| 147 | + | |
106 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
107 | 160 | | |
108 | 161 | | |
109 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
46 | 55 | | |
47 | 56 | | |
48 | 57 | | |
| |||
117 | 126 | | |
118 | 127 | | |
119 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
120 | 132 | | |
121 | 133 | | |
122 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
747 | 747 | | |
748 | 748 | | |
749 | 749 | | |
750 | | - | |
| 750 | + | |
751 | 751 | | |
752 | 752 | | |
753 | 753 | | |
| |||
0 commit comments