diff options
| author | Kevin Morales <32549527+kmorales13@users.noreply.github.com> | 2026-11-21 22:40:34 -0700 |
|---|---|---|
| committer | Kevin Morales <32549527+kmorales13@users.noreply.github.com> | 2026-11-21 22:40:56 -0700 |
| commit | 8b51490ea7e6684b1b8ecf995ee86b5ccc599f58 (patch) | |
| tree | 260dfa4582c73d7398aaa18d9205133502301680 /runtime/shims/process.cpp | |
| parent | dcd8ecf10b138566d567020562b7afd853a89024 (diff) | |
process: park game threads that end the run until the host exits
Under Wine before 11.2, gta6rt --run sometimes exited 0 although the game
ended with 1 (run_tiny_game_exit_code in CI's Wine 9.0 job: 13 of 64 runs).
end_run signalled the host and called ExitThread; the host's ExitProcess
could then send SIGQUIT to that thread while it was exiting, Wine counted
the thread out twice and the process ended with _exit(0). Upstream fixed
it by blocking SIGQUIT during thread exit (4a9bc42a, wine-11.2).
end_run now parks every caller while a run is in progress (the first one
after recording the code and signalling), so no game thread is mid-exit
when the host calls ExitProcess. This also fixes two threads calling exit
at once: the second one called ExitProcess itself, sometimes before the
first had stored its code, and ended the process with 0 on any OS.
CI requires run_tiny_game_exit_code again.
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index e8ec7c34e..e21fb3028 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp | |||
| @@ -4,9 +4,9 @@ | |||
| 4 | // arguments and finally calls exit(); both go through the EXE's own import table, so they can be answered here: | 4 | // arguments and finally calls exit(); both go through the EXE's own import table, so they can be answered here: |
| 5 | // - argv/argc and the command line are the game's (set by run_entry), not gta6rt's; | 5 | // - argv/argc and the command line are the game's (set by run_entry), not gta6rt's; |
| 6 | // - exit(), _exit(), quick_exit(), ExitProcess() and abort() from the game end the game's run, not the host: | 6 | // - exit(), _exit(), quick_exit(), ExitProcess() and abort() from the game end the game's run, not the host: |
| 7 | // the exit code is recorded, the run is signalled as finished and the calling thread ends. atexit handlers | 7 | // the exit code is recorded, the run is signalled as finished and the calling thread waits for the host to |
| 8 | // and static destructors of the game are not run (its CRT shares the host's, whose process-wide atexit table | 8 | // exit. atexit handlers and static destructors of the game are not run (its CRT shares the host's, whose |
| 9 | // also holds gta6rt's own). | 9 | // process-wide atexit table also holds gta6rt's own). |
| 10 | // Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and | 10 | // Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and |
| 11 | // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. | 11 | // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. |
| 12 | #include <windows.h> | 12 | #include <windows.h> |
| @@ -36,6 +36,7 @@ wchar_t** g_wargv_ptr = nullptr; | |||
| 36 | char** g_env = nullptr; | 36 | char** g_env = nullptr; |
| 37 | wchar_t** g_wenv = nullptr; | 37 | wchar_t** g_wenv = nullptr; |
| 38 | 38 | ||
| 39 | std::atomic<bool> g_running{false}; | ||
| 39 | std::atomic<bool> g_exited{false}; | 40 | std::atomic<bool> g_exited{false}; |
| 40 | std::atomic<int> g_exit_code{0}; | 41 | std::atomic<int> g_exit_code{0}; |
| 41 | HANDLE g_done = nullptr; | 42 | HANDLE g_done = nullptr; |
| @@ -62,8 +63,9 @@ std::wstring widen(const std::string& s) { | |||
| 62 | if (g_exited.compare_exchange_strong(expected, true)) { | 63 | if (g_exited.compare_exchange_strong(expected, true)) { |
| 63 | g_exit_code = code; | 64 | g_exit_code = code; |
| 64 | if (g_done) SetEvent(g_done); | 65 | if (g_done) SetEvent(g_done); |
| 65 | ExitThread(static_cast<DWORD>(code)); | ||
| 66 | } | 66 | } |
| 67 | // Park until the host's ExitProcess, not ExitThread: Wine < 11.2 can exit 0 if the two race (4a9bc42a). | ||
| 68 | if (g_running) for (;;) Sleep(INFINITE); | ||
| 67 | // The run is already over: this is game code running during host shutdown (an atexit handler the game | 69 | // The run is already over: this is game code running during host shutdown (an atexit handler the game |
| 68 | // registered with the shared CRT calling exit again). Finish the process with the game's exit code; ending | 70 | // registered with the shared CRT calling exit again). Finish the process with the game's exit code; ending |
| 69 | // just this thread here would make the host's last thread, and so the process, exit with `code` instead. | 71 | // just this thread here would make the host's last thread, and so the process, exit with `code` instead. |
| @@ -153,9 +155,11 @@ int run_entry(void* entry, size_t stack_bytes) { | |||
| 153 | g_exited = false; | 155 | g_exited = false; |
| 154 | g_exit_code = 0; | 156 | g_exit_code = 0; |
| 155 | g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); | 157 | g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); |
| 158 | g_running = true; | ||
| 156 | EntryCall call{entry}; | 159 | EntryCall call{entry}; |
| 157 | HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr); | 160 | HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr); |
| 158 | if (!thread) { | 161 | if (!thread) { |
| 162 | g_running = false; | ||
| 159 | CloseHandle(g_done); | 163 | CloseHandle(g_done); |
| 160 | g_done = nullptr; | 164 | g_done = nullptr; |
| 161 | return -1; | 165 | return -1; |
| @@ -163,9 +167,11 @@ int run_entry(void* entry, size_t stack_bytes) { | |||
| 163 | // The run ends when any game thread exits the process (exit/ExitProcess/abort) or the entry point returns. | 167 | // The run ends when any game thread exits the process (exit/ExitProcess/abort) or the entry point returns. |
| 164 | HANDLE waits[] = {g_done, thread}; | 168 | HANDLE waits[] = {g_done, thread}; |
| 165 | WaitForMultipleObjects(2, waits, FALSE, INFINITE); | 169 | WaitForMultipleObjects(2, waits, FALSE, INFINITE); |
| 170 | if (g_exited) WaitForSingleObject(g_done, 5000); | ||
| 166 | CloseHandle(thread); | 171 | CloseHandle(thread); |
| 167 | CloseHandle(g_done); | 172 | CloseHandle(g_done); |
| 168 | g_done = nullptr; | 173 | g_done = nullptr; |
| 174 | g_running = false; | ||
| 169 | return g_exit_code.load(); | 175 | return g_exit_code.load(); |
| 170 | } | 176 | } |
| 171 | 177 | ||