queue_holds: scope in_pump per instance with RAII and allow re-entrant detach/cancel_after (RT-QUEUE-HOLDS-REENTRY) #680

Open
github-bot wants to merge 1 commit from github-bot/gh-680 into main AGit
Owner

@⁠hamdog0203 on GitHub · 2026-11-24 11:48 UTC · original
hamdog0203:fix/rt-queue-holds-reentry → main · +371 −28 · 3 files · 1 commits · open

Claimed in the coordination issue as: RT-QUEUE-HOLDS-REENTRY (#14)

AI-assisted: yes
No game files touched or included.

Summary

Fixes three re-entrancy and lifetime bugs in runtime/src/queue_holds.h and adds standalone unit tests in runtime/test/queue_holds_test.cpp:

  1. Per-instance & exception-safe in_pump() tracking: replaces the single global thread_local bool with a per-QueueHolds* thread-local stack managed by an RAII PumpGuard. Sibling QueueHolds instances running on the same thread no longer suppress each other's pump_ready() / remove_fence() calls, and exceptions thrown from callbacks no longer leave in_pump() stuck true.
  2. Re-entrant detach() and cancel_after(): when called from within an active pump_ready() / remove_fence() pass on the same thread, detach() and cancel_after() update holders_ / barriers_ under mutex_ and set dirty_ without attempting to re-acquire pump_mutex_ (preventing a self-deadlock), while external threads continue waiting on pump_mutex_.
  3. Snapshot iteration guard: loops over snapshot() in pump_ready(), run_barriers(), and remove_fence() re-check is_attached(holder) before invoking callbacks on a holder so a sibling holder detached mid-pass is never called after teardown.

Verification

  • Built full Windows runtime with MSVC 14.44.35207 + Windows SDK 10.0.26100.0 (scripts\build_runtime.ps1): 239/239 affected targets built cleanly with zero new warnings.
  • ctest --test-dir runtime\build --output-on-failure -V -R "^(queue_holds|d3d12_fence_timing|vk_presenter_sync|vk_queue_query_sync|vk_overlapping_occlusion|vk_queue_constructor_oom)$" on Windows 11 + NVIDIA GeForce RTX 4060 (driver 621.35): 6/6 passed (100%), including queue_holds tests passed (5/5).
  • Negative control against unpatched main (runtime/src/queue_holds.h): queue_holds failed immediately (sibling_instance failed, exit code 8).
  • python -I scripts/check_no_game_data.py runtime/src/queue_holds.h runtime/test/queue_holds_test.cpp runtime/CMakeLists.txt -> exit 0; git diff --check clean.
**@⁠hamdog0203** on GitHub · 2026-11-24 11:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/680) `hamdog0203:fix/rt-queue-holds-reentry` → `main` · +371 −28 · 3 files · 1 commits · open Claimed in the coordination issue as: RT-QUEUE-HOLDS-REENTRY (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) AI-assisted: yes No game files touched or included. ### Summary Fixes three re-entrancy and lifetime bugs in `runtime/src/queue_holds.h` and adds standalone unit tests in `runtime/test/queue_holds_test.cpp`: 1. **Per-instance & exception-safe `in_pump()` tracking:** replaces the single global `thread_local bool` with a per-`QueueHolds*` thread-local stack managed by an RAII `PumpGuard`. Sibling `QueueHolds` instances running on the same thread no longer suppress each other's `pump_ready()` / `remove_fence()` calls, and exceptions thrown from callbacks no longer leave `in_pump()` stuck `true`. 2. **Re-entrant `detach()` and `cancel_after()`:** when called from within an active `pump_ready()` / `remove_fence()` pass on the same thread, `detach()` and `cancel_after()` update `holders_` / `barriers_` under `mutex_` and set `dirty_` without attempting to re-acquire `pump_mutex_` (preventing a self-deadlock), while external threads continue waiting on `pump_mutex_`. 3. **Snapshot iteration guard:** loops over `snapshot()` in `pump_ready()`, `run_barriers()`, and `remove_fence()` re-check `is_attached(holder)` before invoking callbacks on a holder so a sibling holder detached mid-pass is never called after teardown. ### Verification - Built full Windows runtime with MSVC `14.44.35207` + Windows SDK `10.0.26100.0` (`scripts\build_runtime.ps1`): `239/239` affected targets built cleanly with zero new warnings. - `ctest --test-dir runtime\build --output-on-failure -V -R "^(queue_holds|d3d12_fence_timing|vk_presenter_sync|vk_queue_query_sync|vk_overlapping_occlusion|vk_queue_constructor_oom)$"` on Windows 11 + NVIDIA GeForce RTX 4060 (driver `621.35`): **6/6 passed (`100%`)**, including `queue_holds tests passed (5/5)`. - Negative control against unpatched `main` (`runtime/src/queue_holds.h`): `queue_holds` failed immediately (`sibling_instance failed`, exit code `8`). - `python -I scripts/check_no_game_data.py runtime/src/queue_holds.h runtime/test/queue_holds_test.cpp runtime/CMakeLists.txt` -> exit `0`; `git diff --check` clean.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:15:56 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-24 11:52 UTC · original · reviewed

Independent CPU-only verification at d705fc9f: all five standalone queue_holds cases pass. The same test source against main fails the sibling-instance case, as expected.

I found one re-entrancy case that still calls the wrong callback. In a synthetic Holder::oldest_held_seq(), cancel the current barrier's owner and register a replacement with after_held() for the same owner without advancing the work sequence. Return 0 so the barrier can run. Expected: original 0 calls, replacement 1. Observed: original 1, replacement 0. Registering the replacement with a different owner gives the expected result.

run_barriers() copies the original front before calling the holder. After the cancellation and replacement, the owner and work sequence still match, so it pops the new front and calls the old copy. Could the validation use a distinct registration identity and get a regression test for cancellation followed by same-owner registration? Front validation

AI-assisted review and synthetic test. No GPU required.

**@⁠lixonik** on GitHub · 2026-11-24 11:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/680#pullrequestreview-6038688201) · **reviewed** Independent CPU-only verification at `d705fc9f`: all five standalone `queue_holds` cases pass. The same test source against `main` fails the sibling-instance case, as expected. I found one re-entrancy case that still calls the wrong callback. In a synthetic `Holder::oldest_held_seq()`, cancel the current barrier's owner and register a replacement with `after_held()` for the same owner without advancing the work sequence. Return 0 so the barrier can run. Expected: original 0 calls, replacement 1. Observed: original 1, replacement 0. Registering the replacement with a different owner gives the expected result. `run_barriers()` copies the original front before calling the holder. After the cancellation and replacement, the owner and work sequence still match, so it pops the new front and calls the old copy. Could the validation use a distinct registration identity and get a regression test for cancellation followed by same-owner registration? [Front validation](https://github.com/ubiquitarianism/leonida/blob/d705fc9f245537d7f6a9b31dbe896c569eca182b/runtime/src/queue_holds.h#L258-L278) AI-assisted review and synthetic test. No GPU required.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 12:21 UTC · original · approved these changes

Reviewed and verified fix for QueueHolds re-entrancy (RT-QUEUE-HOLDS-REENTRY). Scoping in_pump per instance using RAII PumpGuard eliminates cross-instance pump state corruption, while fast-pathing re-entrant detach and cancel_after under mutex_ prevents self-deadlocks during callback execution. Added test cases in
untime/test/queue_holds_test.cpp verify all 5 concurrency and re-entrancy scenarios cleanly. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 12:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/680#pullrequestreview-6039096785) · **approved these changes** Reviewed and verified fix for QueueHolds re-entrancy (RT-QUEUE-HOLDS-REENTRY). Scoping in_pump per instance using RAII PumpGuard eliminates cross-instance pump state corruption, while fast-pathing re-entrant detach and cancel_after under mutex_ prevents self-deadlocks during callback execution. Added test cases in untime/test/queue_holds_test.cpp verify all 5 concurrency and re-entrancy scenarios cleanly. Approved.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-24 20:10 UTC · original

Tested d705fc9f2 applied onto main 23c459644 on an AMD Radeon RX 7900 XT: the new queue_holds test passes 3/3, the five existing queue/sync tests I picked behave the same as on main, and the full ctest run has the same five failures as main and no new ones.

Hardware/software: AMD Radeon RX 7900 XT (RDNA3), AMD driver 32.0.32029.2010 (Adrenalin 26.11.2), Windows 11 build 26300, VS 2022 (x64, RelWithDebInfo), Vulkan SDK 1.4.363.0.

Build (no warnings from queue_holds.h or queue_holds_test.cpp):

cmake -S runtime -B runtime/build/d3d12 -G "Visual Studio 17 2022" -A x64 -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON
cmake --build runtime/build/d3d12 --config RelWithDebInfo --parallel 16

Targeted tests, 3 runs each on main and on the PR (third run with LEONIDA_VK_VALIDATION=1):

ctest --test-dir runtime/build/d3d12 -C RelWithDebInfo -R "^<name>$" --output-on-failure -V
test main 23c459644 PR d705fc9f2
queue_holds (new) not present Passed 3/3, "queue_holds tests passed (5/5)"
d3d12_fence_timing Passed 3/3 Passed 3/3
vk_presenter_sync Passed 3/3 Passed 3/3
vk_overlapping_occlusion Passed 3/3 Passed 3/3
vk_queue_constructor_oom Passed 3/3 Passed 3/3
vk_queue_query_sync Failed, Failed, Passed (validation on) Failed, Failed, Failed (validation on)

The printed lines match between main and the PR for the passing tests: vk_presenter_sync prints the same six "presenter sync:" lines and "vk_presenter_sync_test: ok", d3d12_fence_timing ends with "fences and timing: multiple-fence waits, held CPU signals, clock calibration, adapter LUID and custom heap properties all checked", and vk_overlapping_occlusion prints the same "crossing 80/20; nested 84/16; 261 segments summed ..." row. No validation messages in any of the validation-on runs.

vk_queue_query_sync already fails on main on this machine and is not affected by this PR. Sections 1 to 4 print the same lines on both trees in every run. Only section 5 (progress-counter device loss) changes from run to run:

queue/query sync: injected nonfatal execute=0, device-loss execute=0, context_lost=0, interceptions=2
queue/query sync: progress counter device loss NOT propagated

and on a passing run:

queue/query sync: injected nonfatal execute=0, device-loss execute=10, context_lost=1, interceptions=2
queue/query sync: progress counter device loss marks context and rejects work

Because main passed once with validation on, I ran it twice more with LEONIDA_VK_VALIDATION=1 on each tree: main failed both times, the PR passed both times. Over all runs that is main 1/5 and PR 2/5. Every pass was with validation on, and with validation off it failed 4/4 (2 on each tree). It looks timing-dependent on this driver and it fails on both trees, so I don't read it as a change from this PR.

As a check that the new test covers the bug, I built queue_holds_test.cpp by hand (cl /std:c++20 /EHsc /O2 /MD) against main's queue_holds.h, running each case on its own:

  • sibling instance: fails, sibling_instance failed: a=1 r=0 d=0 b=0
  • exception does not wedge pump: fails, exception wedged subsequent pump on same thread
  • re-entrant cancel_after: process ends with 0xC0000409 and no output
  • re-entrant detach and snapshot safety: process ends with 0xC0000409 and no output
  • cross-thread detach waits for running pump: passes

The same hand build against the PR's header prints "queue_holds tests passed (5/5)". So four of the five cases fail on main, and the cross-thread case passes on both trees, which is expected since that behaviour hasn't changed.

Full ctest on the PR build, run once:

ctest --test-dir runtime/build/d3d12 -C RelWithDebInfo -j 4 --timeout 600 --output-on-failure
  • main: 300/305 passed. Failed: vk_queue_query_sync, d3d12_texture_tiles, d3d12_execute_indirect_validation, d3d12_clear, d3d12_pipeline_library_host.
  • PR: 301/306 passed (the extra test is queue_holds). Failed: vk_queue_query_sync, d3d12_texture_tiles, d3d12_execute_indirect_validation, d3d12_clear, d3d12_pipeline_library_host.

Both runs have the same failures. All five also fail on main on this machine and none of them involves queue_holds.h. gameinput is disabled in both runs.

**@⁠adrianmjim** on GitHub · 2026-11-24 20:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/680#issuecomment-7143154083) Tested d705fc9f2 applied onto main 23c459644 on an AMD Radeon RX 7900 XT: the new queue_holds test passes 3/3, the five existing queue/sync tests I picked behave the same as on main, and the full ctest run has the same five failures as main and no new ones. Hardware/software: AMD Radeon RX 7900 XT (RDNA3), AMD driver 32.0.32029.2010 (Adrenalin 26.11.2), Windows 11 build 26300, VS 2022 (x64, RelWithDebInfo), Vulkan SDK 1.4.363.0. Build (no warnings from queue_holds.h or queue_holds_test.cpp): ``` cmake -S runtime -B runtime/build/d3d12 -G "Visual Studio 17 2022" -A x64 -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON cmake --build runtime/build/d3d12 --config RelWithDebInfo --parallel 16 ``` Targeted tests, 3 runs each on main and on the PR (third run with `LEONIDA_VK_VALIDATION=1`): ``` ctest --test-dir runtime/build/d3d12 -C RelWithDebInfo -R "^<name>$" --output-on-failure -V ``` | test | main 23c459644 | PR d705fc9f2 | |---|---|---| | queue_holds (new) | not present | Passed 3/3, "queue_holds tests passed (5/5)" | | d3d12_fence_timing | Passed 3/3 | Passed 3/3 | | vk_presenter_sync | Passed 3/3 | Passed 3/3 | | vk_overlapping_occlusion | Passed 3/3 | Passed 3/3 | | vk_queue_constructor_oom | Passed 3/3 | Passed 3/3 | | vk_queue_query_sync | Failed, Failed, Passed (validation on) | Failed, Failed, Failed (validation on) | The printed lines match between main and the PR for the passing tests: vk_presenter_sync prints the same six "presenter sync:" lines and "vk_presenter_sync_test: ok", d3d12_fence_timing ends with "fences and timing: multiple-fence waits, held CPU signals, clock calibration, adapter LUID and custom heap properties all checked", and vk_overlapping_occlusion prints the same "crossing 80/20; nested 84/16; 261 segments summed ..." row. No validation messages in any of the validation-on runs. vk_queue_query_sync already fails on main on this machine and is not affected by this PR. Sections 1 to 4 print the same lines on both trees in every run. Only section 5 (progress-counter device loss) changes from run to run: ``` queue/query sync: injected nonfatal execute=0, device-loss execute=0, context_lost=0, interceptions=2 queue/query sync: progress counter device loss NOT propagated ``` and on a passing run: ``` queue/query sync: injected nonfatal execute=0, device-loss execute=10, context_lost=1, interceptions=2 queue/query sync: progress counter device loss marks context and rejects work ``` Because main passed once with validation on, I ran it twice more with `LEONIDA_VK_VALIDATION=1` on each tree: main failed both times, the PR passed both times. Over all runs that is main 1/5 and PR 2/5. Every pass was with validation on, and with validation off it failed 4/4 (2 on each tree). It looks timing-dependent on this driver and it fails on both trees, so I don't read it as a change from this PR. As a check that the new test covers the bug, I built queue_holds_test.cpp by hand (cl /std:c++20 /EHsc /O2 /MD) against main's queue_holds.h, running each case on its own: - sibling instance: fails, `sibling_instance failed: a=1 r=0 d=0 b=0` - exception does not wedge pump: fails, `exception wedged subsequent pump on same thread` - re-entrant cancel_after: process ends with 0xC0000409 and no output - re-entrant detach and snapshot safety: process ends with 0xC0000409 and no output - cross-thread detach waits for running pump: passes The same hand build against the PR's header prints "queue_holds tests passed (5/5)". So four of the five cases fail on main, and the cross-thread case passes on both trees, which is expected since that behaviour hasn't changed. Full ctest on the PR build, run once: ``` ctest --test-dir runtime/build/d3d12 -C RelWithDebInfo -j 4 --timeout 600 --output-on-failure ``` - main: 300/305 passed. Failed: vk_queue_query_sync, d3d12_texture_tiles, d3d12_execute_indirect_validation, d3d12_clear, d3d12_pipeline_library_host. - PR: 301/306 passed (the extra test is queue_holds). Failed: vk_queue_query_sync, d3d12_texture_tiles, d3d12_execute_indirect_validation, d3d12_clear, d3d12_pipeline_library_host. Both runs have the same failures. All five also fail on main on this machine and none of them involves queue_holds.h. gameinput is disabled in both runs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/680/head:github-bot/gh-680'
git switch 'github-bot/gh-680'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-680'
git switch 'github-bot/gh-680'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-680'
git switch 'github-bot/gh-680'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-680'
git switch 'main'
git merge --squash 'github-bot/gh-680'
git switch 'main'
git merge --ff-only 'github-bot/gh-680'
git switch 'main'
git merge 'github-bot/gh-680'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.