vulkan: read the retirement records under their own lock (RT-RETIRE-TICKET-LOCK) #2402

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

@⁠wavehs on GitHub · 2026-11-26 19:54 UTC · original
wavehs:retire-ticket-lock → main · +109 −16 · 4 files · 1 commits · open

Claimed in the coordination issue as: #1608
(follow-up to DadaCap's finding: #1608)

Depends on: none.

What and why

VulkanContext::completed_retire_ticket() and wait_retire_ticket() read retire_tickets_ under submit_mutex_, so a caller that only asks what retired waits behind whoever holds that lock — a queue submission, or a vkDeviceWaitIdle under it. That is the hop DadaCap described: the held-work releaser inside a gate Signal and the progress watch both want the completed ticket and neither needs to submit.

retire_tickets_ now has its own innermost retire_mutex_:

  • record_submit_locked and retire_queue_idle_locked take submit_mutex_ -> retire_mutex_;
  • completed_retire_ticket and wait_retire_ticket take retire_mutex_ alone; the wait_submitted_locked snapshot takes it under the submit_mutex_ its caller already holds;
  • nothing takes retire_mutex_ before submit_mutex_, so the order is unchanged everywhere else;
  • poll_retirement_locked try-locks retire_mutex_ on purpose: a query already in progress watches the same timelines, and waiting for it while holding the submit lock is what this change removes.

Queue teardown is still safe: it marks the queue's entries idle under retire_mutex_ (via retire_queue_idle_locked) before destroying the timeline semaphore, so an in-flight query either finishes first or is skipped because the entry is idle.

Test

New CPU-only ctest retire_ticket_lock (runtime/test/retire_ticket_lock_test.cpp): one thread holds context.submit_mutex() for 500 ms, the main thread calls completed_retire_ticket() and must get ticket 0 in under 200 ms.

=== main 2d43417d4 (only runtime/src/vulkan_backend.h reverted, same build directory)
completed_retire_ticket returned 505ms after a submit lock was held (ticket 0)
line 55: waited < 200
NEG_EXIT=1

=== with the change
completed_retire_ticket returned 0ms after a submit lock was held (ticket 0)
retire_ticket_lock_test: passed
POS_EXIT=0

Tested

macOS 27.2.1 arm64 (Apple clang, CMake/Ninja, RelWithDebInfo), portable runtime build, no GPU and no game files:

  • ctest after a full build: 114/114 passed (63-66 s). retire_tickets and replay_pipeline_compile_unlock are in that run; submit_mutex is Windows-only and is not part of the portable set.
  • python3 -I tests/test_ctest_registration.py -> 6 tests, OK.
  • python3 -I scripts/check_no_game_data.py on the four changed files -> exit 0.
  • python3 -m compileall -q make_game.py tools tests -> OK.

Not built here: the Windows runtime (MSVC) and the title. The change is portable C++17 and the new target links Vulkan::Headers Threads::Threads like its neighbours, but the Windows build should confirm it.

AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-26 19:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2402) `wavehs:retire-ticket-lock` → `main` · +109 −16 · 4 files · 1 commits · open Claimed in the coordination issue as: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 (follow-up to DadaCap's finding: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) Depends on: none. ### What and why `VulkanContext::completed_retire_ticket()` and `wait_retire_ticket()` read `retire_tickets_` under `submit_mutex_`, so a caller that only asks what retired waits behind whoever holds that lock — a queue submission, or a `vkDeviceWaitIdle` under it. That is the hop DadaCap described: the held-work releaser inside a gate `Signal` and the progress watch both want the completed ticket and neither needs to submit. `retire_tickets_` now has its own innermost `retire_mutex_`: - `record_submit_locked` and `retire_queue_idle_locked` take `submit_mutex_ -> retire_mutex_`; - `completed_retire_ticket` and `wait_retire_ticket` take `retire_mutex_` alone; the `wait_submitted_locked` snapshot takes it under the `submit_mutex_` its caller already holds; - nothing takes `retire_mutex_` before `submit_mutex_`, so the order is unchanged everywhere else; - `poll_retirement_locked` try-locks `retire_mutex_` on purpose: a query already in progress watches the same timelines, and waiting for it while holding the submit lock is what this change removes. Queue teardown is still safe: it marks the queue's entries idle under `retire_mutex_` (via `retire_queue_idle_locked`) before destroying the timeline semaphore, so an in-flight query either finishes first or is skipped because the entry is idle. ### Test New CPU-only ctest `retire_ticket_lock` (`runtime/test/retire_ticket_lock_test.cpp`): one thread holds `context.submit_mutex()` for 500 ms, the main thread calls `completed_retire_ticket()` and must get ticket 0 in under 200 ms. ```text === main 2d43417d4 (only runtime/src/vulkan_backend.h reverted, same build directory) completed_retire_ticket returned 505ms after a submit lock was held (ticket 0) line 55: waited < 200 NEG_EXIT=1 === with the change completed_retire_ticket returned 0ms after a submit lock was held (ticket 0) retire_ticket_lock_test: passed POS_EXIT=0 ``` ### Tested macOS 27.2.1 arm64 (Apple clang, CMake/Ninja, RelWithDebInfo), portable runtime build, no GPU and no game files: - `ctest` after a full build: **114/114 passed** (63-66 s). `retire_tickets` and `replay_pipeline_compile_unlock` are in that run; `submit_mutex` is Windows-only and is not part of the portable set. - `python3 -I tests/test_ctest_registration.py` -> 6 tests, OK. - `python3 -I scripts/check_no_game_data.py` on the four changed files -> exit 0. - `python3 -m compileall -q make_game.py tools tests` -> OK. Not built here: the Windows runtime (MSVC) and the title. The change is portable C++17 and the new target links `Vulkan::Headers Threads::Threads` like its neighbours, but the Windows build should confirm it. AI-assisted.
completed_retire_ticket() and wait_retire_ticket() took submit_mutex_ to read
retire_tickets_, so asking what retired waited behind any submission, including
a vkDeviceWaitIdle under the same lock. retire_tickets_ now has an innermost
retire_mutex_: submissions and queue-idle marking take submit -> retire, while
the retirement reads take retire alone. The progress watch try-locks retire so a
stuck query cannot keep it holding the submit lock. New retire_ticket_lock ctest:
a submit lock held for 500 ms, completed_retire_ticket() must return under 200 ms.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:00:28 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-26 20:17 UTC · original

Native Windows 11 x64, MSVC 19.44 Release: gta6rt.exe built at PR head 35eb7b4a40. All four focused CPU tests passed, including the Windows-only submit_mutex, plus retire_ticket_lock, retire_tickets and replay_pipeline_compile_unlock.

I also checked the change with two outstanding submissions on an RTX 4090 (driver 616.64). The same synthetic probe ran three times on the PR and three times on its parent 2d43417d49. Each submission waited for its own host gate value and signaled a real Vulkan timeline. The reader ran on a separate thread while the main thread held context.submit_mutex(). I waited for the GPU counter to reach each released value independently.

GPU timeline state Parent PR
Neither submission complete Poll returned 0 after unlock Poll returned 0 while locked
First complete, second still gated Poll returned 1 after unlock Poll returned 1 while locked
Waiting for ticket 2, then releasing its gate Wait returned 2 after unlock Wait stayed pending before the signal, then returned 2 while locked

All three runs agreed on each revision. On the parent, each reader remained blocked throughout the 400 ms observation window. This covers both the query and wait paths with live semaphore values. The stock vk_queue_test also passed on the PR: copy-to-direct and direct-to-compute readbacks had zero bad bytes. The queue test and all six probe runs recorded zero Vulkan validation errors. The probe uses synthetic submissions and measures this lock dependency only.

**@⁠ecox982745** on GitHub · 2026-11-26 20:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2402#issuecomment-7202509181) Native Windows 11 x64, MSVC 19.44 Release: `gta6rt.exe` built at PR head `35eb7b4a40`. All four focused CPU tests passed, including the Windows-only `submit_mutex`, plus `retire_ticket_lock`, `retire_tickets` and `replay_pipeline_compile_unlock`. I also checked the change with two outstanding submissions on an RTX 4090 (driver 616.64). The same synthetic probe ran three times on the PR and three times on its parent `2d43417d49`. Each submission waited for its own host gate value and signaled a real Vulkan timeline. The reader ran on a separate thread while the main thread held `context.submit_mutex()`. I waited for the GPU counter to reach each released value independently. | GPU timeline state | Parent | PR | | --- | --- | --- | | Neither submission complete | Poll returned 0 after unlock | Poll returned 0 while locked | | First complete, second still gated | Poll returned 1 after unlock | Poll returned 1 while locked | | Waiting for ticket 2, then releasing its gate | Wait returned 2 after unlock | Wait stayed pending before the signal, then returned 2 while locked | All three runs agreed on each revision. On the parent, each reader remained blocked throughout the 400 ms observation window. This covers both the query and wait paths with live semaphore values. The stock `vk_queue_test` also passed on the PR: copy-to-direct and direct-to-compute readbacks had zero bad bytes. The queue test and all six probe runs recorded zero Vulkan validation errors. The probe uses synthetic submissions and measures this lock dependency only.
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 20:43 UTC · original · reviewed

On Windows 11 with MSVC, retire_ticket_lock, retire_tickets, submit_mutex and replay_pipeline_compile_unlock all passed. The new test measured 0 ms for the retirement read with the submit lock held. A separate synthetic counter test found a watchdog regression on the try-lock path. Details inline.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 20:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2402#pullrequestreview-6080300235) · **reviewed** On Windows 11 with MSVC, `retire_ticket_lock`, `retire_tickets`, `submit_mutex` and `replay_pipeline_compile_unlock` all passed. The new test measured 0 ms for the retirement read with the submit lock held. A separate synthetic counter test found a watchdog regression on the try-lock path. Details inline. AI-assisted.
@ -2230,1 +2239,4 @@
// timelines, and waiting for it here would keep the submit lock, and every submission, behind a stuck query.
std::uint64_t poll_retirement_locked(VkResult& result) {
std::unique_lock lock(retire_mutex_, std::try_to_lock);
if (!lock.owns_lock()) return 0; // report no progress this tick; the next one reads it
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 20:43 UTC · original

[P2] Keep a skipped retirement poll out of the hang watchdog's pending samples. With a fake Vulkan counter held by completed_retire_ticket(), this branch returned 0 with VK_SUCCESS while submitted_retire_ticket() was 1. The in-flight query then returned completed ticket 1. The watch loop classifies that unmeasured sample as pending; feeding it two such samples 15 seconds apart triggered SubmissionProgressWatch's stall result in the focused test. Under sustained contention the same classification also feeds the opt-in LEONIDA_HANG_EXIT_S process-exit path. Can the poll return an explicit skipped result so the watch loop only advances its pending timer on a real measurement?

**@⁠lixonik** on GitHub · 2026-11-26 20:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2402#discussion_r4713211771) [P2] Keep a skipped retirement poll out of the hang watchdog's pending samples. With a fake Vulkan counter held by `completed_retire_ticket()`, this branch returned 0 with `VK_SUCCESS` while `submitted_retire_ticket()` was 1. The in-flight query then returned completed ticket 1. The watch loop classifies that unmeasured sample as `pending`; feeding it two such samples 15 seconds apart triggered `SubmissionProgressWatch`'s stall result in the focused test. Under sustained contention the same classification also feeds the opt-in `LEONIDA_HANG_EXIT_S` process-exit path. Can the poll return an explicit skipped result so the watch loop only advances its pending timer on a real measurement?
Author
Owner

@⁠timbot103 on GitHub · 2026-11-27 12:09 UTC · original

Independently checked head 35eb7b4a4 on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), Vulkan SDK 1.4.363.0, WSL llvm-mc 21.1.8, no game files.

Local build & runtime verification:

  • Clean MSVC build of retire_ticket_lock_test, retire_tickets_test, and d3d12_m4_compute_test (zero new warnings in PR files).
  • retire_ticket_lock_test: 5/5 consecutive runs PASS (completed_retire_ticket returned 0ms after a submit lock was held).
  • retire_tickets_test: PASS (out-of-order queue completion kept the retirement prefix safe).
  • d3d12_m4_compute_test (LEONIDA_VK_VALIDATION=1): 1924/1924 words matched (error 0) on both NVIDIA GeForce RTX 5080 (616.64) and AMD Radeon(TM) Graphics (25.10.30.02), 0 validation errors.

Static lock & lifetime findings on 35eb7b4a4:

  • Lock ordering checks out: all six retire_tickets_ accesses hold retire_mutex_, and every nested path acquires submit_mutex_ before retire_mutex_ (no AB-BA inversion found).
  • 1. Ticket N is published before its retirement record exists (runtime/src/vulkan_backend.h:2058-2064, 1992-2013; caller d3d12_vulkan_device.cpp:5051-5064):
    • The submitter publishes ticket N via fetch_add before acquiring retire_mutex_ to insert the retirement record. If N-1 has already retired, a concurrent waiter can read N, snapshot an empty retire_tickets_ list, skip vkWaitSemaphores, and return N-1 while submission N is still outstanding on the GPU (the old shared submit_mutex_ prevented this window; console-heap teardown ignores the returned prefix before destroying its buffer).
    • Fix: publish ticket N under retire_mutex_ after installing its record (and add a test holding N's timeline unsignaled to prove wait_retire_ticket(N) cannot return early).
  • 2. Failed queue-idle teardown destroys semaphores without retire_mutex_ (runtime/src/vulkan_backend.h:2024-2033, 2631-2645, 2661-2665, 1993-2012):
    • On a failed queue-idle path, teardown skips retire_mutex_ before destroying the semaphore even though a counter query already inside retire_mutex_ can still be reading it. (Separately, on successful idle, wait_retire_ticket copies raw semaphore handles and waits outside the lock, which is an inherited teardown lifetime window.) Synchronizing failed teardown with in-flight queries and pinning timelines across waits closes both.
  • 3. Contended try_lock in the progress sample can feed a false hang exit (runtime/src/vulkan_backend.h:2240-2248, 2281-2286, 2336-2353):
    • A missed retire_mutex_ try-lock returns 0 with VK_SUCCESS, which the watchdog treats as pending unretired work after any submission; repeated misses can trip LEONIDA_HANG_EXIT_S on an idle GPU. Representing an unavailable sample explicitly so it does not advance the hang timer avoids false triggers.

AI-assisted.

**@⁠timbot103** on GitHub · 2026-11-27 12:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2402#issuecomment-7217907773) Independently checked head `35eb7b4a4` on Windows 11 Pro 26200, MSVC 2022 (`19.44.35230.0`, RelWithDebInfo, Ninja), Vulkan SDK `1.4.363.0`, WSL `llvm-mc 21.1.8`, no game files. **Local build & runtime verification:** - Clean MSVC build of `retire_ticket_lock_test`, `retire_tickets_test`, and `d3d12_m4_compute_test` (zero new warnings in PR files). - `retire_ticket_lock_test`: **5/5 consecutive runs PASS** (`completed_retire_ticket returned 0ms after a submit lock was held`). - `retire_tickets_test`: **PASS** (`out-of-order queue completion kept the retirement prefix safe`). - `d3d12_m4_compute_test` (`LEONIDA_VK_VALIDATION=1`): **1924/1924 words matched (error 0)** on both **NVIDIA GeForce RTX 5080** (`616.64`) and **AMD Radeon(TM) Graphics** (`25.10.30.02`), 0 validation errors. **Static lock & lifetime findings on `35eb7b4a4`:** - **Lock ordering checks out:** all six `retire_tickets_` accesses hold `retire_mutex_`, and every nested path acquires `submit_mutex_` before `retire_mutex_` (no AB-BA inversion found). - **1. Ticket `N` is published before its retirement record exists (`runtime/src/vulkan_backend.h:2058-2064, 1992-2013`; caller `d3d12_vulkan_device.cpp:5051-5064`):** - The submitter publishes ticket `N` via `fetch_add` before acquiring `retire_mutex_` to insert the retirement record. If `N-1` has already retired, a concurrent waiter can read `N`, snapshot an empty `retire_tickets_` list, skip `vkWaitSemaphores`, and return `N-1` while submission `N` is still outstanding on the GPU (the old shared `submit_mutex_` prevented this window; console-heap teardown ignores the returned prefix before destroying its buffer). - **Fix:** publish ticket `N` under `retire_mutex_` after installing its record (and add a test holding `N`'s timeline unsignaled to prove `wait_retire_ticket(N)` cannot return early). - **2. Failed queue-idle teardown destroys semaphores without `retire_mutex_` (`runtime/src/vulkan_backend.h:2024-2033, 2631-2645, 2661-2665, 1993-2012`):** - On a failed queue-idle path, teardown skips `retire_mutex_` before destroying the semaphore even though a counter query already inside `retire_mutex_` can still be reading it. (Separately, on successful idle, `wait_retire_ticket` copies raw semaphore handles and waits outside the lock, which is an inherited teardown lifetime window.) Synchronizing failed teardown with in-flight queries and pinning timelines across waits closes both. - **3. Contended `try_lock` in the progress sample can feed a false hang exit (`runtime/src/vulkan_backend.h:2240-2248, 2281-2286, 2336-2353`):** - A missed `retire_mutex_` try-lock returns `0` with `VK_SUCCESS`, which the watchdog treats as pending unretired work after any submission; repeated misses can trip `LEONIDA_HANG_EXIT_S` on an idle GPU. Representing an unavailable sample explicitly so it does not advance the hang timer avoids false triggers. AI-assisted.
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/2402/head:github-bot/gh-2402'
git switch 'github-bot/gh-2402'

Merge

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