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

Merged
Mike7581 merged 2 commits from upstream-pr/1564 into main 2026-11-28 03:48:27 +00:00
Mike7581 commented 2026-11-25 04:40:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Mike7581 on 2026-11-25T04:40:59Z.


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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5485295730 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1564). Originally posted by [Mike7581](https://github.com/Mike7581) on 2026-11-25T04:40:59Z. --- 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)
Andretuta commented 2026-11-25 08:35:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-25T08:35:49Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6054228974 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1564#pullrequestreview-6054228974). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-25T08:35:49Z. **Review:** CHANGES_REQUESTED --- 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.
Mike7581 commented 2026-11-25 19:19:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Mike7581 on 2026-11-25T19:19:52Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7172146878 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1564#issuecomment-7172146878). Originally posted by [Mike7581](https://github.com/Mike7581) on 2026-11-25T19:19:52Z. --- 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 commented 2026-11-27 19:18:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Mike7581 on 2026-11-27T19:18:38Z.


Rebased onto current main (43a9d562d); the branch is now 1dd08900f and merges cleanly.

main gained #2315 (with the maintainer fix in 48f87a9c9) while this was open. It owns the EXE's thread-local exit callback, which overlaps the first commit here. I kept one implementation:

  • process.cpp has a single game_register_thread_local_exe_atexit_callback, the one from this PR (a second registration terminates). The callback is called inside the cleanup lock, before the atexit drain, in exit and _cexit. The duplicate import-table entry is gone. The maintainer's explanation of why the host CRT can't keep the callback (UCRT fast-fails the second registration; Wine replaces gta6rt's own) is kept, next to the registration.
  • One behavioural difference from main: main called the callback once per run (exchange). This PR calls it on every exit/_cexit. concurrent_cexit settles it against Windows. Natively, the second thread's thread_local ends inside its own _cexit(), before second _cexit returned. With main's process.cpp it ends only at thread detach, after that line.
  • tiny_atexit: thread_local_exit/thread_local_cexit stay exactly as on main (codes 13/14). This PR's at_quick_exit modes moved to 15–17; the others are unchanged (18, 19, 3).
  • runtime/README.md: merged both descriptions.

Tested on Windows 11, RTX 4050 Laptop, MSVC RelWithDebInfo:

  • run_tiny_atexit passes: 17 paths, native and --run, no native differences.
  • With main's runtime/shims/process.cpp swapped in, 4 modes fail: at_quick_exit, concurrent_cexit, quick_exit_register, quick_exit_throw. exit_throw passes on main too.
  • Restored: passes again.

Written with AI assistance (Claude Code, Claude Opus 5.5); the conflict resolution was checked line by line against both sides, and the tests above were run locally.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7226399546 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1564#issuecomment-7226399546). Originally posted by [Mike7581](https://github.com/Mike7581) on 2026-11-27T19:18:38Z. --- Rebased onto current `main` (`43a9d562d`); the branch is now `1dd08900f` and merges cleanly. `main` gained #2315 (with the maintainer fix in 48f87a9c9) while this was open. It owns the EXE's thread-local exit callback, which overlaps the first commit here. I kept one implementation: - `process.cpp` has a single `game_register_thread_local_exe_atexit_callback`, the one from this PR (a second registration terminates). The callback is called inside the cleanup lock, before the `atexit` drain, in `exit` and `_cexit`. The duplicate import-table entry is gone. The maintainer's explanation of why the host CRT can't keep the callback (UCRT fast-fails the second registration; Wine replaces gta6rt's own) is kept, next to the registration. - One behavioural difference from `main`: `main` called the callback once per run (`exchange`). This PR calls it on every `exit`/`_cexit`. `concurrent_cexit` settles it against Windows. Natively, the second thread's `thread_local` ends *inside* its own `_cexit()`, before `second _cexit returned`. With `main`'s `process.cpp` it ends only at thread detach, after that line. - `tiny_atexit`: `thread_local_exit`/`thread_local_cexit` stay exactly as on `main` (codes 13/14). This PR's `at_quick_exit` modes moved to 15–17; the others are unchanged (18, 19, 3). - `runtime/README.md`: merged both descriptions. Tested on Windows 11, RTX 4050 Laptop, MSVC RelWithDebInfo: - `run_tiny_atexit` passes: 17 paths, native and `--run`, no native differences. - With `main`'s `runtime/shims/process.cpp` swapped in, 4 modes fail: `at_quick_exit`, `concurrent_cexit`, `quick_exit_register`, `quick_exit_throw`. `exit_throw` passes on `main` too. - Restored: passes again. Written with AI assistance (Claude Code, Claude Opus 5.5); the conflict resolution was checked line by line against both sides, and the tests above were run locally.
Andretuta commented 2026-11-28 03:48:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-28T03:48:14Z.

Review: APPROVED


Re-checked at 1dd08900f merged cleanly onto current main (02f71ccfe, Windows 10 x64, MSVC 14.44 RelWithDebInfo, Ninja).

All items from the previous review and the rebase over #2315 are addressed cleanly:

  • Registration and exit / quick_exit / _cexit cleanup now serialize on one run-local recursive g_cleanup_lock (released in end_run and before parking peer threads in take_exit_lock).
  • Guest cleanup callbacks run inside a C++ exception boundary (guarded) routing escaping exceptions to game_abort() (exit code 3), matching UCRT terminate().
  • The post-#2315 rebase keeps a single _register_thread_local_exe_atexit_callback registration, preserves per-thread _cexit() TLS callback invocation (load()), and resets g_thread_local_exit in run_entry.

Local verification:

  • python -I scripts/check_no_game_data.py on all 5 changed files: exit 0.
  • Built gta6rt + tiny_atexit cleanly (zero new warnings).
  • ctest -R '^run_tiny_atexit$': 3/3 consecutive passes (5.09 s, 4.90 s, 4.76 s — tiny_atexit: 17 termination paths match the Windows sequence).

Looks ready to merge.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6095553149 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1564#pullrequestreview-6095553149). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-28T03:48:14Z. **Review:** APPROVED --- Re-checked at `1dd08900f` merged cleanly onto current `main` (`02f71ccfe`, Windows 10 x64, MSVC 14.44 RelWithDebInfo, Ninja). All items from the previous review and the rebase over #2315 are addressed cleanly: - Registration and `exit` / `quick_exit` / `_cexit` cleanup now serialize on one run-local recursive `g_cleanup_lock` (released in `end_run` and before parking peer threads in `take_exit_lock`). - Guest cleanup callbacks run inside a C++ exception boundary (`guarded`) routing escaping exceptions to `game_abort()` (exit code 3), matching UCRT `terminate()`. - The post-#2315 rebase keeps a single `_register_thread_local_exe_atexit_callback` registration, preserves per-thread `_cexit()` TLS callback invocation (`load()`), and resets `g_thread_local_exit` in `run_entry`. Local verification: - `python -I scripts/check_no_game_data.py` on all 5 changed files: exit 0. - Built `gta6rt` + `tiny_atexit` cleanly (zero new warnings). - `ctest -R '^run_tiny_atexit$'`: **3/3 consecutive passes** (`5.09 s`, `4.90 s`, `4.76 s` — `tiny_atexit: 17 termination paths match the Windows sequence`). Looks ready to merge.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1564
No description provided.