Skip to content

vulkan: a fence counter of UINT64_MAX no Signal asked for is a loss, without vkDeviceWaitIdle (RT-FENCE-MAX-CONFIRM-NO-STALL) - #2365

Open
HasiKe wants to merge 3 commits into
ProjectLeonida:mainfrom
HasiKe:fence-max-no-stall
Open

HasiKe wants to merge 3 commits into
ProjectLeonida:mainfrom
HasiKe:fence-max-no-stall

Conversation

@HasiKe

@HasiKe HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

Item: RT-FENCE-MAX-CONFIRM-NO-STALL (claim and analysis in #1608)

What and why

VulkanFence::completed (runtime/src/vulkan_backend.h) handles the NVIDIA case where a lost device first shows as a timeline counter of UINT64_MAX. It confirmed that with vkDeviceWaitIdle under the submit lock.

In a cold world-load boot on an RTX 2070 SUPER (NVIDIA 616.56), that call blocked for 270 s: submit-lock: held 270089 ms by thread 17780, t=200.490..470.579 s. I resolved the runtime's own submit-lock stack dump against the build's PDB:

  • The holder is the list-value pump: Pump::run → take_landed → VulkanFence::completed → vkDeviceWaitIdle.
  • The releaser waits for the lock: Pump::release_loop → Fence::Signal → VulkanQueue::release_held → QueueHolds::pump_ready → execute_now → SubmitMutex::lock, in the middle of submitting a held batch.
  • So does another VulkanFence::completed reader, and a compile worker.

Nothing could submit for 4.5 minutes, so the title froze. The loss, and the LEONIDA_DEVICE_FAULT report, came 270 s after the fault, so that report describes the device after the wait.

QueueHolds::submitted_value(semaphore) already knows the highest value anyone asked of the fence: its initial value (add_fence), CPU Signals (signaled) and submitted Signals. This PR uses that:

  • Below UINT64_MAX: no Signal can have produced a counter of UINT64_MAX, so the device is marked lost at once ("a fence's counter read UINT64_MAX, a value no Signal asked for"). No vkDeviceWaitIdle and no submit lock.
  • At UINT64_MAX: the reading is the value, and nothing is recorded.

Behaviour change, for @quartz-heron-58 (author of 35e38ba)

Case 3 of vk_fence_lost_by_counter was "UINT64_MAX on a device that is not lost records no loss when vkDeviceWaitIdle succeeds". It is now "UINT64_MAX on a fence a Signal asked that value of records no loss". Two open questions from me:

  1. Was the old case 3 a measured driver case, i.e. a counter of UINT64_MAX with no Signal for it on a device that was fine? If it was, this needs another way to tell the two cases apart.
  2. The removed comment said vkDeviceWaitIdle "is also what a driver needs to have returned before vkGetDeviceFaultInfoEXT answers". With this change, a loss found this way no longer goes through a VK_ERROR_DEVICE_LOST result, so the LEONIDA_DEVICE_FAULT report is not triggered from here. If you want the report kept, I can add a vkDeviceWaitIdle after the loss is marked, only with the fault diagnostics on. It then stalls only diagnostic runs, and by then the context is already lost.

Tests

vk_fence_lost_by_counter, rewritten:

  1. UINT64_MAX with no Signal for it marks the loss once and never calls vkDeviceWaitIdle.
  2. A second read on the lost device asks the driver nothing.
  3. UINT64_MAX on a fence with a recorded Signal(UINT64_MAX) records no loss and does not call vkDeviceWaitIdle.
  4. An ordinary value never reaches it.
  5. New: a reading while another thread holds the submit lock still answers and marks the loss.

Results:

The 270 s stall itself came from one boot in about five, so I have not shown in a game boot that it is gone. I'll run more cold world-load boots with this change and report there.

Environment: RTX 2070 SUPER, NVIDIA 616.56, Windows 11 Pro 26300, MSVC (VS 18) RelWithDebInfo via scripts/build_runtime.ps1 -FetchSdl3. No game files in the change or the test.

AI-assisted (Claude Code).

…without vkDeviceWaitIdle

VulkanFence::completed confirmed a UINT64_MAX counter with vkDeviceWaitIdle under the submit lock. On an
RTX 2070 SUPER (NVIDIA 616.56) that call blocked for 270 s in a world-load boot while the list-value
releaser waited for the same lock to submit a held batch: the title froze and the loss was reported
minutes after it happened.

QueueHolds::submitted_value already records the highest value anyone asked of a fence (its initial
value, CPU Signals and submitted ones). Below UINT64_MAX, no Signal can have produced the reading, so
the device is marked lost at once; at UINT64_MAX the reading is the value. vk_fence_lost_by_counter
covers both, a second read on the lost device, and a read while another thread holds the submit lock.

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

Built the exact PR head with MSVC on Windows 11, and vk_fence_lost_by_counter passed on NVIDIA GeForce RTX 5070 (610.88). A scratch-only test of a real CPU timeline signal found a false device-loss classification in a concurrent counter read. Details inline.

AI-assisted.

Comment thread runtime/src/vulkan_backend.h Outdated
// That is decided without vkDeviceWaitIdle: under the submit lock it can block for minutes on such a
// device (measured 270 s on NVIDIA 616.56), and meanwhile the releaser cannot submit the held batch a
// queue's Wait may be waiting for, so the title froze and the loss was reported minutes late.
if (context_.holds().submitted_value(semaphore_) != UINT64_MAX)

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] Account for a legitimate max-value signal while publication is in flight.

signal() calls vkSignalSemaphore before it updates QueueHolds. I used the existing interposition harness to pause a CPU signal to UINT64_MAX just after the real Vulkan call succeeded, then read the same fence on another thread before the wrapper returned. The fence went from UINT64_MAX - 1 to UINT64_MAX on the real device, and the reader marked the healthy device lost because the bookkeeping still held UINT64_MAX - 1. The queue-submit path also publishes after the Vulkan call. Track an in-flight max signal or synchronize that narrow window without waiting for device idle.

@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown
Author

One game boot with this PR. AI-assisted (Claude Code).

Runtime: main fbf100b1b (which has #2195) plus this PR. Recipe: RTX 2070 SUPER, NVIDIA 616.56, a cold boot (no caches), LEONIDA_ASYNC_PIPELINES=1, LEONIDA_COMPILE_GATE=0, LEONIDA_DEVICE_FAULT=2, the watchdog kicked, 660 s.

Result:

  • The new path fired. The log says the device was lost (a fence's counter read UINT64_MAX, a value no Signal asked for).
  • The report follows at once. The next submission gets VK_ERROR_DEVICE_LOST from vkQueueSubmit (present), and the fault report is taken at t=304.8 s. The report says submit lock held by thread 2680 for 0 ms.
  • No stall. There is no submit-lock: held or device-stall line anywhere in the log. The boot this PR comes from had the lock held for 270 s at this point.
  • The timing is right. nvlddmkm 153 came at 16:46:49. The boot started at 16:41:43, so the driver reset and the reported loss fall in the same second, rather than 270 s apart.
  • The report now shows the real fault:
    • 45 instruction-pointer addresses, so waves were still running.
    • The direct queue is at a draw: _MS_VS_TransformD_c5fd36bb_Wrapped~153 + %PS_DeferredTextured_942fcba4_Wrapped~30.
    • The compute queues are at lib_cs_01468, command 16. That is the same shape as the GPU-hang boots in Coordinating who works on what (continued from #14) #1608, now without the wait in front of it.

This is one boot. The 270 s stall needs the same timing to come back, so this does not show the stall can never happen. It does show that the changed branch is taken in a real loss, and that the report then comes promptly and points at the hang.

@quartz-heron-58

Copy link
Copy Markdown
Contributor

Answers to the two questions, as author of 35e38ba (AI-assisted, Claude Code):

  1. Case 3 was not a measured driver case. What I measured (RTX 4080, NVIDIA 621.24) was only the lost case: vkGetSemaphoreCounterValue succeeding with UINT64_MAX, and vkWaitSemaphores succeeding on it, before any call had returned VK_ERROR_DEVICE_LOST. I never saw UINT64_MAX on a healthy device with no Signal for it. Case 3 was a synthetic guard (interposed driver table) so that the value alone was not taken as proof. Deciding from what was asked of the fence covers the same concern, so I have no objection to dropping the vkDeviceWaitIdle. The window lixonik describes (the Vulkan signal done, the bookkeeping not yet updated) is the one place where the new rule can call a healthy device lost, so that needs closing.

  2. The sentence about vkGetDeviceFaultInfoEXT comes from the same machine: there the driver did not answer the fault query until some call had returned VK_ERROR_DEVICE_LOST. I measured that with vkDeviceWaitIdle as the call; I did not test whether a later vkQueueSubmit returning the error serves equally. Your boot above suggests it does on 616.56 (the report follows the next submission). The case not covered is a loss found here when nothing submits afterwards, for example a title that stops on reading the value. A vkDeviceWaitIdle only with the fault diagnostics on, after the loss is marked, would keep the report for that case and stalls nothing in ordinary runs, so yes, I would keep it in that form.

Not checked by me: this PR's head in a game run.

…rt's wait with diagnostics on

Review of ProjectLeonida#2365: VulkanFence::signal, the queue submit that signals a fence and the COM fence callback
record their value with QueueHolds::signaled only after the Vulkan call returns. A counter read on
another thread in that window saw UINT64_MAX with the record one below and called a healthy device lost
(found by lixonik). QueueHolds::asking now announces the value before each of the three calls, and
completed() decides from asked_value, the larger of the two records.

With the device-fault diagnostics on, completed() still calls vkDeviceWaitIdle after marking the loss:
on NVIDIA 621.24 vkGetDeviceFaultInfoEXT answered only once a call had returned VK_ERROR_DEVICE_LOST
(quartz-heron-58). The context is lost by then, so the wait can stall only a diagnostic run.

vk_fence_lost_by_counter case 6: a Signal(UINT64_MAX) announced but not yet recorded is not a loss.
@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown
Author

Thanks to both of you. Pushed in 69b1338f2, on top of a merge of main d5634defe. AI-assisted (Claude Code).

@lixonik's window (P2):

  • QueueHolds::asking(semaphore, value) now records the value before each of the three calls that can signal a D3D12 fence's semaphore:
    • VulkanFence::signal (vkSignalSemaphore);
    • the vkQueueSubmit in VulkanQueue that signals the fence;
    • the COM fence callback's vkSignalSemaphore (d3d12_com.cpp).
  • completed() decides from asked_value, the larger of that record and the existing signaled() one. A reader that sees UINT64_MAX while such a call is still returning therefore finds the value already announced.
  • A failed call leaves the announcement in place. That can only make a later UINT64_MAX count as asked-for, never turn a healthy device into a lost one.
  • The other two vkSignalSemaphore/submit-signal sites in the backend (the fence dispatcher's wakeup semaphore and the queues' progress timelines) are not D3D12 fences; completed() never reads them.
  • New case 6 in vk_fence_lost_by_counter: a Signal(UINT64_MAX) announced but not yet recorded (submitted_value still UINT64_MAX - 1) reads as the value, with no loss and no vkDeviceWaitIdle.

@quartz-heron-58's point 2:

  • With the fault diagnostics on (context.device_fault()), completed() still calls vkDeviceWaitIdle under the submit lock, but only after it has marked the loss, and only the first time.
  • Without diagnostics there is no wait at all, so ordinary runs cannot stall.

Tests:

  • Full runtime CTest, 483 tests: all pass except xbox_present and d3d12_set_descriptor_heaps, the same two that fail on unmodified main here.
  • vk_fence_lost_by_counter passes with all six cases.
  • RTX 2070 SUPER, NVIDIA 616.56, Windows 11, MSVC RelWithDebInfo.

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

Re-tested 69b1338 on RTX 5070 (610.88, Windows 11). The full fence suite and my original concurrent CPU-signal case both passed. The real vkSignalSemaphore reached UINT64_MAX, the reader ran before VulkanFence::signal() published completion, and the device remained healthy. This verifies the earlier finding. I did not exercise a separate GPU-queue interleaving.

AI-assisted.

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

Approving 69b1338.

Merged into current main (d1dff79): only vulkan_backend.h auto-merges, no conflicts. Cross-built with clang-cl 19 + xwin and run under Wine 11.22 on llvmpipe: all 27 fence and queue tests pass (ctest -R "fence|queue_query|presenter_sync|wait_registration"), including vk_fence_lost_by_counter, d3d12_fence_device_lost_counter, d3d12_fence_timing (held CPU signals) and vk_presenter_sync.

Read the diff with lixonik's P2 in mind. Every path that can signal a D3D12 fence's semaphore now announces its value first: VulkanFence::signal, the fence-signalling vkQueueSubmit in VulkanQueue, and the COM fence callback. The two other vkSignalSemaphore/signal sites are the dispatcher's wakeup semaphore and the sparse-bind progress ticket, which completed() never reads. asked_ is cleared with fences_ in remove_fence. Deciding from what was asked instead of a vkDeviceWaitIdle under the submit lock removes the multi-minute stall HasiKe measured, and the wait is kept only for fault diagnostics on an already-lost context.

The updated test uses asking(), so it can't run against main's code as a before/after check; the lixonik and HasiKe runs above cover the real-driver side. AI-assisted (Claude Code, Claude Opus 5.5).

@HasiKe
HasiKe marked this pull request as ready for review November 27, 2026 10:14
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.

4 participants