fix(gpu-d3d12): don't sentinel-0 a slot whose command list was already submitted - #16
Merged
Merged
Conversation
…y submitted
RecordTimestamp ran ExecuteCommandLists, then on a failed Signal stored the
"never submitted" sentinel (slot.fenceValue = 0) and returned. But the
command list was already in flight on the GPU. The next ring wrap's
pre-check (slot.fenceValue > 0 && GetCompletedValue() < fenceValue) then
treated the slot as free and Reset() its command allocator while the GPU was
still executing it -- a use-after-free the D3D12 debug layer flags and that
release builds can TDR the host over.
Tag the slot with nextFenceValue unconditionally and gate reuse on the fence
(the queue is in-order):
* transient Signal failure: a later frame's Signal advances the fence past
nextFenceValue, so GetCompletedValue() >= nextFenceValue proves the work
is done; the slot reuses safely and PollResolved can still resolve the
already-written timestamp;
* device removed (TDR): GetCompletedValue() returns UINT64_MAX, so reuse is
allowed and the allocator Reset just fails harmlessly on the dead device;
* no later Signal ever lands: the slot stays Pending and is counted as a
drop when the ring overwrites it -- never a Reset() of in-flight work.
This is the same invariant Reset() already relies on (it keeps non-zero fence
values when its bounded drain times out). The other slot.fenceValue = 0 sites
remain correct: init, the pre-submit Reset/Close failure paths, the
drain-succeeded branch of Reset(), and shutdown.
The D3D12 backend has no unit test (it needs a real device); covered by the
CI build + review, like the rest of gpu_timer.cpp.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Finding #4 (code review): D3D12 failed-
Signaluse-after-freeRecordTimestamprunsExecuteCommandLists(work now in flight on the GPU), then on a failedSignalstored the "never submitted" sentinel (slot.fenceValue = 0) and returned. The next ring wrap's pre-check —— treats
fenceValue == 0as free, so itReset()s the slot's command allocator while the GPU may still be executing that command list. That's a use-after-free the D3D12 debug layer flags and release builds can TDR the host over. (Trigger is narrow: a transientSignalfailure —E_OUTOFMEMORY— without device removal, then a wrap before the GPU drains.)Fix
Tag the slot with
nextFenceValueunconditionally and gate reuse on the fence (the queue is in-order):SignalfailureSignaladvances the fence pastnextFenceValue→GetCompletedValue() >= nextFenceValueproves the work is done → safe reuse, andPollResolvedcan still resolve the already-written timestampGetCompletedValue()returnsUINT64_MAX→ reuse allowed, allocatorResetfails harmlessly on the dead deviceSignalever landsPending, counted as a drop when the ring overwrites it — never aReset()of in-flight workThis is the same invariant
Reset()already relies on (it deliberately keeps non-zero fence values when its bounded drain times out — lines 577-586). The otherslot.fenceValue = 0sites stay correct: init, the pre-submit Reset/Close failure paths, the drain-succeeded branch ofReset(), and shutdown.Verification
gpu_timer.cpp.🤖 Generated with Claude Code