Skip to content

d3d12: keep the ExecuteCommandLists serial stack out of TLS callbacks (RT-XMEM-STARTUP-FASTFAIL) - #2195

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
brandostrong:fix/geometry-view-tls-callbacks
Nov 26, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
brandostrong:fix/geometry-view-tls-callbacks

Conversation

@brandostrong

Copy link
Copy Markdown
Contributor

Item: RT-XMEM-STARTUP-FASTFAIL (claim: #1608 comment 7182512479).

861bb0546 (#2138) made the title fail-fast at startup: 0xC0000409, info0=0x7, ~1.6 s in, before any D3D12 call. Bisecting the 49 commits from ad41fd517 to 0b11d86bc: ad41fd517 boots, 861bb0546 is the first bad commit, and its only new runtime construct is the thread_local std::vector<std::uint64_t> in geometry_execute_serials().

Cause

A function-local thread_local with a destructor in gta6rt.exe adds TLS callbacks to the image. The loader's TLS index-0 move must not have any (pe_loader.cpp, reserve_tls_index0), and VulkanContext::stage_xdxr_table already keeps its equivalent list a member for that reason. With the callback present, the title's static-CRT startup reaches ucrtbase's one-shot _register_thread_local_exe_atexit_callback guard (ucrtbase+0x92170) after the token is taken and calls terminate; the fast-fail stack is ucrtbase!terminate -> abort.

Change

The same per-thread LIFO of replay serials, held in a fixed aggregate (GeometryExecuteSerials, 32 entries): no destructor, no TLS callbacks. A replay nested deeper than 32 is not tracked, reported once; the innermost replays end first, so skipped entries unwind correctly.

Testing

  • Built gta6rt (MSVC 14.44, RelWithDebInfo).
  • Boot with LEONIDA_D3D12=vulkan on the retail 1.0.2.1 package: the same build without the change exits 0xC0000409 at ~1.5 s with no D3D12 activity; with it the run passes startup and keeps submitting (20 s check, slots/shadows counters advancing).
  • No game data in the change or in this report.

NVIDIA RTX 4090, driver 616.64, Windows 11.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

861bb05 added geometry_execute_serials() as a function-local
thread_local std::vector. A C++ thread_local with a destructor in
gta6rt.exe adds TLS callbacks to the image, which the loader's TLS
index-0 move must not have (pe_loader.cpp reserve_tls_index0; see
VulkanContext::stage_xdxr_table). With it the title's CRT startup hits
ucrtbase's one-shot _register_thread_local_exe_atexit_callback guard
and terminates: 0xC0000409 (info0=7) at ~1.6 s, before any D3D12 call.

Use a fixed aggregate instead: no destructor, no TLS callbacks, the same
per-thread LIFO, bounded at 32 replays with overflow reported once.

Tested: runtime builds; with LEONIDA_D3D12=vulkan the fix boots past
startup and submits, while 0b11d86 exits 0xC0000409 at ~1.5 s.
NVIDIA RTX 4090, driver 616.64, Windows 11.

AI-assisted (Claude Code).

Co-Authored-By: Claude Code <noreply@anthropic.com>
@sodove

sodove commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Independent Windows 11 / RTX 5070 Ti (610.74) result: unmodified main a73a57e exited with 0xC0000409 about 1.6 s after launch. The UCRT stack included register_thread_local_exe_atexit_callback and terminate. With only eaf0b8d applied to that main, two runs passed startup, presented the intro, and reached indirect graphics. Both ended later with a different exception class (0xC0000005, 113–145 s). This confirms the startup fix on this machine. AI-assisted.

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

Independent native Windows A/B with MSVC 19.44. I linked gta6rt.exe from this head and rebuilt with the original d3d12_vulkan_device.cpp under the same configuration. The original PE has two non-null TLS callbacks. The PR PE has zero. All five selected loader/TLS CTests passed with the PR. run_tiny_tls_ga also passes with the original, so the PE check confirms callback removal and the synthetic loader tests provide regression coverage. No retail startup was run. AI-assisted.

@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

Confirmed on a second machine: RTX 2070 SUPER, NVIDIA 616.56, Windows 11 Pro 26300, MSVC (VS 18) RelWithDebInfo via scripts/build_runtime.ps1 -FetchSdl3, own 1.0.2.1 package.

  • main a73a57e alone: the title exits about 6-7 s in, after the first XMemVirtualAlloc calls and before any D3D12 call. WER reports 0xC0000409 in ucrtbase with parameter 7. Under a small debugger the stack is ucrtbase!abort <- terminate <- _register_thread_local_exe_atexit_callback, called from the title's CRT startup, the same as described above. Turning the pipeline journal off, the input script or the keyboard pad off, or removing the TITLE_WINDOWS_MB/ARENA_MB switches did not change it. A build from before [integration] RT-BOOT-COMBINED: translator fixes + null-barrier fix on main; scripted pad past legal terms to the story load #2138 boots on the same setup.
  • main + this PR (eaf0b8d): it boots past that point and into the world load. One run went on for several minutes and built 1,392 pipelines before it was stopped from outside. No fast-fail.

AI-assisted.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Independent test report for this PR at head eaf0b8d0c, merged locally on main a73a57e13 (merge 3f6acc177, no conflicts, no rebase). It confirms the startup fix on a fourth machine (third independent report): main exits with 0xc0000409 about 1.5 s after launch, and the merge runs the full 60 s.

What this adds to the reports above: an RTX 4060 (Ada); a 60 s boot of main and of the merge on the same machine, one after the other, with counts; the TLS callback check on the two executables that were booted.

Configuration: Windows 11 Pro 10.0.26300, desktop RTX 4060 (8 GB), NVIDIA driver 610.88. MSVC 19.44 (VS 2022), CMake + Ninja, RelWithDebInfo, targets gta6rt and geometry_views_test (63 steps, exit 0). Head eaf0b8d0c still matched the PR just before I wrote this.

Counts

  • Main a73a57e13 (already-built exe), 60 s boot: exited by itself with exit code 0xc0000409. The boot script saw it at its first 5 s poll; the Windows error event is logged about 1.5 s after process start. No shaders line (the Vulkan device was not reached), PresentX ok 0 / failed 0, faults none.
  • Merge (PR + main), 60 s boot, same switches: ran the full time limit and was stopped. 28,433 translated shaders, PresentX ok 257 / failed 0, faults none, 5 captures, all non-black. Game state reached 2 at t=15 s.
  • geometry_views_test on the merge: 67 checks, 0 failures.
  • check_no_game_data.py on runtime/shims/d3d12_vulkan_device.cpp: exit 0.

Call stack on main (small debugger)
ucrtbase!abort+0x4e <- terminate+0x1e <- _register_thread_local_exe_atexit_callback+0x3b <- code of the title <- the game thread. The fast-fail is at ucrtbase offset 0xa527e. This is the same stack as in the reports above. The Windows error event gives the same module and offset. The timing here, about 1.5 s, is in line with the ~1.6 s reported above.

PE check (dumpbin /tls)

  • Main exe: two TLS callbacks, __dyn_tls_init and __dyn_tls_dtor.
  • Merge exe: the callback list is empty.
  • Both still import _register_thread_local_exe_atexit_callback, so the import alone is not the signal. The empty callback list is. This agrees with the PE comparison in lixonik's review, on a different build.

Reading the fix

  • GeometryExecuteSerials is a plain aggregate: 32 uint64_t, a count and a skipped counter. It has no destructor.
  • More than 32 nested replays: begin logs once, counts the replay in skipped and does not push. end first drains skipped, then pops. The innermost replays end first, so the pairing is right. The cost is that a geometry view retired in an extra replay is not deferred to it, and the log line says so. I did not try to reach depth 33; I do not know whether a real replay can.
  • I grepped runtime/shims and runtime/src for other thread_local. All I found are bool, integers, pointers, HANDLE or an array of plain structs. A few are function-local static thread_local (the 7 KiB Texture in d3d12_xs_alloc_size.h and two HANDLEs in d3d12_xs_device.cpp). None has a visible destructor. The empty callback list in the merge exe confirms none adds a callback today.
  • Nothing stops a later thread_local with a destructor from bringing the crash back. A cheap guard could be a CTest that asserts the callback list of gta6rt.exe is empty. This is a suggestion only.

Attribution note. The PR body says commit 861bb0546 came with #2138. By git, it is one of the commits of #2094: it is on the second-parent side of that merge (7f3d0095a), and not on its first-parent side. #2138 (0b11d86bc) was merged after that and already had it from main on both sides. This is a small point and does not change the fix.

Not covered: no full ctest suite, only geometry_views_test. No run longer than 60 s, so the later 0xC0000005 exits that sodove saw at 113-145 s were not tested. No test of replay depth over 32. No Linux or Wine run. No input was sent during the boot (-NoInput).

AI-assisted: yes (Claude Code, Claude Sonnet 5.5, built and ran the tests; a second agent checked this text)

@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

Follow-up to my report above, on the later 0xC0000005 that sodove saw at 113-145 s. Same machine and build: main a73a57e + this PR. AI-assisted.

I couldn't reproduce it here in three boots, 200 s, 300 s and 300 s, with no exception in any of them.

  • Two had LEONIDA_TITLE_WATCHDOG=0 with LEONIDA_TITLE_WATCHDOG_KICK set.
  • The last 300 s boot had only LEONIDA_TITLE_WATCHDOG=0. As the log says, main now leaves the title's watchdog armed when LEONIDA_TITLE_WATCHDOG_KICK is missing (runtime/src/main.cpp).

That matters for the 0xC0000005 reports. Since that change, a LEONIDA_TITLE_WATCHDOG=0 from an older launcher no longer stops the watchdog. In #2019 a write to 0x0 at that point turned out to be the title's watchdog (+0x2215c35 on the stack). So check that stack before reading the crash as a second bug of this PR. My boots probably stayed under the 30 s window because the pipeline journal was warm (see #1608).

@qhukz13

qhukz13 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

I checked eaf0b8d0c with the TLS directory of the built exe, on Windows 10 / MSVC 14.44 RelWithDebInfo, main 73516a290 with and without this PR. I did not run the game. dumpbin /tls gta6rt.exe shows the TLS callbacks:

build TLS callbacks
main std::sys::thread_local::guard::windows::tls_callback (Rust), __dyn_tls_init, __dyn_tls_dtor
main + this PR std::sys::thread_local::guard::windows::tls_callback (Rust) only

So the two MSVC dynamic-TLS callbacks are gone, and those are the ones that make the title's own _register_thread_local_exe_atexit_callback fast-fail, which matches HasiKe's report. The one left is Rust std's. It comes from the XMA2 decoder (runtime/xma_decoder), which runtime/CMakeLists.txt builds and links whenever cargo is on PATH, as it is on this machine. Rust's callback runs Rust thread-local destructors and does not register anything with the UCRT, so it should not fast-fail the title. If your boots were built without cargo, a boot of a build with the Rust decoder linked would confirm that.

The change builds clean.

AI-assisted (Claude Code).

@aslan2142

Copy link
Copy Markdown
Collaborator

Thank you, @brandostrong. This went into main through #2350 (RT-LINUX-AMD-WORLD), merged at your head eaf0b8d0c with your commits and authorship kept. GitHub shows this PR as merged for that reason, so nothing needs closing.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

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

Tested on Windows 11 (i7-12700KF, RTX 5070, MSVC RelWithDebInfo via build_runtime.ps1 -FetchSdl3), on main 73516a2.

Before this PR: gta6rt --first-run-ui exited with 0xC0000409 right after the first xmem allocations, no window. The dump showed ucrtbase!terminate called from register_thread_local_exe_atexit_callback, from the title's CRT startup.

With this PR merged locally: the title starts, opens its window and reaches the first-run UI (driven with the keyboard pad).

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.

8 participants