Repository navigation
Conversation
Andretuta
left a comment
There was a problem hiding this comment.
The termination order is right, and it matches the UCRT's common_exit:
- the TLS callback runs with
DLL_PROCESS_DETACHbefore theatexittable inexitand_cexit; quick_exitdrains only theat_quick_exittable;- a second
_register_thread_local_exe_atexit_callbackterminates.
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
_cexitran their thread-local destructor callbacks at the same time. - A second thread that called
at_quick_exitduring aquick_exitdrain 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
_cexitreturning. - 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), theloader*tests and allrun_tiny_*tests. The 5 failures are in code this PR does not touch:root_sig,xmem,xmem_no_write_watchandroot_signature_oraclefail the same way on the other PR branches built here.xmem_write_watchfailed only on this run and passed on the other two branches.
- Native vs mapped probes for concurrent
_cexit, cross-threadat_quick_exitregistration and a throwing quick handler, compared against the SDK's UCRTexit.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.
… 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.
|
Thanks for the probes against the UCRT. Both points are addressed in 27a2711:
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. |
…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.
27a2711 to
1dd0890
Compare
|
Rebased onto current
Tested on Windows 11, RTX 4050 Laptop, MSVC RelWithDebInfo:
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. |
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.cppandruntime/README.mdlisted two gaps: the game'sat_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 undergta6rt --runbefore this change:quick_exit()ran none of the game'sat_quick_exithandlers;exit()and_cexit()never ran the exiting thread'sthread_localdestructors (and a second registration in the host's one-slot callback would terminate gta6rt).Now:
_crt_at_quick_exitgoes 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 sharinggame_quick_exit).exit()and_cexit()call it withDLL_PROCESS_DETACHbefore the atexit handlers, as the UCRT does. A second registration aborts, as the UCRT'sterminate()does. Both reset per run.Tested
tiny_atexitgains five modes:at_quick_exit,at_quick_exit_exit,at_quick_exit__exit,thread_local_exit,thread_local_cexit. Their expected sequences incheck_atexit.cmakecome from running the fixture natively on Windows 11 (the test also holds the native run to them). Before the fix,--rundiffered onat_quick_exit(no handler ran),thread_local_exitandthread_local_cexit(nodtor 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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)