Repository navigation
RT-374: detach initialised modules before ExitProcess in gta6rt --run - #5161
tomgamer22 wants to merge 6 commits into
Conversation
Preserve upstream Unicode-safe console diagnostics alongside entry-thread startup. Retain merged process completion and TLS lifecycle changes. Verified focused TLS/rollback tests, CPU CTest and native/upstream/fixed synthetic comparisons.
Kam471
left a comment
There was a problem hiding this comment.
Blocking: the new immediate-exit test fails on Windows 11 in all four modes.
Built at 1a29f898d (MSVC RelWithDebInfo) and ran ctest -R 'startup_rollback|tls_startup|tls_immediate_exit':
3/4 passed; 58 - run_tls_immediate_exit ... Failed (0.08 s)
[_exit] expected (8): tls_attach;main;tls_detach=0x42
--run (8): tls_attach;main;tls_detach=0x11
[_Exit] expected (9): tls_attach;main;tls_detach=0x42
--run (9): tls_attach;main;tls_detach=0x11
[quick_exit] expected (10): tls_attach;main;tls_detach=0x42
--run (10): tls_attach;main;tls_detach=0x11
[ExitProcess] expected (11): tls_attach;main;tls_detach=0x42
--run (11): tls_attach;main;tls_detach=0x11
The native runs match; only the gta6rt --run path differs, and both the exit code and the attach/main lines are right. The detach callback reads the TLS initializer 0x11 instead of the value main wrote (0x42) — note it does not even read attach's 0x22, so the callback is running against a different (fresh/static) TLS block for the module, not the thread's block.
startup_rollback and both tls_startup_* tests pass, and startup_rollback_test prints usage when run without arguments (exit 2), as designed. So the rollback logic looks fine; the new detach path in process.cpp/main.cpp needs to invoke the mapped image's TLS callbacks in a context where _tls_index resolves to the guest thread's block (or defer to the loader's existing thread-detach path) before this can merge.
git diff --check clean. This is the only failure in the new tests I ran.
… SetEvent(g_done)
|
Thanks @Kam471 for testing Indeed, Pushed a fix ( AI-assisted (Antigravity). |
Kam471
left a comment
There was a problem hiding this comment.
Fixed at ac90c998 (run detach_initialised_modules on exiting guest thread before SetEvent(g_done)): the mapped detach callback now sees the thread's TLS block.
Rebuilt and reran on Windows 11 (MSVC RelWithDebInfo):
ctest -R 'startup_rollback|tls_startup|tls_immediate_exit'-> 4/4 passed:startup_rollback,tls_startup_native,tls_startup_mapped,run_tls_immediate_exit.run_tls_immediate_exitnow reportstls_attach;main;tls_detach=0x42with exit codes 8/9/10/11 for_exit/_Exit/quick_exit/ExitProcess, matching the native run in all four modes.git diff --checkclean.
The earlier failure (tls_detach=0x11, the initializer) is gone. No blocking issue.
|
Independent check — FAIL: the new ctest is red at head (Windows 11, MSVC, native run — no Wine).
The sibling TLS tests ( AI-assisted (Devin); fixture is hand-written test code, no game files. |
|
Windows retest of Proposed test-only adjustment below. Accept the two observed native sequences and keep the mapped detach check strict. With it, CTest passes at --- a/runtime/test/check_tls_immediate_exit.cmake
+++ b/runtime/test/check_tls_immediate_exit.cmake
@@ -1,9 +1,11 @@
# PE TLS DLL_PROCESS_DETACH on immediate-termination paths (_exit, _Exit, quick_exit, ExitProcess).
-# Runs tls_immediate_exit natively and through `gta6rt --run` and verifies both emit tls_attach, main, and
-# tls_detach=0x42 (without running atexit) and return the expected exit code.
+# Runs tls_immediate_exit natively and through `gta6rt --run`. Both must attach, run main, and return the
+# expected exit code without running atexit. Native Windows may omit EXE TLS detach. When present it must
+# see 0x42, and the mapped launch always requires detach with that value.
get_filename_component(guest_dir "${GUEST}" DIRECTORY)
get_filename_component(guest_name "${GUEST}" NAME)
+set(expect_native_no_detach "tls_attach;main")
set(expect_seq "tls_attach;main;tls_detach=0x42")
set(code__exit 8)
set(code__Exit 9)
@@ -33,9 +35,10 @@
RESULT_VARIABLE run_code OUTPUT_VARIABLE run_out ERROR_VARIABLE run_err)
guest_lines("${native_out}" native_lines)
guest_lines("${run_out}" run_lines)
- if(NOT native_code STREQUAL code_${mode} OR NOT native_lines STREQUAL expect_seq)
+ if(NOT native_code STREQUAL code_${mode} OR
+ (NOT native_lines STREQUAL expect_native_no_detach AND NOT native_lines STREQUAL expect_seq))
string(CONCAT native_diff "[${mode}] native run differs from the expected sequence"
- "\n expected (${code_${mode}}): ${expect_seq}\n native (${native_code}): ${native_lines}")
+ "\n expected (${code_${mode}}): ${expect_native_no_detach}, optionally followed by tls_detach=0x42\n native (${native_code}): ${native_lines}")
if(RUNNER)
message(STATUS "${native_diff}\n (the host CRT/loader differs from Windows; not a gta6rt failure)")
else() |
|
Thanks @Ingenioeren for retesting (And thanks @Atomic258 — your AI-assisted (Antigravity). |
|
Tested cca7612 on Intel UHD 770, driver 32.0.101.7088, Windows 11, MSVC 19.44, at the stack head: the eb47393 and bb561f5 commits from #3790 are ancestors of this head, and #3790 is still open. gta6rt, tls_immediate_exit and startup_rollback_test build with exit 0. With LEONIDA_VK_VALIDATION=1 set, ctest -R 'run_tls_immediate_exit|startup_rollback' passes: run_tls_immediate_exit prints "tls_immediate_exit: 4 immediate-exit paths delivered PE TLS DLL_PROCESS_DETACH" (Test #58, 1.64 sec), and startup_rollback (Test #50, 2.91 sec) prints exit=0 for native_load, native_executable, loader, loader_reentrant, loader_success, loader_success_reentrant, loader_destructor and loader_destructor_reentrant, and exit=1 for mapped and mapped_reentrant. The first startup_rollback run failed with "native_load returned 1" until the startup_rollback_exe and startup_rollback_native targets were built. I did not run the base without this change, and I did not confirm that the validation layer loaded. AI-assisted. |
|
@tomgamer22 — I checked a remaining shutdown-ordering case at A synthetic EXE starts a worker that increments a DLL-global interlocked heartbeat. During
The worker can still execute while mapped detach callbacks run. The shutdown callback in I prepared an experimental review branch on #3790, with original synthetic fixtures and a CTest runner: The experiment hands native The branch still needs integration with current main's newer CRT/atexit work, and targeted audit/stress of loader/registry locks held by workers at termination. Direct Could the worker-quiescence probe be included in the existing RT-374 / TEST-1168 integration? The branch is available for review/adaptation; a follow-up PR remains to be coordinated with you. |
Hiiva
left a comment
There was a problem hiding this comment.
TEST-1168: built the actual gta6rt target at exact head cca7612264e7443c9c13dde59dca1ada2cce6adf on Windows 11 x64, MSVC 19.44.35228 / SDK 10.0.26100.0. Two process-shutdown issues remain.
-
Process termination is reported to DLLs as explicit unload.
shutdown_for_run()inruntime/src/main.cppcallsLoader::detach_initialised_modules(), whose DllMain call atruntime/src/pe_loader.cpp:611unconditionally passesnullptr. DllMain's contract distinguishes process termination with a non-null reserved pointer; null describes explicit unload or failed loading. A DLL can therefore take its unload cleanup branch during process termination. Keep startup rollback/destructor behavior distinct when adding a termination reason. -
Other guest threads still execute during process detach. This independently confirms the worker-heartbeat finding using the full runtime target.
end_run()callsg_shutdownbeforeSetEvent(g_done)and before nativeExitProcesshas stopped other threads. ExitProcess's documented ordering stops those threads before process-detach calls. DLL cleanup can currently race guest code using the resources it releases.
An original synthetic DLL/EXE produced these results. Each native and mapped process returned the indicated expected exit code:
| Route | Exit code | Native reserved / worker progress | Exact-head mapped reserved / worker progress |
|---|---|---|---|
_exit |
8 | non-null / 0 | null / 1 |
_Exit |
9 | non-null / 0 | null / 1 |
quick_exit |
10 | non-null / 0 | null / 1 |
ExitProcess |
11 | non-null / 0 | null / 1 |
The worker first signals readiness, then waits on an initially unsignaled kick event. Its only subsequent action before parking is to signal an initially unsignaled acknowledged event. DllMain does this during DLL_PROCESS_DETACH:
SetEvent(kick);
const DWORD progress = WaitForSingleObject(acknowledged, 1000);
// progress == WAIT_OBJECT_0 proves the worker executed after detach began.This is a bounded diagnostic handshake, not a proposed DllMain implementation. No conclusion relies on GetExitCodeThread or whether the thread handle was signaled.
The same result occurs in a local integration with #5039/#5074 and the other completed fixes. Its eight selected CTests pass: process command line, startup rollback, native/mapped TLS startup, quick-exit/atexit, thread-local destructors, immediate exit, and console current directory. Those passing tests do not cover these two contracts.
I am checking the linked experimental branch's compatibility with the newer CRT cleanup work under the existing TEST-1168 claim. No competing implementation PR. Synthetic files only; no GPU, Wine, or game-file execution.
AI-assisted: yes.
throny
left a comment
There was a problem hiding this comment.
Tested cca7612 on NVIDIA RTX 4080, driver 610.88, Windows 11: pass
Build: MSVC 19.44.35230, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363. Merge base 3faac557 (= current main). This is a native MSVC run; the earlier Windows checks used clang-cl and MSVC 19.43/Intel.
ctest -R "startup_rollback|tls_startup|tls_immediate|atexit|loader_tls|run_tiny|startup": 20/20 pass (startup_rollback,tls_startup_native,tls_startup_mapped,run_tls_immediate_exit,run_tiny_atexit,loader_tlsand therun_tiny_game*set among them).- Full
ctest -j 6: 181/181 pass (9 skip themselves). - Native
tls_immediate_exit.exebuilt by MSVC 19.44 printstls_attach;main;tls_detach=0x42for all four modes (_exit,_Exit,quick_exit,ExitProcess). So with MSVC the native run takes theexpect_seqbranch, and the relaxed native check fromcca76122isn't needed here. It does no harm. - Negative control:
runtime/src/main.cpp,runtime/shims/process.cppandruntime/shims/process_shim.hfrom56d84999(the #3790 merge, before the RT-374 commits), with the PR's fixture and test →run_tls_immediate_exitfails in all four modes,--rungivingtls_attach;mainwithouttls_detach=0x42. With the head restored it passes.
I read the whole diff (#3790's part included). Code note, the same case l-CatDemon-l raised above: end_run now calls g_shutdown → detach_initialised_modules() on the exiting thread while the game's other threads are still running. On Windows, ExitProcess terminates the other threads before it delivers DLL_PROCESS_DETACH, so a detach handler there never runs concurrently with a game worker that uses the same DLL state. Here it can. I didn't build a reproduction for this; I'm noting that the code reads that way at runtime/shims/process.cpp (end_run, the g_running && g_shutdown branch). Nothing else found.
AI-assisted.
|
Experimental-branch follow-up for @l-CatDemon-l's Windows 11 x64, MSVC 19.44.35228, SDK 10.0.26100.0, Python 3.14.6, RelWithDebInfo. CPU-only synthetic fixtures. Positive integration results:
The remaining lookup-lock concern has a focused synthetic reproduction. A test-only scheduling control exposes the existing private
The held case prints the DLL's initial detach marker with a non-null reserved pointer, then omits both lookup completion and the later EXE TLS callback. Markers use direct This establishes callback truncation under a deliberately forced ownership schedule. Natural occurrence frequency and the mechanism that cuts termination short remain unmeasured. The source allows the ownership window: Independent review of both the source window and scratch harness found no harness-only explanation for the held/unheld difference. No production fix was added for either remaining lock concern. All inputs and probes are original synthetic code; no game files. AI-assisted: yes. |
|
Thanks @Hiiva, @l-CatDemon-l and @throny. Both points are right: process termination should reach DllMain with a non-null @l-CatDemon-l's branch already does the thread-quiescent hand-back to native AI-assisted (Claude Code). |
|
Closing as superseded by #5565 (@l-CatDemon-l, who now holds RT-374). That PR carries the native exit hand-off forward and covers both points in @Hiiva's review. Thanks to everyone who tested and reviewed this one. AI-assisted (Claude Code). |
Claim: #5156 (comment)
Depends on: #3790
Problem (
RT-374)In
runtime/src/main.cpp,wmainendsgta6rt --runby callingrt::cleanup_tls_clones()andExitProcess(static_cast<UINT>(code))directly whilert::Loader loaderis still in scope. BecauseExitProcessterminates the host process beforeLoader::~Loader()runs, mapped modules never receive PE TLSDLL_PROCESS_DETACHnotifications (orDllMain(DLL_PROCESS_DETACH)) when the mapped EXE exits via immediate-termination paths (_exit,_Exit,quick_exit,ExitProcess).Fix
#3790(Loader::detach_initialised_modules()) ontoorigin/main(resolving theruntime/shims/process.cppmerge conflict withorigin/main'sg_atexit.clear()/g_exiting_threadreset).runtime/src/main.cpp, callloader.detach_initialised_modules()and flushstdout/stderrbeforert::cleanup_tls_clones()andExitProcess(static_cast<UINT>(code)).runtime/test/fixtures/tls_immediate_exit.cppand CMake testrun_tls_immediate_exit(runtime/test/check_tls_immediate_exit.cmake) comparing native vsgta6rt --runacross_exit,_Exit,quick_exit, andExitProcess.Testing
git diff --checkand LF line endings locally.ctestexecution (run_tls_immediate_exit) are left to CI and Windows testers.AI-assisted (Antigravity).
Generated by Antigravity (Gemini).