runtime: make QueueHolds pump handoff race-free, exception-safe, and per-instance (RT-QUEUE-HOLDS-PUMP-SAFETY) #1783
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1783"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T12:04:26Z.
Summary
Fixes three concurrency/deadlock bugs in
runtime/src/queue_holds.h(the cross-queue Wait-before-Signal and Present barrier scheduler) and adds a standalone portable unit + concurrency stress test suite (runtime/test/queue_holds_test.cpp).RT-QUEUE-HOLDS-PUMP-SAFETYRoot causes
pump_ready():Previously a thread finishing a pump pass executed
pump.unlock()(release) followed bydirty_.load(memory_order_acquire), while a concurrent caller wrotedirty_.store(true, memory_order_release)followed by a failingtry_to_lock(). Under the C++ memory model (and on weakly ordered architectures such as ARM64), a release store followed by an acquire load permits Store-Load reordering: the exiting thread can observedirty_ == falsebefore unlocking while the arriving thread'stry_to_lock()fails before the unlock becomes visible, leaving ready held batches or deferred Presents stalled.in_pump()was manually set totruebeforesnapshot()/discard_waiting_on()/release_ready()/run_barriers()and reset tofalseafterwards. Any thrown exception (e.g.std::bad_allocinsidesnapshot()or a holder/barrier callback) leftin_pump()permanentlytrueon that thread, silently turning all laterpump_ready()andremove_fence()calls on that thread into no-ops.thread_local boolshared acrossQueueHoldsinstances:A nested
pump_ready()orremove_fence()on a secondQueueHoldsinstance while pumping a first instance on the same thread sawin_pump() == trueand returned without pumping or discarding.Changes
runtime/src/queue_holds.h:dirty_andpump_active_under the leafmutex_(preserving strictpump_mutex_->mutex_lock ordering) with an RAIIActiveGuard, eliminating the Store-Load lost-wakeup race.static thread_local boolwith a zero-allocation,noexcept, stack-intrusive RAIIPumpScopechain keyed byconst QueueHolds*, makingin_pump()both exception-safe and per-instance.runtime/test/queue_holds_test.cpp&runtime/CMakeLists.txt:queue_holdsCTest covering cross-queue Wait-before-Signal chains + Present barriers, exception unwinding inrelease_ready()/Barrier::fn/remove_fence(), nested independentQueueHoldsinstances,detach()/cancel_after()synchronization, and a 4-thread high-contention pump handoff stress test (24,000 held ops + 3,000 barriers).Verification
clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/queue_holds_test.cpp— passclang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=thread -O1 runtime/test/queue_holds_test.cpp— pass (zero data races)clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/queue_holds_test.cpp(5 consecutive runs) — 5/5 passqueue_holds_test.cppagainst unpatchedmain:runtime/src/queue_holds.h— fails ontest_exception_safety_does_not_wedge_thread(ran == 1 && !holds.any_held()), confirming regression coveragepython3 -I scripts/check_no_game_data.py runtime/src/queue_holds.h runtime/test/queue_holds_test.cpp runtime/CMakeLists.txt— passpython3 -I tests/test_text_write_line_endings.py— 3/3 passImported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T12:04:43Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-25T12:13:38Z.
Review: COMMENTED
Independent CPU verification at
3e6e229a. The standalone queue_holds_test passed with GCC and UBSan under Ubuntu/WSL. I reproduced an additional case that combines a concurrent handoff with a throwing holder, described inline. A three-way merge of this head with #680 also conflicts in queue_holds.h and queue_holds_test.cpp, so those changes will need coordination if both proceed.AI-assisted. Synthetic holders only.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-25T12:13:38Z.
Code review location:
runtime/src/queue_holds.h; line 174A dirty handoff can still be left pending if the active pump throws. I used a latch to hold thread A inside release_ready(). While it was inside, thread B registered an after_held callback. B set dirty_ and returned through the pump_active_ path. I then let A throw and unwind. The callback count was still zero after both calls returned. An explicit later pump_ready() ran it once. This reproduced on the base and on this head, and differs from the sequential exception test because the incoming work has already requested a pump. Could the unwind path preserve that handoff, or explicitly document that concurrently queued work needs a fresh pump after an exception?
Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T12:28:37Z.
Thank you @lixonik for the GCC/UBSan verification and for catching that concurrent-handoff-on-throw edge case!
Fixed in
eee02b769:QueueHolds::pump_ready()andrun_barriers()now capture the first exception (std::exception_ptr), skip anyHolderthat already threw during the current pump (preventing a throwing holder from spinning on re-entrantpump_ready()), drain sibling holders and barriers, and keep honouring any concurrentdirty_handoff before clearingpump_active_and rethrowingfirst_error.test_concurrent_handoff_preserved_on_throw()inruntime/test/queue_holds_test.cppcovering the latch-heldafter_heldhandoff you described, concurrent sibling-holder signal handoff, barrier-throw handoff, and an adversarial re-entrant throwing holder.clang++ -std=c++20 -Wall -Wextra -Werror) under ASan+UBSan, TSan (0 races), and-O2× 3.Also noted on
#680(RT-QUEUE-HOLDS-REENTRY): if#680lands first (adding re-entrantdetach/cancel_after), I will rebase this PR cleanly on top to keep both the re-entrantdetach/cancel_afterpaths and themutex_-serializedpump_active_/dirty_handoff fix.AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-25T20:01:22Z.
Review: APPROVED
Approving. Merged into current main (
e48ff03) with no conflicts. I builtqueue_holds_testnatively (clang 19, Linux) and ran it 3 times plain and 3 times under ThreadSanitizer: all pass, with no TSan reports. Against main'squeue_holds.hthe same test fails (line 162, the exception case), so it does exercise the new code.The new handoff is right.
dirty_andpump_active_are read and written only undermutex_, so a caller either becomes the pumper or leavesdirty_for one that has not yet checked it. The release-store / acquire-load window the description names can no longer drop a wakeup. The lock order (pump_mutex_then the leafmutex_) is the onedetach,cancel_afterandremove_fencealready use. A per-instancePumpScopeinstead of one thread_local flag also fixes nested instances.One note for whoever links this to the post-story-load stall: on x86 with MSVC's
std::mutex(SRWLock, whose release is an interlocked op) and under Wine, the old pattern would rarely if ever lose a wakeup in practice. So this is correct hardening, but probably not the cause of that stall on its own. The stall reports show list-value gates timing out while writes stay unlanded, which points at held work or an unlandedwrittenfence instead.AI-assisted (Claude).
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:41:31Z.
#1783 | head
eee02b7699(merged cleanly with maind48a957bb9) |ctest --test-dir runtime/build -C RelWithDebInfo -R "^(queue_holds|d3d12_present)$" --output-on-failure(LEONIDA_VK_VALIDATION=1) | red on main:queue_holdsfails atline 162: ran == 1 && !holds.any_held()(test_exception_safety_does_not_wedge_threadleavesin_pump()wedged after exception unwinding) | green on PR+main: 2/2 CTests pass (queue_holdsunit/exception/nested-instance/4-thread 24k-op stress +d3d12_present70-frame Xbox-ABI swapchain present/resize/failure-recovery) on RTX 3090 with0Khronos validation errors;check_no_game_data.py0violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yesImported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T13:43:30Z.
Review: APPROVED
Re-reviewed
eee02b769and verified the concurrent-handoff-on-throw fix.Code audit:
runtime/src/queue_holds.h:pump_ready()andrun_barriers()cleanly capturefirst_error(std::exception_ptr), skip holders that already threw during the current pump viafailed_holders, continue draining sibling holders/barriers and honoring concurrentdirty_handoffs undermutex_before clearingpump_active_and rethrowingfirst_error. Per-instancePumpScopereplaces the globalthread_local bool.Verification at
eee02b769(Windows 11, MSVC 19.44, Ninja Release,LEONIDA_VK_VALIDATION=1):queue_holds_testandd3d12_present_test.ctest -R '^(queue_holds|d3d12_present)$': 2/2 passed (0 validation errors).python -I scripts/check_no_game_data.py: passed.GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341,
VK_LAYER_KHRONOS_validation1.4.363).AI-assisted (Antigravity, Gemini).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T00:13:48Z.
Verified by OreoFR
a8465211f+ PR headeee02b769(merged with --no-ff, no conflict; tested in a package with other PRs that share no file with it)ctest -R ^queue_holds$(red: the PR's test with src/queue_holds.h from main)ran == 1 && !holds.any_held())runtime/CMakeLists.txtandruntime/src/queue_holds.hwith the open #2566; a rebase may be needed if #2566 lands first.AI-assisted (Claude Code, Claude Opus 5.5).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:15:15Z.
Verified by OreoFR: link check
59e65849b+ this PR (headeee02b769, the head verified earlier) merged with --no-ff, built without the XMA decoder (LEONIDA_CARGO=OFF, noLEONIDA_XMA2_DECODER_LIB: configure reports the XMA2 and Opus decoders not built), RelWithDebInfo, Ninja, MSVC 19.44.35230 (VS 2022 BuildTools),LEONIDA_REQUIRE_VULKAN=ONgta6rt.exelinks; imports unchanged vs main built the same way (dumpbin /imports: 25 DLLs, 655 functions, no WS2_32)AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T08:10:35Z.
Merged after OreoFR's red/green check (check: green with this PR merged onto
main, red without it;gta6rt.exestill links with unchanged imports when the PR touches the runtime) and a re-check here: no conflict withmaind66464eb4, same head as checked, not frozen, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known onmainor Vulkan).AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.