upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/process.cpp
diff options
context:
space:
mode:
authortotonii <totonicontato@gmail.com>2026-11-22 15:34:07 -0300
committertotonii <totonicontato@gmail.com>2026-11-22 15:34:07 -0300
commit8d9a50759aecdca281d5ff438b30d385e619c77c (patch)
tree8de3f090500e4ca8cc50a532a92b5d0efd10d509 /runtime/shims/process.cpp
parentec8ec4c9a46c6623f1a9c125afd3566da97f1e0c (diff)
fix(process): close g_done race, guard CreateEventW, and preserve ExitThread code in run_entry
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 9da50e4e9..1fc4fee7f 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -206,6 +206,7 @@ int run_entry(void* entry, size_t stack_bytes) {
206 g_exited = false; 206 g_exited = false;
207 g_exit_code = 0; 207 g_exit_code = 0;
208 g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); 208 g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr);
209 if (!g_done) return -1;
209 g_running = true; 210 g_running = true;
210 EntryCall call{entry}; 211 EntryCall call{entry};
211 HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr); 212 HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
@@ -218,7 +219,17 @@ int run_entry(void* entry, size_t stack_bytes) {
218 // The run ends when any game thread exits the process (exit/ExitProcess/abort) or the entry point returns. 219 // The run ends when any game thread exits the process (exit/ExitProcess/abort) or the entry point returns.
219 HANDLE waits[] = {g_done, thread}; 220 HANDLE waits[] = {g_done, thread};
220 WaitForMultipleObjects(2, waits, FALSE, INFINITE); 221 WaitForMultipleObjects(2, waits, FALSE, INFINITE);
221 if (g_exited) WaitForSingleObject(g_done, 5000); 222 bool expected = false;
223 if (g_exited.compare_exchange_strong(expected, true)) {
224 // The entry thread exited without calling end_run (e.g. ExitThread); claim g_exited so no worker thread can
225 // touch g_done while we close it, and preserve the thread's exit code.
226 DWORD thread_code = 0;
227 if (GetExitCodeThread(thread, &thread_code)) g_exit_code = static_cast<int>(thread_code);
228 } else {
229 // Another thread already won g_exited and is publishing g_exit_code + SetEvent(g_done); wait until it finishes
230 // so we never close g_done or return before g_exit_code is published.
231 WaitForSingleObject(g_done, INFINITE);
232 }
222 CloseHandle(thread); 233 CloseHandle(thread);
223 CloseHandle(g_done); 234 CloseHandle(g_done);
224 g_done = nullptr; 235 g_done = nullptr;