Skip to content

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

Open
hamdog0203 wants to merge 1 commit into
ProjectLeonida:mainfrom
hamdog0203:fix/rt-queue-holds-reentry
Open

hamdog0203 wants to merge 1 commit into
ProjectLeonida:mainfrom
hamdog0203:fix/rt-queue-holds-reentry

Conversation

@hamdog0203

Copy link
Copy Markdown
Contributor

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

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.

…t detach/cancel_after (RT-QUEUE-HOLDS-REENTRY)

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adrianmjim

Copy link
Copy Markdown

Tested d705fc9 applied onto main 23c4596 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 23c4596 PR d705fc9
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.

tiguan26 pushed a commit to tiguan26/leonida that referenced this pull request Nov 28, 2026
…ging-upload-128

GPU: use the host upload path for texture staging copies
@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#680 | head d705fc9 (merged cleanly with main c7753ed) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(queue_holds|d3d12_fence_timing|vk_presenter_sync|vk_queue_query_sync|vk_overlapping_occlusion|vk_queue_constructor_oom)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: building queue_holds_test against main's runtime/src/queue_holds.h fails immediately (sibling_instance failed: a=1 r=0 d=0 b=0, exit code 8) | green on PR+main: 6/6 CTests pass (queue_holds 5/5, d3d12_fence_timing, vk_presenter_sync, vk_overlapping_occlusion, vk_queue_constructor_oom, and 3/3 consecutive runs of vk_queue_query_sync with device-loss execute=10, context_lost=1, interceptions=2) with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

AI-assisted (Claude Code, Claude Opus 5.5).

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants