[PR #1783] runtime: make QueueHolds pump handoff race-free, exception-safe, and per-instance (RT-QUEUE-HOLDS-PUMP-SAFETY) #1797

Open
opened 2026-11-26 21:18:59 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1783
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-queue-holds-pump-safety


📝 Commits (2)

  • 3e6e229 runtime: make QueueHolds pump handoff race-free, exception-safe, and per-instance
  • eee02b7 runtime: preserve dirty handoff in QueueHolds::pump_ready on exception

📊 Changes

3 files changed (+631 additions, -23 deletions)

View changed files

📝 runtime/CMakeLists.txt (+5 -0)
📝 runtime/src/queue_holds.h (+85 -23)
➕ runtime/test/queue_holds_test.cpp (+541 -0)

📄 Description

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).

Root causes

  1. Lost-wakeup race in pump_ready():
    Previously a thread finishing a pump pass executed pump.unlock() (release) followed by dirty_.load(memory_order_acquire), while a concurrent caller wrote dirty_.store(true, memory_order_release) followed by a failing try_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 observe dirty_ == false before unlocking while the arriving thread's try_to_lock() fails before the unlock becomes visible, leaving ready held batches or deferred Presents stalled.
  2. Permanent per-thread hang on exception:
    in_pump() was manually set to true before snapshot() / discard_waiting_on() / release_ready() / run_barriers() and reset to false afterwards. Any thrown exception (e.g. std::bad_alloc inside snapshot() or a holder/barrier callback) left in_pump() permanently true on that thread, silently turning all later pump_ready() and remove_fence() calls on that thread into no-ops.
  3. Process-wide thread_local bool shared across QueueHolds instances:
    A nested pump_ready() or remove_fence() on a second QueueHolds instance while pumping a first instance on the same thread saw in_pump() == true and returned without pumping or discarding.

Changes

  • runtime/src/queue_holds.h:
    • Serialize dirty_ and pump_active_ under the leaf mutex_ (preserving strict pump_mutex_ -> mutex_ lock ordering) with an RAII ActiveGuard, eliminating the Store-Load lost-wakeup race.
    • Replace the manual static thread_local bool with a zero-allocation, noexcept, stack-intrusive RAII PumpScope chain keyed by const QueueHolds*, making in_pump() both exception-safe and per-instance.
  • runtime/test/queue_holds_test.cpp & runtime/CMakeLists.txt:
    • Register portable queue_holds CTest covering cross-queue Wait-before-Signal chains + Present barriers, exception unwinding in release_ready() / Barrier::fn / remove_fence(), nested independent QueueHolds instances, detach() / cancel_after() synchronization, and a 4-thread high-contention pump handoff stress test (24,000 held ops + 3,000 barriers).

Verification

  • AI-assisted: yes
  • GPUs tested: none (portable standard-library C++20 runtime header + test, verified locally on macOS Apple Silicon ARM64 with Clang; Windows MSVC build / Vulkan queue integration tests rely on Windows CI).
  • Commands run:
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/queue_holds_test.cpp — pass
    • clang++ -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 pass
    • Mutation test compiling queue_holds_test.cpp against unpatched main:runtime/src/queue_holds.h — fails on test_exception_safety_does_not_wedge_thread (ran == 1 && !holds.any_held()), confirming regression coverage
    • python3 -I scripts/check_no_game_data.py runtime/src/queue_holds.h runtime/test/queue_holds_test.cpp runtime/CMakeLists.txt — pass
    • python3 -I tests/test_text_write_line_endings.py — 3/3 pass

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1783 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-queue-holds-pump-safety` --- ### 📝 Commits (2) - [`3e6e229`](https://github.com/ubiquitarianism/leonida/commit/3e6e229a642e3c4c7787291d9d975dff74b535a9) runtime: make QueueHolds pump handoff race-free, exception-safe, and per-instance - [`eee02b7`](https://github.com/ubiquitarianism/leonida/commit/eee02b7699235b48f0243e88ac16471f7c8afed8) runtime: preserve dirty handoff in QueueHolds::pump_ready on exception ### 📊 Changes **3 files changed** (+631 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+5 -0) 📝 `runtime/src/queue_holds.h` (+85 -23) ➕ `runtime/test/queue_holds_test.cpp` (+541 -0) </details> ### 📄 Description ## 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`). - **Coordination item**: `RT-QUEUE-HOLDS-PUMP-SAFETY` - **Claim**: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161810177 ### Root causes 1. **Lost-wakeup race in `pump_ready()`**: Previously a thread finishing a pump pass executed `pump.unlock()` (release) followed by `dirty_.load(memory_order_acquire)`, while a concurrent caller wrote `dirty_.store(true, memory_order_release)` followed by a failing `try_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 observe `dirty_ == false` before unlocking while the arriving thread's `try_to_lock()` fails before the unlock becomes visible, leaving ready held batches or deferred Presents stalled. 2. **Permanent per-thread hang on exception**: `in_pump()` was manually set to `true` before `snapshot()` / `discard_waiting_on()` / `release_ready()` / `run_barriers()` and reset to `false` afterwards. Any thrown exception (e.g. `std::bad_alloc` inside `snapshot()` or a holder/barrier callback) left `in_pump()` permanently `true` on that thread, silently turning all later `pump_ready()` and `remove_fence()` calls on that thread into no-ops. 3. **Process-wide `thread_local bool` shared across `QueueHolds` instances**: A nested `pump_ready()` or `remove_fence()` on a second `QueueHolds` instance while pumping a first instance on the same thread saw `in_pump() == true` and returned without pumping or discarding. ### Changes - [`runtime/src/queue_holds.h`](runtime/src/queue_holds.h): - Serialize `dirty_` and `pump_active_` under the leaf `mutex_` (preserving strict `pump_mutex_` -> `mutex_` lock ordering) with an RAII `ActiveGuard`, eliminating the Store-Load lost-wakeup race. - Replace the manual `static thread_local bool` with a zero-allocation, `noexcept`, stack-intrusive RAII `PumpScope` chain keyed by `const QueueHolds*`, making `in_pump()` both exception-safe and per-instance. - [`runtime/test/queue_holds_test.cpp`](runtime/test/queue_holds_test.cpp) & [`runtime/CMakeLists.txt`](runtime/CMakeLists.txt): - Register portable `queue_holds` CTest covering cross-queue Wait-before-Signal chains + Present barriers, exception unwinding in `release_ready()` / `Barrier::fn` / `remove_fence()`, nested independent `QueueHolds` instances, `detach()` / `cancel_after()` synchronization, and a 4-thread high-contention pump handoff stress test (24,000 held ops + 3,000 barriers). ## Verification - **AI-assisted**: yes - **GPUs tested**: none (portable standard-library C++20 runtime header + test, verified locally on macOS Apple Silicon ARM64 with Clang; Windows MSVC build / Vulkan queue integration tests rely on Windows CI). - Commands run: - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/queue_holds_test.cpp` — pass - `clang++ -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 pass - Mutation test compiling `queue_holds_test.cpp` against unpatched `main:runtime/src/queue_holds.h` — fails on `test_exception_safety_does_not_wedge_thread` (`ran == 1 && !holds.any_held()`), confirming regression coverage - `python3 -I scripts/check_no_game_data.py runtime/src/queue_holds.h runtime/test/queue_holds_test.cpp runtime/CMakeLists.txt` — pass - `python3 -I tests/test_text_write_line_endings.py` — 3/3 pass --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1797
No description provided.