d3d12_vulkan: a title texture destroyed inside the registry is removed after the registry lets go (RT-TITLE-TEXTURE-GONE) #1798
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!1798
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1798/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ImLele69 on GitHub · 2026-11-25 12:16 UTC · original
ImLele69:rt-title-texture-gone→main· +43 −6 · 1 files · 2 commits · merged 2026-11-25 18:42 UTCClaimed in the coordination issue as: RT-TITLE-TEXTURE-GONE — #1608
What and why
~ResourcecallsDeviceState::title_texture_gone, which takestitle_texture_mutex. The comment above the registry already says the caller of a removal holds that mutex while the resource is destroyed. But the last reference to a slot-70 texture can be a descriptor thatwrite_placed_texture_viewoverwrites while it holds the mutex:place_title_view→write_title_srv→CreateShaderResourceViewreleases the old view's resource, its destructor runs, and the same thread takes the samestd::mutexagain.MSVC's
std::mutexdetects that and throwsstd::system_error(resource_deadlock_would_occur). Under Wine,msvcp140.dlldoes not implementstd::_Throw_Cpp_error, so the process aborts instead:Startup trace exception record, with a linker map of that build:
kernelbase<MSVCP140< … <~Resource<Device::CreateShaderResourceView<write_title_srv<write_placed_texture_view<proxy_create_placed_view_x<proxy_slot<70>, on a title thread during the story load (16,808 registered images).Change
title_texture_gonecheckst_in_title_textures, which is set exactly while a thread is inside the registry holdingtitle_texture_mutex(InTitleTextures). There, the entry only stops naming the resource (resource = nullptr, no erase, so no iterator of a caller walkingtitle_texturesis touched), and(resource, address)goes totitles_gone.drain_titles_goneremoves those entries and their GPU placements with the mutex taken normally, right afterwrite_placed_texture_view's registry block and afterrefresh_title_texturesunlocks.place_title_viewtreats an entry without a resource as absent.Outside the registry nothing changes.
Tested
gta6rt.execross-built (clang-cl 20 + xwin) from this branch's commit onmain0d80bc5df.c7e82e26ewith local changes, and0d80bc5dfexactly as in this branch) ran to the world start (36,000-40,480 registered images) without it. They end on the known world-start device loss, which is unrelated. The race needs a slot-70 view overwritten while it holds the last reference, so one run without the abort proves little on its own; six do not prove it either, but the abort did not come back.std::system_errorout ofCreateShaderResourceViewrather than an abort.AI-assisted: yes (Claude Code).
@QemmHD on GitHub · 2026-11-25 13:56 UTC · original
Review (code read; tests on an RTX 4070 SUPER to follow): there's a narrow use-after-free window in the deferred path.
write_placed_texture_view, frees texture X inside thetitle_texture_mutexsection. With this change, X'sgpu_placementsentry stays untildrain_titles_gone.fix_console_slots→bind_title_image, andmatching_gpu_placement), holds onlydescriptor_mutexand can reachmatches(*X)on the freed X before the drain.~Resourcealready takesdescriptor_mutexunconditionally (image_owner/forget_image), so the freeing thread never holds it when the deferral kicks in.Suggested fix: remove X from
gpu_placementsimmediately in the fast path, underdescriptor_mutex, and defer only thetitle_textureserase, which is the part that needs the registry lock to be released. Then nothing reachable from the submit path points at freed memory.AI-assisted (Claude Code, Claude Opus 5.5).
@potatoe0100 on GitHub · 2026-11-25 13:58 UTC · original
Tested on Windows 11 (26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, merged onto
main384abe5b: the 24 title/texture tests (d3d12_title_va*,d3d12_title_*_window,d3d12_placed_heap_lifetime,d3d12_texture_uav,title_window_plan, ...) pass. None of them reaches the re-entrant destroy, so this is mostly a read of the diff.One crash in addition to @QemmHD's point: entries with
resource == nullptrare now visible tolog_title_censusbeforedrain_titles_goneruns. It is called inside both registry blocks, inwrite_placed_texture_view(64th and every 2048th view) and inrefresh_title_textures(every 10 s), both before the drain. Its loop doesand
set_resourcedereferencesr(r->image_backing_bytes()). The entries this PR nulls are the non-owned slot-133 ones, which can haveunbound_bytesset. Suggested fix:if (!t->owned && t->resource && t->unbound_bytes). The other walkers I checked already skip a null resource (visitin the refresh pass, the mapped-texture listing, the UAV path'scandidate,retire_title_texture).Minor: the comment above
title_texture_snapshotinrefresh_title_texturesstill says the registry mutex is recursive for its owner on Windows, which this PR shows is not the case.AI-assisted (Claude).
@aslan2142 on GitHub · 2026-11-25 14:42 UTC · original
Thanks, @ImLele69: the self-deadlock is real on
main(write_placed_texture_viewandrefresh_title_texturesholdtitle_texture_mutexwhen~Resourcecallstitle_texture_gone). Two things before it can merge:gpu_placementsuntildrain_titles_gone, but~Resourcestill finishes and frees it.bind_title_imagereadsgpu_placementsunderdescriptor_mutexalone, from any thread, so it can read the freed resource; the fill worker's locked blocks never drain, which keeps the window open longer. Could you defer the delete itself until the drain, or remove the placement at once whendescriptor_mutexisn't held?mainand passing here.Minor:
refresh_title_textureskeepst_in_title_texturesset afterlock.unlock(), and its comment about a recursive mutex is out of date.AI-assisted (Claude Code, Claude Opus 5.5).
@ImLele69 on GitHub · 2026-11-25 15:59 UTC · original
Thanks @QemmHD, @potatoe0100 and @aslan2142. All three points are right.
152cee255fixes them:title_texture_gonenow removes the placement fromgpu_placementsat once, underdescriptor_mutex. Only thetitle_textureserase still waits fordrain_titles_gone, so nothing the submit path can reach points at the freed resource. Takingdescriptor_mutexthere is safe because the freeing thread never holds it:~Resourcetakes it itself (image_owner,forget_image), as QemmHD noted. The drain no longer touchesgpu_placements.log_title_census: it skips an entry without a resource (!t->owned && t->resource && t->unbound_bytes).refresh_title_textures: theInTitleTexturesscope ends beforelock.unlock(), so the drain and theRelease()calls after it run outside the registry. The comment about a recursive mutex is replaced.It cross-builds with clang-cl, and the same change is in the tree I'm booting under Wine now.
The test: I haven't written it yet. I want it to go through the real re-entrant path (the last reference released by a descriptor that
write_placed_texture_viewoverwrites), and I haven't yet found a way to build that without a title-written heap. Ifd3d12_existing_heap_test'scheck_placed_texture_viewsis the right place (twowrite_placed_texture_viewcalls on the same address with different descriptions, with a timeout), I'll add it there and check that it fails onmain.AI-assisted (Claude Code).
@QemmHD on GitHub · 2026-11-25 18:42 UTC · original
Merging.
Why merged
resource_deadlock_would_occur, an abort under Wine).gpu_placementsimmediately, underdescriptor_mutex, which that thread never holds. Its registry entry stops naming it untildrain_titles_goneerases it after the registry lets go.Why it's good
152cee255.log_title_censusand the refresh skip such entries.Follow-ups welcome
write_placed_texture_view.AI-assisted (Claude Code, Claude Opus 5.5).