Assertion failure: numDelayed_ == 0, at js/src/vm/OffThreadPromiseRuntimeState.cpp:262 (shutdown() drains dispatch queue without decrementing numDelayed_)
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: decoder, Unassigned)
References
(Blocks 1 open bug)
Details
(Keywords: ai-involved, testcase)
Attachments
(3 files)
Summary
OffThreadPromiseRuntimeState::shutdown() fails to maintain its delayed-dispatchable accounting (numDelayed_) when it drains the internal dispatch queue at runtime teardown. A cross-thread Atomics.notify that lands in another runtime's exit window — after that runtime's final job-queue drain but before shutdown() closes the queue — moves an expired WaitAsyncTimeoutTask from the delayed priority queue into the internal dispatch queue and increments numDelayed_. The matching decrement exists only in internalDrain(); shutdown()'s swap-and-drain loop consumes the task without decrementing. The runtime is then destroyed with numDelayed_ != 0, hitting:
Assertion failure: numDelayed_ == 0, at js/src/vm/OffThreadPromiseRuntimeState.cpp:262
in ~OffThreadPromiseRuntimeState() (MOZ_ASSERT_IF(!forceQuitting_, numDelayed_ == 0)). The reproducer is 100% deterministic.
JS shell version (product_version): 20260603-5cffec5992d3 (m-c debug, aarch64).
Root cause
numDelayed_ tracks JS::Dispatchables that are not registered with OffThreadPromiseRuntimeState (today only WaitAsyncTimeoutTask, see field comment OffThreadPromiseRuntimeState.h:275-286) after they have been moved from the delayed priority queue into the internal dispatch queue.
-
Increment —
dispatchDelayedTasks(),js/src/vm/OffThreadPromiseRuntimeState.cpp:415: every expired entry moved frominternalDelayedDispatchPriorityQueue_tointernalDispatchQueue_doesnumDelayed_++.dispatchDelayedTasks()is called not only frominternalDrain()but also frominternalDispatchToEventLoop()(OffThreadPromiseRuntimeState.cpp:348), which runs on whatever thread dispatches into this runtime's queue — e.g. another thread'sAtomics.notify→atomics_notify_impl(AtomicsObject.cpp:1503) →OffThreadPromiseTask::removeFromCancellableListAndDispatch(OffThreadPromiseRuntimeState.cpp:190-208) →DispatchResolveAndDestroy→dispatchToEventLoop. -
Decrement — only in
internalDrain(),OffThreadPromiseRuntimeState.cpp:462-464:
d = std::move(internalDispatchQueue().front());
internalDispatchQueue().popFront();
if (!d->registered()) {
numDelayed_--;
}
-
Missing decrement —
shutdown(),OffThreadPromiseRuntimeState.cpp:535-551: the queue is swapped out, closed, and every entry is run withJS::Dispatchable::ShuttingDown, but unregistered entries are never subtracted fromnumDelayed_. -
Detection —
~OffThreadPromiseRuntimeState(), line 262:MOZ_ASSERT_IF(!forceQuitting_, numDelayed_ == 0).
The hole is only observable when a task is moved into the dispatch queue after the runtime's last internalDrain() — i.e. by a foreign thread dispatching into the dying runtime's queue. That window is real on every runtime exit: between the final drain and shutdown() setting internalDispatchQueueClosed_, the queue is still open and the global helper-thread lock is free.
Related earlier fixes in this assert family: bug 1963067 ("correct asserts related to SAB deletion and unregistered timeout tasks") and bug 1995991 ("Do not assert the remaining tasks when force-quitting", which added forceQuitting_ to paper over the same shutdown-drain asymmetry for the quit() case). The cross-thread-dispatch-in-exit-window case remains unhandled.
Reproduction
Run the deterministic PoC in the debug shell:
$ m-c-debug/dist/bin/js --fuzzing-safe poc.js
poc.js: main thread shares a SharedArrayBuffer, spawns a worker, then calls Atomics.waitAsync(ta, 0, 0, 1) (registers a cancellable notify task + a 1ms timeout task). The script ends; the shell's final job-queue drain runs while the timeout has not expired (it stays in the delayed queue), so that is the runtime's last internalDrain(). The shell then blocks ~200ms joining the worker. The worker sleeps 200ms (so the 1ms timeout is long expired and main is parked in the join) then calls Atomics.notify(ta, 0), which dispatches the notify task into the main runtime's queue via internalDispatchToEventLoop → dispatchDelayedTasks(), moving the expired timeout task into the dispatch queue and doing numDelayed_++. Main then joins, reaches DestroyContext → shutdown(), drains the tasks without decrementing, and ~OffThreadPromiseRuntimeState() asserts.
Reproduction rate: 8/8 across poc.js and poc_v2.js. stress1.js is a non-deterministic stress variant that hits the same assertion on a worker runtime's destruction, showing the window exists on any runtime's exit.
The triggering operations (Atomics.waitAsync with timeout + cross-agent Atomics.notify + agent teardown) are all web-exposed; the shell evalInWorker/setSharedObject/getSharedObject helpers only make it conveniently reproducible.
Crash signature
Assertion failure: numDelayed_ == 0, at js/src/vm/OffThreadPromiseRuntimeState.cpp:262
#01: js::OffThreadPromiseRuntimeState::~OffThreadPromiseRuntimeState()
#02: JSRuntime::~JSRuntime()
#03: js::DestroyContext(JSContext*)
#04: main
(SIGSEGV, exit code 139, core dumped)
Impact
Debug builds: guaranteed assertion failure / abort at runtime teardown, controllable from script timing. Opt-ASan builds run the PoC cleanly — no memory corruption was observed; the practical effect is that numDelayed_ is left permanently desynchronized, feeding a wrong result into internalHasPending() (numDelayed_ > 0 || numRegistered_ > cancellable().count()), i.e. the engine's notion of whether off-thread promise work is still pending. Because no memory-safety impact was demonstrated, marked sec-audit.
Suggested fix
Mirror internalDrain()'s bookkeeping in shutdown()'s swap-drain loop — decrement numDelayed_ for each unregistered entry (counting them while still holding the lock during the swap), and consider explicitly draining/accounting internalDelayedDispatchPriorityQueue_ at shutdown rather than relying on the destructor assert.
Testcase files
poc.js— deterministic reproducer (assert on main-runtime destruction).poc_v2.js— variant with an un-notified timeout waiter (same assert).stress1.js— stress variant hitting the same assert on worker-runtime destruction.poc_output.txt,pnd_*.log,pv2_*.log,stress1_dbg.log— crash logs.
| Reporter | ||
Comment 1•5 months ago
|
||
| Reporter | ||
Comment 2•5 months ago
|
||
| Reporter | ||
Comment 3•5 months ago
|
||
Updated•5 months ago
|
Comment 6•2 months ago
|
||
The browser doesn't use our internal dispatch queue.
Description
•