Skip to content

Fix deferred fence signal callback lifetime (TEST-1017) - #4725

Open
illusionart453-max wants to merge 1 commit into
ItsNotRexed:mainfrom
illusionart453-max:diagnose/test-1017
Open

illusionart453-max wants to merge 1 commit into
ItsNotRexed:mainfrom
illusionart453-max:diagnose/test-1017

Conversation

@illusionart453-max

Copy link
Copy Markdown

A deferred CPU Fence::Signal callback can leave FenceWaitDispatcher's pending list before the fence is released. cancel_fence_waits waits for an active Vulkan wait, but a ready callback can still query or signal the semaphore after VulkanFence destroys it. This caused intermittent access violations during d3d12_fence_timing teardown.

Add shared callback lifetime state. Fence destruction marks it released under the same mutex held while deferred callbacks access the semaphore. Active callbacks finish before destruction continues, and later callbacks skip semaphore access. Add stderr phase diagnostics to the timing test.

Claimed as TEST-1017
#4571 (comment)

Verified locally on Windows 11, NVIDIA RTX 5060, driver 610.88, MSVC 19.44, RelWithDebInfo, base 0db7fed.

  • Before the fix, 1 of 20 repeated runs crashed with 0xC0000005.
  • A local ProcDump/CDB capture placed the fault inside nvoglv64, called by the deferred Fence::Signal lambda through FenceWaitDispatcher::dispatch_entries.
  • After the fix, 100 of 100 repeated timing-test runs passed.
  • git diff --check passed.

Vulkan validation layers and an independent GPU check are pending. The crash dump remains local.

AI-assisted with Codex.
Depends on no other PR.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified commit 3916c46e locally on Windows 11 x64 (AMD Radeon RX 6700 XT, RelWithDebInfo):

  • Built d3d12_fence_timing_test.exe and d3d12_queue_handover_test.exe cleanly ([13/13]).
  • ctest --test-dir build --output-on-failure -R "^(d3d12_fence_timing|d3d12_queue_handover)$" -V:
    • Standard: 2/2 passed (9.33 sec real: d3d12_queue_handover 7.55 sec — layer 371, host AMD Radeon RX 6700 XT 369; d3d12_fence_timing 1.76 sec).
    • LEONIDA_VK_VALIDATION=1: 2/2 passed (9.65 sec real: d3d12_queue_handover 7.81 sec, d3d12_fence_timing 1.83 sec, 0 validation errors on AMD Radeon RX 6700 XT (validation on)).
  • Direct d3d12_fence_timing_test.exe under LEONIDA_VK_VALIDATION=1: exit 0 plus 5/5 consecutive repeat runs passed cleanly.
  • Repo checks: tests/test_ctest_registration.py (4/4 in 0.877s), scripts/check_no_game_data.py exit 0, and git diff --check exit 0.

Reviewed runtime/shims/d3d12_com.cpp and runtime/test/d3d12_fence_timing_test.cpp: synchronizing ~Fence() with the ready deferred callback on callback_lifetime_->mutex before cancel_fence_waits() and VulkanFence destroys the semaphore (and capturing held = held_ / lifetime = callback_lifetime_ by shared_ptr so a delayed callback safely drops the work without touching Fence or context_) cleanly closes the destructor-vs-callback lifetime race.

AI-assisted (Gemini, Antigravity).

@Atomic258

Copy link
Copy Markdown

Tested 3916c46e on Windows 11, NVIDIA GeForce RTX 5090 (driver 617.42), MSVC 19.44/RelWithDebInfo, Vulkan SDK 1.4.363:

  • d3d12_fence_timing_test.exe ×5 consecutive runs: all exit 0, no AV, no validation errors. The stderr phase diagnostics print cleanly through teardown.
  • Sibling coverage on the same build: d3d12_queue_wait_test, d3d12_fence_events_test, vk_fence_dispatcher_sync_test — all exit 0.

Not a red control (the bug is an intermittent teardown AV — absence of a crash over 5 runs on a second NVIDIA part is corroboration, not proof). No game files. AI-assisted (Devin).

@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 d3d12_fence_timing_test at 3916c46eb on Windows 11, RTX 5070 Ti: exit 0 — fences and timing: multiple-fence waits, held CPU signals, clock calibration, adapter LUID and custom heap properties all checked (calibration 0.050700 s GPU / 0.050699 s CPU; LUID matches the adapter). git diff --check clean.

The deferred fence-signal callback now holds its state for the callback's lifetime instead of capturing a reference that can expire while the signal is pending; the added case covers the released-fence signal. No blocking issue.

@memorypressure

Copy link
Copy Markdown

Verified on Linux (CPU-only): sits on current main, compileall clean, no Python tests affected. The callback-lifetime guard reads correctly — the mutex orders the deferred callback's semaphore access against ~Fence's released store, both paths drop the held count symmetrically, and the includes are in place. The timing test itself needs a Vulkan/Windows host and was not executed here; CI legs were still queued at check time. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Independent check of 3916c46e: Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51, Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC.

  • Full LEONIDA_VK_VALIDATION=1 ctest -j4: 163/163, 0 VUIDs.
  • d3d12_fence_timing_test with validation, 50 runs in a row: 50/50, no crash.

One regression under validation, found with a release-race probe I added locally to d3d12_fence_timing_test (not pushed; described on #4282). Each of 200 iterations uses a fresh fence F: queue A Wait(gate, i) + Signal(F, 1), CPU F->Signal(2) (postponed), queue B Wait(F, 2), open the gate, busy-wait 0–1.9 ms, then F->Release().

tree (fresh build folders) VUID-vkDestroySemaphore-semaphore-05149
main 7b7d73f8 0 in 8/8 runs
this PR 2–3 per run, 3/3 runs (all from the probe: 0 before it)
this PR + #4663 0cc958d0 0 in 6/6 runs (gated and immediate)

Why: on main, the callback that cancel runs from ~Fence still calls vkGetSemaphoreCounterValue on F. That read is what establishes, for the app and for the validation layer, that queue A's Signal(F, 1) has completed before ~VulkanFence destroys the semaphore. Here, released is set before cancel, so the callback returns without touching F, and the semaphore is destroyed while nothing has shown that queue A's submission is done. #4663 (D3D-486) retires the semaphore through the deferred-deletion tickets, which covers exactly that, and the two merge cleanly. So I'd mark this Depends on: #4663, or land #4663 first.

Heads-up: #4282 edits the same deferred-signal lambda (it pumps QueueHolds after the signal), so whichever lands second will need a small merge there.

(Note for anyone re-running locally: after merging #4663, which changes VulkanContext's layout in vulkan_backend.h, MSBuild's incremental build left other libraries on the old header and the test crashed with VUID-vkFreeMemory-memory-parameter. --clean-first fixed it, so that wasn't a real bug.)

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

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.

6 participants