process: cover the game's thread-local exe-atexit callback #2354

Closed
github-bot wants to merge 2 commits from github-bot/gh-2354 into main AGit
Owner

@⁠ERROR890 on GitHub · 2026-11-26 14:59 UTC · original
ERROR890:fix/thread-local-atexit → main · +91 −2 · 4 files · 2 commits · closed without merging

Claimed in the coordination issue as: BOOT-TLS-ATEXIT-CALLBACK
(#1608)

Depends on: none.

Fixes: none (no issue tracks this).

What and why

shims/process.cpp answers the retail title's process-level imports (its command line, exit/abort,
_crt_atexit, ...) and its header lists what is still missing: "Not covered yet: at_quick_exit() handlers ...
and the thread-local destructor callback (_register_thread_local_exe_atexit_callback); both still reach the host
CRT."
This covers the second one.

It is also where the title stopped on a73a57e1. Running the 1.0.2.1 package through gta6rt --run there, the
process ended with 0xC0000409 in ucrtbase after the static initialisers and before any D3D12 call. An int3
planted on ucrtbase!terminate fired first, and the return address on the stack was inside
_register_thread_local_exe_atexit_callback (the call terminate of its cold path): the UCRT's own consistency
check failed in this process and its CRT ended the run. The fast-fail exception information was 0x7
(FAST_FAIL_RANGE_CHECK_FAILURE), the code that path raises. The runtime's own shim header already named the gap.

On current main (fbf100b1b) that stop is already gone - it was cleared by the TLS/thread work that landed after
a73a57e1 (#2195 and neighbours) - so please read this as the coverage the header promises plus its test, not as a
crash fix.

What changes:

  • the shim keeps the callback per thread and returns the previous one, as the UCRT does;
  • the loader runs it in the DLL_THREAD_DETACH branch of on_host_thread_event, after the DLL slots' callbacks and
    the executable's own, which is where the UCRT would run it; a throw is caught so the Windows loader lock is never
    left held;
  • process_cmdline_test gains a case that fails on main (there is no such shim) and passes with this change.

Tested

  • process_cmdline_test (cmake --build <build> --target process_cmdline_test) built and run on main plus this
    change: exit 0, including the new case (shim found; first registration returns no previous callback and runs
    nothing; the runner runs it once and not twice; a later registration returns the callback it replaces).
  • The retail 1.0.2.1 title through gta6rt --run, with the shader workspace this checkout translated, on main plus
    this change: reaches the Vulkan device and presents ([rt] d3d12_xs: PresentX ready (... client area 1904x1041)).
    Plain main on the same machine does the same, so no regression from this change.

GPUs tested: NVIDIA GeForce RTX 5070, driver 621.40, Windows 11 26H2 (build 26300.9974) - boot only; the GPU test
suite was not run for this change.

AI-assisted: yes.

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator
    change in this pull request.)
**@⁠ERROR890** on GitHub · 2026-11-26 14:59 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2354) `ERROR890:fix/thread-local-atexit` → `main` · +91 −2 · 4 files · 2 commits · closed without merging Claimed in the coordination issue as: BOOT-TLS-ATEXIT-CALLBACK (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) Depends on: none. Fixes: none (no issue tracks this). ### What and why `shims/process.cpp` answers the retail title's process-level imports (its command line, `exit`/`abort`, `_crt_atexit`, ...) and its header lists what is still missing: *"Not covered yet: at_quick_exit() handlers ... and the thread-local destructor callback (`_register_thread_local_exe_atexit_callback`); both still reach the host CRT."* This covers the second one. It is also where the title stopped on `a73a57e1`. Running the 1.0.2.1 package through `gta6rt --run` there, the process ended with `0xC0000409` in `ucrtbase` after the static initialisers and before any D3D12 call. An `int3` planted on `ucrtbase!terminate` fired first, and the return address on the stack was inside `_register_thread_local_exe_atexit_callback` (the `call terminate` of its cold path): the UCRT's own consistency check failed in this process and its CRT ended the run. The fast-fail exception information was `0x7` (`FAST_FAIL_RANGE_CHECK_FAILURE`), the code that path raises. The runtime's own shim header already named the gap. On current `main` (`fbf100b1b`) that stop is already gone - it was cleared by the TLS/thread work that landed after `a73a57e1` (#2195 and neighbours) - so please read this as the coverage the header promises plus its test, not as a crash fix. What changes: - the shim keeps the callback per thread and returns the previous one, as the UCRT does; - the loader runs it in the `DLL_THREAD_DETACH` branch of `on_host_thread_event`, after the DLL slots' callbacks and the executable's own, which is where the UCRT would run it; a throw is caught so the Windows loader lock is never left held; - `process_cmdline_test` gains a case that fails on `main` (there is no such shim) and passes with this change. ### Tested - `process_cmdline_test` (`cmake --build <build> --target process_cmdline_test`) built and run on `main` plus this change: exit 0, including the new case (shim found; first registration returns no previous callback and runs nothing; the runner runs it once and not twice; a later registration returns the callback it replaces). - The retail 1.0.2.1 title through `gta6rt --run`, with the shader workspace this checkout translated, on `main` plus this change: reaches the Vulkan device and presents (`[rt] d3d12_xs: PresentX ready (... client area 1904x1041)`). Plain `main` on the same machine does the same, so no regression from this change. GPUs tested: NVIDIA GeForce RTX 5070, driver 621.40, Windows 11 26H2 (build 26300.9974) - boot only; the GPU test suite was not run for this change. AI-assisted: yes. ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator change in this pull request.)
_register_thread_local_exe_atexit_callback is the CRT's per-thread hook for an EXE's thread_local destructors. The
runtime left it to the host UCRT, which is the gap shims/process.cpp's header records (''both still reach the host
CRT''). On a73a57e1 that path is where the retail title stopped: the UCRT's consistency check failed in this process,
its cold path called ucrtbase!terminate, and the title aborted with 0xC0000409 after the static initialisers and
before its first D3D12 call.

Keep the callback per thread in process.cpp and return the previous one as the UCRT does. The loader runs it in the
DLL_THREAD_DETACH branch of on_host_thread_event, after the DLL slots' callbacks and the executable's own, which is
where the UCRT would run it; a throw is caught so the Windows loader lock is never left held.

process_cmdline_test covers it: the shim is found, the first registration returns no previous callback and runs
nothing yet, the runner runs it once and not twice, and a later registration returns the callback it replaces.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:04:32 +00:00
Author
Owner

@⁠ERROR890 on GitHub · 2026-11-26 15:20 UTC · original

Companion pull request for the hardware this was found and tested on:
#2358 gpu-test-results: RTX 5070, driver 621.40, Windows 11 26H2 - the GPU test result from the same machine
(it currently sits as a draft; the account is not permitted to publish it right now, so it needs "Ready for review").

Claimed in the coordination issue as BOOT-TLS-ATEXIT-CALLBACK:
#1608

For the record on where this was found: the boot stop on a73a57e1 was 0xC0000409 in ucrtbase after the static
initialisers and before the first D3D12 call. An int3 on ucrtbase!terminate fired first, and the return address on
the stack was inside _register_thread_local_exe_atexit_callback (the call terminate of its cold path); the
fast-fail exception information was 0x7 (FAST_FAIL_RANGE_CHECK_FAILURE), the code that path raises. Current main
no longer stops there, so this pull request is the coverage and the test, as the description says.

**@⁠ERROR890** on GitHub · 2026-11-26 15:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2354#issuecomment-7196130199) Companion pull request for the hardware this was found and tested on: **#2358 gpu-test-results: RTX 5070, driver 621.40, Windows 11 26H2** - the GPU test result from the same machine (it currently sits as a draft; the account is not permitted to publish it right now, so it needs "Ready for review"). Claimed in the coordination issue as BOOT-TLS-ATEXIT-CALLBACK: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 For the record on where this was found: the boot stop on `a73a57e1` was `0xC0000409` in `ucrtbase` after the static initialisers and before the first D3D12 call. An `int3` on `ucrtbase!terminate` fired first, and the return address on the stack was inside `_register_thread_local_exe_atexit_callback` (the `call terminate` of its cold path); the fast-fail exception information was `0x7` (`FAST_FAIL_RANGE_CHECK_FAILURE`), the code that path raises. Current `main` no longer stops there, so this pull request is the coverage and the test, as the description says.
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 15:37 UTC · original · reviewed

Built process_cmdline_test with MSVC on Windows 11. CTest passed at this head, including the thread-local executable teardown callback tests. Synthetic process fixtures only.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 15:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2354#pullrequestreview-6076879116) · **reviewed** Built `process_cmdline_test` with MSVC on Windows 11. CTest passed at this head, including the thread-local executable teardown callback tests. Synthetic process fixtures only. AI-assisted.
github-bot closed this pull request 2026-11-26 19:12:49 +00:00
Author
Owner

@⁠ERROR890 on GitHub · 2026-11-26 19:04 UTC · original

Closing as superseded: main now implements this itself.

  • 338dc11ea "runtime: own the executable TLS exit callback" and 48f87a9c9 "process: call the EXE's thread-local
    destructor callback with its real signature, before the atexit handlers" (review fix for #2315), merged through
    #2395, keep the registration in shims/process.cpp with the UCRT's real callback signature and run it from exit()
    and _cexit() before the atexit handlers. That is the substantive part of what this pull request proposed.

While checking this branch against current main I also found that the additional call this pull request made from
the loader's thread-detach path is harmful: the executable's callback is already listed in its TLS directory, which
on_host_thread_event runs on DLL_THREAD_DETACH (the same thing the Windows loader does), so calling it a second
time from there double-runs vcruntime's thread-local destructors. Measured on 1.0.2.1, RTX 5070 / driver 621.40 /
Windows 11 26H2: with that extra call the run ended in an unhandled 0xC0000005 inside a minute, twice; with the same
tree and only that call removed it ran its full 75 s window and presented normally. So the loader-side hook should
not be merged, and there is nothing left here that main does not already do.

Thanks to whoever wrote #2315 and its review fix; the signature detail (void (__stdcall*)(void*, unsigned long, void*) and the (nullptr, DLL_PROCESS_DETACH, nullptr) arguments) is right where this branch guessed.

No further work is planned on this branch.

**@⁠ERROR890** on GitHub · 2026-11-26 19:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2354#issuecomment-7201160079) Closing as superseded: `main` now implements this itself. - `338dc11ea` "runtime: own the executable TLS exit callback" and `48f87a9c9` "process: call the EXE's thread-local destructor callback with its real signature, before the atexit handlers" (review fix for #2315), merged through #2395, keep the registration in `shims/process.cpp` with the UCRT's real callback signature and run it from `exit()` and `_cexit()` before the atexit handlers. That is the substantive part of what this pull request proposed. While checking this branch against current `main` I also found that the additional call this pull request made from the loader's thread-detach path is harmful: the executable's callback is already listed in its TLS directory, which `on_host_thread_event` runs on `DLL_THREAD_DETACH` (the same thing the Windows loader does), so calling it a second time from there double-runs vcruntime's thread-local destructors. Measured on 1.0.2.1, RTX 5070 / driver 621.40 / Windows 11 26H2: with that extra call the run ended in an unhandled `0xC0000005` inside a minute, twice; with the same tree and only that call removed it ran its full 75 s window and presented normally. So the loader-side hook should not be merged, and there is nothing left here that `main` does not already do. Thanks to whoever wrote #2315 and its review fix; the signature detail (`void (__stdcall*)(void*, unsigned long, void*)` and the `(nullptr, DLL_PROCESS_DETACH, nullptr)` arguments) is right where this branch guessed. No further work is planned on this branch.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.