process: the game's at_quick_exit handlers and thread-local exit callback are the run's own (RT-CRT-QUICK-EXIT-TLS) #1564

Open
github-bot wants to merge 2 commits from github-bot/gh-1564 into main AGit
Owner

@⁠Mike7581 on GitHub · 2026-11-25 04:40 UTC · original
Mike7581:rt-crt-quick-exit-tls → main · +295 −36 · 5 files · 2 commits · open

Claimed in the coordination issue as: RT-CRT-QUICK-EXIT-TLS (#1562, since #14 is at the 2,500-comment cap)

What and why

runtime/shims/process.cpp and runtime/README.md listed two gaps: the game's at_quick_exit() handlers and its thread-local destructor callback (_register_thread_local_exe_atexit_callback) still reached the host CRT, which the game shares with gta6rt. Measured under gta6rt --run before this change:

  • quick_exit() ran none of the game's at_quick_exit handlers;
  • exit() and _cexit() never ran the exiting thread's thread_local destructors (and a second registration in the host's one-slot callback would terminate gta6rt).

Now:

  • _crt_at_quick_exit goes to a second table of the run's own. quick_exit() runs it newest first under the exit lock; _exit() and _Exit() run nothing (they get their own function instead of sharing game_quick_exit).
  • The thread-local callback is the run's own: exit() and _cexit() call it with DLL_PROCESS_DETACH before the atexit handlers, as the UCRT does. A second registration aborts, as the UCRT's terminate() does. Both reset per run.

Tested

  • tiny_atexit gains five modes: at_quick_exit, at_quick_exit_exit, at_quick_exit__exit, thread_local_exit, thread_local_cexit. Their expected sequences in check_atexit.cmake come from running the fixture natively on Windows 11 (the test also holds the native run to them). Before the fix, --run differed on at_quick_exit (no handler ran), thread_local_exit and thread_local_cexit (no dtor thread_local).
  • run_tiny_atexit: 13 termination paths match the Windows sequence.
  • ctest -R "run_tiny|process|loader|exit": 29/29 passed.

GPUs tested: n/a (CPU only). Built with MSVC (VS 2022 Build Tools 17.14) + Ninja via scripts\build_runtime.ps1, Windows 11 Pro 10.0.26200.

AI-assisted: yes (Claude Code)

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. (n/a: no translator change)
**@⁠Mike7581** on GitHub · 2026-11-25 04:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1564) `Mike7581:rt-crt-quick-exit-tls` → `main` · +295 −36 · 5 files · 2 commits · open Claimed in the coordination issue as: RT-CRT-QUICK-EXIT-TLS (#1562, since #14 is at the 2,500-comment cap) ### What and why `runtime/shims/process.cpp` and `runtime/README.md` listed two gaps: the game's `at_quick_exit()` handlers and its thread-local destructor callback (`_register_thread_local_exe_atexit_callback`) still reached the host CRT, which the game shares with gta6rt. Measured under `gta6rt --run` before this change: - `quick_exit()` ran none of the game's `at_quick_exit` handlers; - `exit()` and `_cexit()` never ran the exiting thread's `thread_local` destructors (and a second registration in the host's one-slot callback would terminate gta6rt). Now: - `_crt_at_quick_exit` goes to a second table of the run's own. `quick_exit()` runs it newest first under the exit lock; `_exit()` and `_Exit()` run nothing (they get their own function instead of sharing `game_quick_exit`). - The thread-local callback is the run's own: `exit()` and `_cexit()` call it with `DLL_PROCESS_DETACH` before the atexit handlers, as the UCRT does. A second registration aborts, as the UCRT's `terminate()` does. Both reset per run. ### Tested - `tiny_atexit` gains five modes: `at_quick_exit`, `at_quick_exit_exit`, `at_quick_exit__exit`, `thread_local_exit`, `thread_local_cexit`. Their expected sequences in `check_atexit.cmake` come from running the fixture natively on Windows 11 (the test also holds the native run to them). Before the fix, `--run` differed on `at_quick_exit` (no handler ran), `thread_local_exit` and `thread_local_cexit` (no `dtor thread_local`). - `run_tiny_atexit`: 13 termination paths match the Windows sequence. - `ctest -R "run_tiny|process|loader|exit"`: 29/29 passed. GPUs tested: n/a (CPU only). Built with MSVC (VS 2022 Build Tools 17.14) + Ninja via `scripts\build_runtime.ps1`, Windows 11 Pro 10.0.26200. AI-assisted: yes (Claude Code) ### 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. (n/a: no translator change)
The game's at_quick_exit() (_crt_at_quick_exit) and _register_thread_local_exe_atexit_callback still
reached the host CRT, which the game shares with gta6rt. Under `gta6rt --run`:
  - quick_exit() ran none of the game's at_quick_exit handlers;
  - exit() and _cexit() never ran the exiting thread's thread_local destructors, and a second
    registration in the host's one-slot callback would terminate gta6rt.

Now _crt_at_quick_exit goes to a second table of the run's own, which quick_exit() runs newest first
under the exit lock; _exit() and _Exit() run none (they no longer share game_quick_exit). The thread-local
callback is the run's own: exit() and _cexit() call it with DLL_PROCESS_DETACH before the atexit handlers,
as the UCRT does, and a second registration aborts, as the UCRT's terminate() does. Both reset per run.

tiny_atexit gains five modes (at_quick_exit, at_quick_exit_exit, at_quick_exit__exit, thread_local_exit,
thread_local_cexit). Their expected sequences in check_atexit.cmake were measured by running the fixture
natively on Windows 11; before this change --run differed on at_quick_exit, thread_local_exit and
thread_local_cexit. run_tiny_atexit now holds 13 paths to the Windows sequence.
Review follow-up for #1564:
- Registration and the cleanup of exit, quick_exit and _cexit hold one recursive lock of the run's own, from before the thread-local callback through the table's drain (and _cexit's stream flush), as the UCRT holds its exit lock: two threads never clean up at once, and a registration from another thread waits for the cleanup. The exiting thread may still register from a handler, _cexit still returns, and the lock is released before the run ends (end_run), also when a handler ends the run from inside a drain.
- Each guest cleanup callback (atexit, at_quick_exit, the thread-local destructor callback) runs inside a C++ exception boundary: one that throws ends the run through game_abort, as the UCRT's terminate() would (exit code 3), instead of leaving the process with 0xE06D7363.

tiny_atexit gains four modes, held to the native sequence: concurrent_cexit (a second thread's _cexit, with a thread_local of its own, waits for the cleanup), quick_exit_register (an at_quick_exit from a second thread during a quick_exit drain waits, so its handler never runs), quick_exit_throw and exit_throw (exit code 3). In the concurrent modes the slow handler waits until the second thread is about to call, so the sequence is fixed under any scheduling with the lock. All four fail on c5b51e9 through --run.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:56:47 +00:00
Author
Owner

@⁠Andretuta on GitHub · 2026-11-25 08:35 UTC · original · requested changes

The termination order is right, and it matches the UCRT's common_exit:

  • the TLS callback runs with DLL_PROCESS_DETACH before the atexit table in exit and _cexit;
  • quick_exit drains only the at_quick_exit table;
  • a second _register_thread_local_exe_atexit_callback terminates.

run_tiny_atexit passes all 13 paths natively and through --run. Two things differ from the UCRT in ways a test can observe. Both come from patterns that already exist on main, which the PR extends to the new paths. They could be a follow-up, but the second one turns a clean exit code into an unhandled exception, so it is worth fixing here. Line numbers refer to the PR head c5b51e9.

Should-fix (extends an existing pattern): cleanup is not serialized like the UCRT's exit lock

game_cexit (process.cpp:194) runs the handlers without taking the exit lock. run_handlers (:130, :145) releases g_atexit_lock before each call. Two probes on the PR head showed the gap:

  • Two threads in _cexit ran their thread-local destructor callbacks at the same time.
  • A second thread that called at_quick_exit during a quick_exit drain got success, and its handler ran in that drain.

Native UCRT holds the exit lock across the TLS callback and the table, and registration takes the same lock. In the native fixture, the callbacks were serialized and the peer registrar blocked.

Suggested fix:

  • Use one run-local recursive cleanup mutex for registration and every cleanup mode.
  • Take it before deciding which thread cleans up, and hold it through the callback and the table drain.
  • Release it before the mapped run ends.
  • Keep same-thread late registration and _cexit returning.
  • Add a two-thread test for each case.

Should-fix (extends an existing pattern): a throwing quick-exit handler crashes instead of terminating

process.cpp:190 (and the existing atexit drain at :139) has no C++ exception boundary. In a synthetic test, a quick-exit handler throws:

Run Result
Native UCRT catches it, calls terminate, exit code 3 with the default handler
Through --run on the PR head process ends with the unhandled C++ exception status 0xE06D7363

Suggested fix: wrap each guest cleanup callback the way the UCRT does, and send it to the run's own termination path.

void run_handlers(std::vector<AtexitFn>& table) {
    for (;;) {
        AtexitFn fn;
        { /* pop newest under the lock, as now */ }
        try {
            fn();
        } catch (...) {
            game_abort();  // the UCRT calls terminate(), whose default handler aborts: exit code 3
        }
    }
}

Add a tiny_atexit mode with a throwing at_quick_exit handler, and compare it with the native run.

What was checked

  • Merge with main: clean.
  • Full build: OK. CTest: 145/150 pass, including run_tiny_atexit (13 termination paths), the loader* tests and all run_tiny_* tests. The 5 failures are in code this PR does not touch:
    • root_sig, xmem, xmem_no_write_watch and root_signature_oracle fail the same way on the other PR branches built here.
    • xmem_write_watch failed only on this run and passed on the other two branches.
  • Native vs mapped probes for concurrent _cexit, cross-thread at_quick_exit registration and a throwing quick handler, compared against the SDK's UCRT exit.cpp / onexit.cpp.

Review done with AI assistance (Claude Code and GPT-6.2 Sol, cross-checked against each other). Every finding above was reproduced with a test probe.

**@⁠Andretuta** on GitHub · 2026-11-25 08:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1564#pullrequestreview-6054228974) · **requested changes** The termination order is right, and it matches the UCRT's `common_exit`: - the TLS callback runs with `DLL_PROCESS_DETACH` before the `atexit` table in `exit` and `_cexit`; - `quick_exit` drains only the `at_quick_exit` table; - a second `_register_thread_local_exe_atexit_callback` terminates. `run_tiny_atexit` passes all 13 paths natively and through `--run`. Two things differ from the UCRT in ways a test can observe. Both come from patterns that already exist on `main`, which the PR extends to the new paths. They could be a follow-up, but the second one turns a clean exit code into an unhandled exception, so it is worth fixing here. Line numbers refer to the PR head `c5b51e9`. ### Should-fix (extends an existing pattern): cleanup is not serialized like the UCRT's exit lock `game_cexit` (`process.cpp:194`) runs the handlers without taking the exit lock. `run_handlers` (`:130`, `:145`) releases `g_atexit_lock` before each call. Two probes on the PR head showed the gap: - Two threads in `_cexit` ran their thread-local destructor callbacks at the same time. - A second thread that called `at_quick_exit` during a `quick_exit` drain got success, and its handler ran in that drain. Native UCRT holds the exit lock across the TLS callback and the table, and registration takes the same lock. In the native fixture, the callbacks were serialized and the peer registrar blocked. Suggested fix: - Use one run-local recursive cleanup mutex for registration and every cleanup mode. - Take it before deciding which thread cleans up, and hold it through the callback and the table drain. - Release it before the mapped run ends. - Keep same-thread late registration and `_cexit` returning. - Add a two-thread test for each case. ### Should-fix (extends an existing pattern): a throwing quick-exit handler crashes instead of terminating `process.cpp:190` (and the existing atexit drain at `:139`) has no C++ exception boundary. In a synthetic test, a quick-exit handler throws: | Run | Result | | --- | --- | | Native UCRT | catches it, calls `terminate`, exit code 3 with the default handler | | Through `--run` on the PR head | process ends with the unhandled C++ exception status `0xE06D7363` | Suggested fix: wrap each guest cleanup callback the way the UCRT does, and send it to the run's own termination path. ```cpp void run_handlers(std::vector<AtexitFn>& table) { for (;;) { AtexitFn fn; { /* pop newest under the lock, as now */ } try { fn(); } catch (...) { game_abort(); // the UCRT calls terminate(), whose default handler aborts: exit code 3 } } } ``` Add a `tiny_atexit` mode with a throwing `at_quick_exit` handler, and compare it with the native run. ### What was checked - Merge with `main`: clean. - Full build: OK. CTest: 145/150 pass, including `run_tiny_atexit` (13 termination paths), the `loader*` tests and all `run_tiny_*` tests. The 5 failures are in code this PR does not touch: - `root_sig`, `xmem`, `xmem_no_write_watch` and `root_signature_oracle` fail the same way on the other PR branches built here. - `xmem_write_watch` failed only on this run and passed on the other two branches. - Native vs mapped probes for concurrent `_cexit`, cross-thread `at_quick_exit` registration and a throwing quick handler, compared against the SDK's UCRT `exit.cpp` / `onexit.cpp`. Review done with AI assistance (Claude Code and GPT-6.2 Sol, cross-checked against each other). Every finding above was reproduced with a test probe.
Author
Owner

@⁠Mike7581 on GitHub · 2026-11-25 19:19 UTC · original

Thanks for the probes against the UCRT. Both points are addressed in 27a2711:

  • Registration and the cleanup of exit, quick_exit and _cexit now hold one run-local recursive lock, from before the thread-local callback through the table's drain (and _cexit's stream flush), as the UCRT holds its exit lock. Two threads never clean up at once, and a registration from another thread waits for the cleanup. Same-thread late registration still works, _cexit still returns, and the lock is released in end_run before the mapped run ends (also when a handler ends the run from inside a drain).
  • Every guest cleanup callback runs inside a C++ exception boundary; one that throws ends the run through game_abort, exit code 3, as the UCRT's terminate() would.

tiny_atexit adds four modes, each held to the native sequence: concurrent_cexit (a second thread's _cexit, with a thread_local of its own, waits for the first cleanup), quick_exit_register (an at_quick_exit from a second thread during a quick_exit drain waits, so its handler never runs), quick_exit_throw and exit_throw. In the concurrent modes the slow handler waits until the second thread is about to call, so the sequence is fixed under any scheduling. All four fail on c5b51e9 through --run; all 17 paths pass now, 5 runs in a row. Full build and CTest pass here (RTX 4050).

Written with AI assistance (Claude Code, Claude Opus 5.5 and Claude Fable 5.1) and reviewed with GPT-6 Sol High; every change above was checked against the code and the tests were run locally.

**@⁠Mike7581** on GitHub · 2026-11-25 19:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1564#issuecomment-7172146878) Thanks for the probes against the UCRT. Both points are addressed in 27a2711: - Registration and the cleanup of `exit`, `quick_exit` and `_cexit` now hold one run-local recursive lock, from before the thread-local callback through the table's drain (and `_cexit`'s stream flush), as the UCRT holds its exit lock. Two threads never clean up at once, and a registration from another thread waits for the cleanup. Same-thread late registration still works, `_cexit` still returns, and the lock is released in `end_run` before the mapped run ends (also when a handler ends the run from inside a drain). - Every guest cleanup callback runs inside a C++ exception boundary; one that throws ends the run through `game_abort`, exit code 3, as the UCRT's `terminate()` would. `tiny_atexit` adds four modes, each held to the native sequence: `concurrent_cexit` (a second thread's `_cexit`, with a `thread_local` of its own, waits for the first cleanup), `quick_exit_register` (an `at_quick_exit` from a second thread during a `quick_exit` drain waits, so its handler never runs), `quick_exit_throw` and `exit_throw`. In the concurrent modes the slow handler waits until the second thread is about to call, so the sequence is fixed under any scheduling. All four fail on `c5b51e9` through `--run`; all 17 paths pass now, 5 runs in a row. Full build and CTest pass here (RTX 4050). Written with AI assistance (Claude Code, Claude Opus 5.5 and Claude Fable 5.1) and reviewed with GPT-6 Sol High; every change above was checked against the code and the tests were run locally.
This pull request has changes conflicting with the target branch.
  • runtime/README.md
  • runtime/shims/process.cpp
  • runtime/test/check_atexit.cmake
  • runtime/test/fixtures/tiny_atexit.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1564/head:github-bot/gh-1564'
git switch 'github-bot/gh-1564'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1564'
git switch 'github-bot/gh-1564'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1564'
git switch 'github-bot/gh-1564'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1564'
git switch 'main'
git merge --squash 'github-bot/gh-1564'
git switch 'main'
git merge --ff-only 'github-bot/gh-1564'
git switch 'main'
git merge 'github-bot/gh-1564'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.