process: the game's at_quick_exit handlers and thread-local exit callback are the run's own (RT-CRT-QUICK-EXIT-TLS) #1564
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1564"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.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)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:DLL_PROCESS_DETACHbefore theatexittable inexitand_cexit;quick_exitdrains only theat_quick_exittable;_register_thread_local_exe_atexit_callbackterminates.run_tiny_atexitpasses 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 onmain, 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 headc5b51e9.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) releasesg_atexit_lockbefore each call. Two probes on the PR head showed the gap:_cexitran their thread-local destructor callbacks at the same time.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:
_cexitreturning.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:terminate, exit code 3 with the default handler--runon the PR head0xE06D7363Suggested fix: wrap each guest cleanup callback the way the UCRT does, and send it to the run's own termination path.
Add a
tiny_atexitmode with a throwingat_quick_exithandler, and compare it with the native run.What was checked
main: clean.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._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.
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:exit,quick_exitand_cexitnow 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,_cexitstill returns, and the lock is released inend_runbefore the mapped run ends (also when a handler ends the run from inside a drain).game_abort, exit code 3, as the UCRT'sterminate()would.tiny_atexitadds four modes, each held to the native sequence:concurrent_cexit(a second thread's_cexit, with athread_localof its own, waits for the first cleanup),quick_exit_register(anat_quick_exitfrom a second thread during aquick_exitdrain waits, so its handler never runs),quick_exit_throwandexit_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 onc5b51e9through--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.
Imported from ubiquitarianism/leonida. Originally posted by Mike7581 on 2026-11-27T19:18:38Z.
Rebased onto current
main(43a9d562d); the branch is now1dd08900fand merges cleanly.maingained #2315 (with the maintainer fix in48f87a9c9) while this was open. It owns the EXE's thread-local exit callback, which overlaps the first commit here. I kept one implementation:process.cpphas a singlegame_register_thread_local_exe_atexit_callback, the one from this PR (a second registration terminates). The callback is called inside the cleanup lock, before theatexitdrain, inexitand_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.main:maincalled the callback once per run (exchange). This PR calls it on everyexit/_cexit.concurrent_cexitsettles it against Windows. Natively, the second thread'sthread_localends inside its own_cexit(), beforesecond _cexit returned. Withmain'sprocess.cppit ends only at thread detach, after that line.tiny_atexit:thread_local_exit/thread_local_cexitstay exactly as onmain(codes 13/14). This PR'sat_quick_exitmodes 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_atexitpasses: 17 paths, native and--run, no native differences.main'sruntime/shims/process.cppswapped in, 4 modes fail:at_quick_exit,concurrent_cexit,quick_exit_register,quick_exit_throw.exit_throwpasses onmaintoo.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.
Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-28T03:48:14Z.
Review: APPROVED
Re-checked at
1dd08900fmerged cleanly onto currentmain(02f71ccfe, Windows 10 x64, MSVC 14.44 RelWithDebInfo, Ninja).All items from the previous review and the rebase over #2315 are addressed cleanly:
exit/quick_exit/_cexitcleanup now serialize on one run-local recursiveg_cleanup_lock(released inend_runand before parking peer threads intake_exit_lock).guarded) routing escaping exceptions togame_abort()(exit code 3), matching UCRTterminate()._register_thread_local_exe_atexit_callbackregistration, preserves per-thread_cexit()TLS callback invocation (load()), and resetsg_thread_local_exitinrun_entry.Local verification:
python -I scripts/check_no_game_data.pyon all 5 changed files: exit 0.gta6rt+tiny_atexitcleanly (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.