Skip to content

process shim: the game thread that ends a run no longer exits the process before gta6rt finishes - #4043

Open
AAyberkU wants to merge 1 commit into
ItsNotRexed:mainfrom
AAyberkU:process-end-run-park-race
Open

AAyberkU wants to merge 1 commit into
ItsNotRexed:mainfrom
AAyberkU:process-end-run-park-race

Conversation

@AAyberkU

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #3918 (comment) (new RT item)

What and why

end_run (runtime/shims/process.cpp) signalled g_done and only then read g_running. run_entry wakes on g_done, closes its handles and sets g_running = false, so the game thread that ended the run could read false and take the "run is already over" path, calling ExitProcess while main.cpp was still finishing. That lost the game exited with code N line, report_called_unimplemented(), the trace exit event and cleanup_tls_clones(). The exit code stayed right, but check_startup_cmdline.cmake, check_loaded_*.cmake and check_game_dir.cmake require that line, so their tests failed now and then.

The fix reads g_running before signalling. It is set before the game thread is created and cleared only after g_exited is claimed and g_done is set, so the snapshot is true for any thread that ends a live run. The "exit during host shutdown" path (no run active) still calls ExitProcess, and so does an exit before any run started. (Making the CAS winner always park would have hung that case.)

process_cmdline_test (already registered as the process_cmdline ctest, so runtime/CMakeLists.txt is untouched) gains a regression check: pinned to one CPU, 50 run_entry calls of an entry point that returns 77, with a Sleep(1) after each. Without the fix the test process exits with code 77 on the first one.

Tested

Windows 11 Pro, VS2022 MSVC 14.44.35207, CMake 3.31.6, Ninja, RelWithDebInfo. CPU only: no GPU tests, no game files.

AI-assisted (Claude Code); the runs above were done on this branch.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

🤖 Generated with Claude Code

…cess before gta6rt finishes

end_run signalled g_done and only then read g_running. run_entry wakes on g_done, closes it and clears
g_running, so the game thread could read false and take the "run is already over" path: ExitProcess with
the game's code while main.cpp was still printing "game exited with code N", reporting unimplemented
imports, writing the trace "exit" event and cleaning up TLS clones. The exit code stayed right, but the
lost line made run_startup_cmdline and the other checks that look for it flaky.

end_run now reads g_running before it signals. g_running is set before the game thread starts and is
cleared only after g_exited is claimed and g_done is set, so a read taken before the CAS is true for any
thread that ends a live run. The exit during host shutdown (no run active) still calls ExitProcess, and
so does an exit with no run started, which an "always park the CAS winner" fix would have hung.

process_cmdline_test runs 50 entry points that return 77 on one CPU and fails (exit 77) without the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested b6299c9d on Windows 11, MSVC 19.44.35228 + Ninja, RelWithDebInfo.

  • process_cmdline_test.exe -> exit 0; its new end-of-run block runs 50 entry-point returns with the process affinity pinned to one CPU; the existing command-line checks still pass.
  • Repeated the whole test 10 times (500 run-end iterations): 0 failures.
  • ctest -R process_cmdline -> 1/1 passed, 0.79 s.

The fix reads g_running before SetEvent(g_done), closing the window where run_entry could clear it and the game thread would take the ExitProcess path while main.cpp was still printing/cleaning up. Parking on the observed value is the right minimal change; no game files, no blocker.

@MKmajster

Copy link
Copy Markdown

Tested b6299c9 on Windows 11 24H2 (26100), MSVC 19.44 RelWithDebInfo + Ninja: pass. I found the same race independently while reproducing TEST-874 (comment in #3918), with the same two-line fix, so this is also a measurement of it:

  • Out-of-process harness: gta6rt --game-dir . --run tiny_game.exe -- hello 42 game-file from its folder, 8 at a time from Python, checking the output ends with game exited with code 0. Before (main aa2988c): 6 of 400 truncated (...tiny_game: OK\ngame e), exit code 0. This branch: 0 of 1,200, with one merged pipe and with separate pipes.
  • ctest -R "run_tiny|process_cmdline" -j 8 --repeat until-fail:40: 480/480 passed. Without the fix, run_tiny_game_dir failed within 25 parallel repeats here.
  • Negative control for the new check: this branch's process_cmdline_test.cpp with main's process.cpp exits 77, as described. With the fix process_cmdline passes.

One nit: 77 is the code this tree uses for SKIP (SKIP_RETURN_CODE 77 on many tests). process_cmdline has no SKIP_RETURN_CODE, so today 77 fails it as it should. But if someone ever adds one, the regression would show up as a skip. An entry-point return value other than 77 would avoid that.

No game files. AI-assisted (Claude).

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at b6299c9. The change keeps the host process alive until the runtime's completion signal is observed; I found no additional blocking issue beyond the existing comment about the test's return code. The PR reports 1,200 concurrent process runs and 480 repeated CTest executions passing on Windows.

@requiemsnkrp

Copy link
Copy Markdown

Tested b6299c9d (merged onto main aa2988cc) on NVIDIA GeForce RTX 3060, driver 32.0.16.1714, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0: pass.

  • ctest -R "^(process_cmdline|run_startup_cmdline|run_tiny_async|run_tiny_gdk_load|run_tiny_bundled_load|run_tiny_game_dir)$" --repeat until-fail:50 -j 4 with LEONIDA_VK_VALIDATION=1: 100% tests passed, 0 tests failed out of 6 (50 rounds each, about 42 s).
  • Full ctest (-E "present|swapchain|frame_pacer", llvm-mc build): 100% tests passed, 0 tests failed out of 148 (9 skipped/disabled, same set as main: shader_library_corpus, bink_decode, bink_play_headless, fsr_dispatch, bvh_builder, bink_audio, gameinput, d3d12_rgb9e5_rtv, d3d12_first_light).
  • Context: on this machine run_tiny_game_dir failed now and then under -j 4 on main-based trees (2 of my last ~20 full runs, GAME: did not resolve to the game folder). That fits the race described here. It did not fail in the runs above, but I did not run main with --repeat as a control, so that is consistent with the fix rather than proof of it.

AI-assisted (Claude). No game files.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested b6299c9 (merged onto main aa2988c) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass

  • Merges cleanly; no new compiler warnings against main.
  • ctest: 144 of 144 pass (same 9 skips as main). Loader test passes; validation run: 56 tests clean.
  • process_cmdline with --repeat until-fail:50: 50 of 50 pass.
  • run_startup_cmdline, run_tiny_async, run_tiny_gdk_load, run_tiny_bundled_load and run_tiny_game_dir with --repeat until-fail:100 -j4: all 500 runs pass.
  • I also built this PR's process_cmdline_test.cpp against main's process.cpp and ran it 20 times: 20 of 20 fail, each with exit code 77, as in your table. So the new check fails on main and passes here.

AI-assisted (Claude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants