Skip to content

vulkan: drop holds created while a fence is being released - #4570

Open
sds-228 wants to merge 2 commits into
ItsNotRexed:mainfrom
sds-228:queue-holds-release
Open

sds-228 wants to merge 2 commits into
ItsNotRexed:mainfrom
sds-228:queue-holds-release

Conversation

@sds-228

@sds-228 sds-228 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

QueueHolds::remove_fence() erased the fence and returned when held_ was still zero. A queue that had already called pending() (seeing the fence unsatisfied) but had not yet counted its hold could create that hold after the drop. pump_ready() then treated the unknown semaphore as satisfied and submitted the batch with a destroyed VkSemaphore.

Changes:

  • Bracket the pending()..hold() window with a reservation that remove_fence() waits for.
  • Drop held batches while the fence is still registered, then erase it, so a concurrent pump_ready() cannot release them first.
  • CPU-only two-thread regression forcing the exact interleaving; red before, green after.

Validation: RTX 3090 / 621.24, validation on. Targeted 1/1; full CTest 155 passed, 0 failed; 0 validation errors. No game data.

AI-assisted (OpenCode)

QueueHolds::remove_fence() erased the fence and returned when held_ was still zero. A queue that had already called pending() (seeing the fence unsatisfied) but had not yet counted its hold could then create that hold after the drop, and pump_ready() treated the now-unknown semaphore as satisfied and submitted the batch with a destroyed VkSemaphore.

Bracket the pending()..hold() window with a reservation that remove_fence() waits for, and drop held batches while the fence is still registered so a concurrent pump_ready() cannot release them first. Only after the drop is the fence erased.

Regression: two threads force remove_fence() into the window between a pending() decision and hold(). Red before (hold released), green after (hold dropped). CPU-only; no game data.

@Kam471 Kam471 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.

Built and ran at 299c98f1 on Windows 11 (MSVC, RelWithDebInfo): queue_holds_release_test -> passed (ctest 1/1, 0.02 s; direct run exit 0, queue_holds: a fence released mid-decision drops the hold instead of submitting it). git diff --check clean.

The test is deterministic rather than probabilistic: it brackets begin_hold()/pending(), waits until remove_fence() is entered, then creates the hold inside the reservation, and asserts dropped, not released, and any_held()==0. Moving the fences_.erase after the drop and pumping with the fence still registered closes both halves of the window.

Overlap to coordinate: #4557 also rewrites runtime/src/queue_holds.h and both call sites in runtime/src/vulkan_backend.h (held_changed -> held_added/held_removed, plus the same execute_lists/Signal functions), and its d3d12_queue_handover_test uses held_changed. Whichever of #4557/#4570 lands first, the other needs a rebase and a re-run; if both are wanted, the reservation bracket should sit next to #4557's place accounting rather than be resolved mechanically.

@Flyander Flyander 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.

The new reservation closes the pre-existing pending-to-hold window, but there is still a window after the zero-reservation wait.

Comment thread runtime/src/queue_holds.h
std::lock_guard lock(mutex_);
fences_.erase(semaphore);
std::unique_lock reservation(reservation_mutex_);
reservation_cv_.wait(reservation, [this] { return hold_reservations_ == 0; });

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.

[P2] This wait only drains reservations that already exist. Once it returns and releases reservation_mutex_, a new execute() can call begin_hold() while this fence is still registered and unsatisfied. If that queue acquires its state lock after discard_waiting_on() passed it, it parks a batch; remove_fence() then erases the fence and pump_ready() treats the now-unknown semaphore as satisfied, submitting with a destroyed VkSemaphore. Please prevent new holds from entering until dropping and erasing this fence are complete, and add a deterministic test for this post-zero window.

@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verdict: needs-work. The window this PR names is closed, but the adjacent one of the same kind is not, and I reproduced it on the PR head.

QueueHolds::remove_fence() (runtime/src/queue_holds.h:51-69) waits for reservations to drain and only then drops the holds that wait on the semaphore — but the reservation is released before fences_.erase (:66). A begin_hold() ... end_hold() bracket opened between the drain and the erase creates a hold that, after the erase, is no longer pending() (:83), so pump_ready() (:68) submits it with a VkSemaphore that was just destroyed. The comment at :105-107 ("a decision made before the removal cannot turn into a hold after it") is therefore not true.

Reproduction: a two-thread counterexample in a scratch copy (a fake Holder whose discard_waiting_on does nothing, bracket opened right after the reservation is released) prints iteration 0: BUG reproduced on the PR head, exit 1. With the drop moved after the erase, both the counterexample and the PR's new test pass.

Suggested fix: hold pump_mutex_, erase the fence, then drop. The hold decision and the push already sit in one state_mutex_ critical section (vulkan_backend.h:1577-1612), so nothing can decide to hold in between and the reservation/cv is not needed. Keep reservation_mutex_ out of the drop path: the bracket already takes state_mutex_ → reservation_mutex_, so extending the reservation would invert the order and deadlock.

Other findings:

  1. runtime/src/queue_holds.h:27-30 and docs/d3d12_vulkan.md:2181: "no caller waits for another's recording" is no longer true — remove_fence now waits for another thread's bracket, and the bracket wraps all of execute_now (vulkan_backend.h:1579). Both texts need updating.
  2. queue_holds.h:51-69 lost the if (held_ == 0) return; early-out, so every fence destroy takes pump_mutex_ and the state_mutex_ of every queue and can queue behind an ExecuteCommandLists.
  3. Pre-existing, same class: vulkan_backend.h:2083-2092 — discard_waiting_on only looks at it->waits, so a hold-Signal on the destroyed fence, and waits_ records written by wait() but never submitted (:1620-1624), still reach a dead handle. Worth a finding:.
  4. queue_holds.h:108-118: begin_hold()/end_hold() are not RAII; an exception out of hold() (it copies lists) leaves the counter open and every later remove_fence hangs.

Build/tests: cmake -S runtime -B ... -G Ninja + cmake --build → 116/116 targets, exit 0; ctest -R "queue_holds|large_module_gate" → 2/2 passed; the PR's test also passes under -fsanitize=thread. The new test is honest, but it keeps the bracket open while remove_fence waits, so it can never see the window above.

Hygiene: no item ID in #4571 for this work; #4557 (D3D-203) edits the same two call sites, so one of the two needs a rebase — worth stating explicitly.

AI-assisted review.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified PR #4570 on Windows 11 (25H2 26200.9974) / AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, MSVC 19.44.35230.0, Vulkan SDK 1.4.363.0) at head 299c98f1:

  • Build: configured Ninja RelWithDebInfo and built queue_holds_release_test + d3d12_queue_handover_test ([13/13], exit 0).
  • CTest (standard & LEONIDA_VK_VALIDATION=1):
    • Standard: 2/2 passed (7.83 s): queue_holds_release (0.03 s), d3d12_queue_handover (7.65 s).
    • LEONIDA_VK_VALIDATION=1: 2/2 passed (7.44 s): queue_holds_release (0.01 s), d3d12_queue_handover (7.43 s).
  • Direct AMD RX 6700 XT runs under LEONIDA_VK_VALIDATION=1 (0 Vulkan validation errors):
    • queue_holds_release_test.exe (rc=0): queue_holds: a fence released mid-decision drops the hold instead of submitting it.
    • d3d12_queue_handover_test.exe (rc=0): Vulkan layer 371 cases passed, every one exact and Host D3D12 (AMD Radeon RX 6700 XT) 369 cases passed, every one exact.
  • Hygiene & CTest registration: tests/test_ctest_registration.py 4/4 OK (0.052s), scripts/check_no_game_data.py & git diff --check upstream/main...HEAD clean (0).

Agree with @Flyander's CHANGES_REQUESTED and @wavehs's reproduction above: releasing reservation_mutex_ (runtime/src/queue_holds.h:54-56) before fences_.erase(semaphore) (:66) still leaves the post-wait window open to a new begin_hold() ... end_hold() bracket, and #4557 edits the same call sites (held_changed -> held_added/held_removed + QueueHolds::discard_waiting_on), so whichever lands second will need a rebase.

AI-assisted (Gemini, Antigravity).

@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested 299c98f1 merged onto main d0835dd9 (2 behind; merges cleanly) on RTX 4070, NVIDIA 621.14, Windows 11 (10.0.26300), MSVC 19.44, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363: pass.

  • The new queue_holds_release passes.
  • ctest: 145 passed, 10 skipped, 1 failed; run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed, same as main d0835dd9. d3d12_present failed once in this run; it fails the same way intermittently on main d0835dd9 on this machine (once in three full runs, 5/5 passes alone), so it is not from this change.

AI-assisted (Claude Code).

Flyander and wavehs found that waiting for existing zero reservations was not enough: once that wait returned, a new execute() could open a bracket while the fence was still registered, park a hold, and let pump_ready() submit it after the semaphore was destroyed.\n\nPublish the removal before draining reservations so begin_hold() cannot open while the fence is still being removed, and take begin_hold() outside the queue lock so the drain can still take that lock. Add a deterministic two-thread regression for the post-zero window (red 4, green 0). Use an RAII reservation so an exception while copying held lists cannot hang every later remove_fence(), and update the thread comment.
@sds-228

sds-228 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in b17551c0: the removal is published before reservations drain, so no new bracket opens until the fence is erased. begin_hold() moved outside the queue lock to avoid lock inversion, the bracket is now RAII for exception safety, and a deterministic post-zero-window regression was added (red 4, green 0). Targeted 50/50; full CTest 155 passed, 0 failed, 0 validation errors. Thanks Flyander and wavehs. Noted the #4557 call-site overlap for coordination.

AI-assisted (OpenCode)

@DiazE117

Copy link
Copy Markdown
Contributor

Tested b17551c0 (the head after the needs-work review) on Windows 11 Pro 10.0.26320, NVIDIA GeForce RTX 4070 SUPER, driver 610.88, MSVC 19.44, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0: pass.

  • Clean build of the PR head (910 behind main, built as is): 423/423 steps, no new warnings in the changed files.
  • ctest with LEONIDA_VK_VALIDATION=1: 147/147 passed (9 skipped as usual, gameinput disabled), 0 VUIDs in the log. queue_holds_release, vk_fence_dispatcher_sync, d3d12_multithread and d3d12_vulkan_device all pass.
  • queue_holds_release_test.exe run 50 times in a row: 50/50 pass.
  • Negative control: the same test built against queue_holds.h from the previous head 299c98f1 exits 4 (red), and 0 again with this head's header.

Code read of the new remove_fence(): publishing removals_ before draining, then dropping under pump_mutex_ while the fence is still in fences_, then erasing, closes the post-drain window @wavehs reproduced: no begin_hold() can open between the drain and the erase. Both brackets in vulkan_backend.h are taken before state_mutex_ and released after it, so the order reservation → state is the same everywhere. Two things I'd still note, neither blocking:

  • @wavehs's other findings are not in this diff: the header's "no caller waits for another's recording" now has its exception written down, but the matching text in docs/d3d12_vulkan.md is unchanged; remove_fence() has no early-out when nothing is held, so every fence destroy waits for any ExecuteCommandLists in flight (the bracket covers execute_now); and the pre-existing discard_waiting_on gap (held Signals, unsubmitted waits_) stays open.
  • A remove_fence() reached from a thread that holds a Reservation would wait for itself. I found no such path today (~VulkanFence is the only caller and runs on the app's Release), but a comment on reserve() saying so would keep it that way.

No game files. AI-assisted (Claude Code).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested b17551c on Windows 11, MSVC 19.44 (Intel UHD 770 machine; the test is CPU-only): pass. Built queue_holds_release_test; ctest -R queue_holds_release passes (1/1). The red-before run on main was not repeated here. AI-assisted.

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor

Tested b17551c0 on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC (translated-shader D3D12 gate tests included), full LEONIDA_VK_VALIDATION=1 ctest -j4: 146/146 passed, 0 VUIDs, 0 build errors. AI-assisted (Claude Code).

@editrice

Copy link
Copy Markdown

Integration finding for #4740 (D3D-496, stacked on #4663): this PR's removals_/reservation barrier in runtime/src/queue_holds.h:52-77 closes the pending()→hold() fence-release race. But this head's VulkanQueue::discard_waiting_on (runtime/src/vulkan_backend.h:2084-2097) erases held batches, and runtime/test/queue_holds_release_test.cpp:115 asserts fake.dropped. #4740's host-backed case requires the held batch to survive, with only the released fence wait removed, then complete and reach Signal 101. Please preserve this PR's barrier when integrating #4740, while updating discard_waiting_on and the deterministic race test to assert the batch runs after that wait is stripped. Resolving only the file conflict could reopen the race or restore the host mismatch. AI-assisted review; no game files used.

@sds-228

sds-228 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Integration check only, no head change. Local merge with #4557: queue_holds 2/2, d3d12_queue_handover 1/1 pass. Local merge with #4663/#4740: queue_holds_release 1/1, d3d12_queue_handover 1/1 pass, 0 validation errors; erase-only negative control fails return 1. Both integrations need the barrier kept and the released wait stripped from held batches, not the batch dropped.nnAI-assisted (OpenCode)

@memorypressure

Copy link
Copy Markdown

Static review on a clean merge with current main — the C++ race test cannot be built here. The reservation is correctly acquired before the per-queue lock and released after, bracketing the hold decision as documented. One reliability note on the second race case: the 50 ms sleep does not prove the newcomer reached the reservation attempt before removal was allowed to proceed — a delayed thread can make a broken implementation pass; a handshake (event or condvar rendezvous at the reservation attempt) would make the case sound. Checks pending, and they are the functional verdict here. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.