Skip to content

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
Mike7581 wants to merge 2 commits into
ProjectLeonida:mainfrom
Mike7581:rt-crt-quick-exit-tls
Open

Mike7581 wants to merge 2 commits into
ProjectLeonida:mainfrom
Mike7581:rt-crt-quick-exit-tls

Conversation

@Mike7581

Copy link
Copy Markdown

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)

@Andretuta Andretuta left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Mike7581 added a commit to Mike7581/leonida that referenced this pull request Nov 25, 2026
… throwing handler terminates

Review follow-up for ubiquitarianism#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.
@Mike7581

Copy link
Copy Markdown
Author

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.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 27, 2026
…back are the run's own

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.
… throwing handler terminates

Review follow-up for ProjectLeonida#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.
@Mike7581
Mike7581 force-pushed the rt-crt-quick-exit-tls branch from 27a2711 to 1dd0890 Compare November 27, 2026 19:16
@Mike7581

Copy link
Copy Markdown
Author

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

main gained #2315 (with the maintainer fix in 48f87a9) 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.

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 27, 2026
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.

2 participants