process: the game's at_quick_exit handlers and thread-local exit callback are the run's own (RT-CRT-QUICK-EXIT-TLS) #1564
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!1564
Loading…
Reference in a new issue
No description provided.
Delete branch "rt-crt-quick-exit-tls"
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?
Pull request successfully merged and closed
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)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.
Rebased onto current
main(43a9d562d); the branch is now1dd08900fand merges cleanly.maingained #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.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.
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.