Skip to content

Runtime: a game thread that ends the run no longer exits the host mid-line (TEST-874) - #4052

Closed
undeadhuman37 wants to merge 1 commit into
ItsNotRexed:mainfrom
undeadhuman37:test-874-ctest-race
Closed

undeadhuman37 wants to merge 1 commit into
ItsNotRexed:mainfrom
undeadhuman37:test-874-ctest-race

Conversation

@undeadhuman37

Copy link
Copy Markdown
Contributor

TEST-874 (claim). Depends on: nothing open. Builds on @tedin7's notes in the claim thread.

Cause

run_tiny_game_dir failed under ctest -j because gta6rt was killed partway through its last line. The failing output ends game exited w (or g, game exite, ...), and the exit code is still 0.

The race is in end_run (runtime/shims/process.cpp). A game thread that calls exit/ExitProcess:

  1. sets g_exit_code and g_done;
  2. then reads g_running to decide whether to park.

If the thread is preempted between those two steps, the following happens:

  • run_entry wakes, clears g_running and returns.
  • The host prints game exited with code 0.
  • The game thread resumes, sees false, takes the "run already over" branch and calls ExitProcess(g_exit_code), which ends the host mid-line.

check_game_dir.cmake captures stdout and stderr into one variable and checks the game exited with code 0 line, so the missing tail fails the test. The other tiny-game tests don't check that line.

Fix

end_run reads g_running before it sets g_done, so the thread that ends the run always parks. The atexit case (game code calling exit after run_entry returned) still sees false and exits with the game's code, as before.

How it was found and checked (RTX 4070 SUPER, driver 621.01, Windows 11, MSVC 14.44)

  • Reproduced on main: ctest -j 8 -R "run_tiny|winhttp" --repeat until-fail:30 failed within one or two rounds.
  • Isolated outside CTest: 3,000 parallel runs of gta6rt --game-dir . --run tiny_game.exe -- hello 42 game-file, with stdout and stderr on one pipe, cut the last line in about 3 per 1,000 runs. With separate pipes, none of 400 did.
  • Located with a temporary probe: fflush succeeded, the stdout handle was the same object before and after the run, and the probe's own log after printf was never written. So the process ended inside printf, which points to another thread's ExitProcess.
  • After the fix:
    • 0 of 6,000 merged-pipe runs bad.
    • ctest -j 8 -R "run_tiny|gta6rt_" --repeat until-fail:60 passes all 60 rounds.
    • Full ctest: 146/146 passed.
    • compileall and make_game.py --help: OK.

Not covered

winhttp_offline failed once in my first -j 8 runs (every handle reports HANDLE_CLOSING once, closing the request inside the callback reports HANDLE_CLOSING once). It doesn't use gta6rt, so it's a separate race in the WinHTTP shim's close callbacks; I'm posting it as a finding rather than mixing it in here. It passed 60 rounds afterwards, so it's rarer.

AI-assisted (Claude Code). No game files used.

🤖 Generated with Claude Code

…-line (TEST-874)

end_run read g_running after setting g_done. If the thread was preempted between
the two, run_entry returned, cleared g_running and the host went on printing;
the game thread then saw false and called ExitProcess with the game's exit code,
which ended gta6rt partway through "game exited with code 0". With stdout and
stderr on one pipe (CMake's execute_process in check_game_dir.cmake) and ctest
-j, run_tiny_game_dir failed intermittently. g_running is now read before g_done
is set, so the thread that ends the run always parks.

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

Copy link
Copy Markdown

Overlap note: #4043 (opened a few minutes earlier) has the same change to end_run, reading g_running into a local before SetEvent and parking on it. It also adds a regression check to process_cmdline_test (50 run_entry calls pinned to one CPU; without the fix the process exits 77).

I measured that code on Windows 11, MSVC 19.44 (results on #4043): truncated game exited with code 0 lines went from 6 of 400 parallel runs on main to 0 of 1,200, and run_tiny* passed 480/480 under -j 8 --repeat. Since the diff here is the same two lines, that covers this branch's fix too. The project's rule is that whichever lands first is merged and the other closes, so only one of the two is needed.

AI-assisted (Claude).

@0xTehuti 0xTehuti 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 b35b18fc on Windows 11 Pro build 26100, MSVC 19.44 (VS 2022 Community), Ninja 1.13, CMake 4.4.

1. Concurrency Stress Test Verification (PASS)

Ran ctest -j 4 -R "run_tiny|process" --output-on-failure --repeat until-fail:20:

  • Result: 12/12 tests passed (100%), 0 failures across all 20 iterations (2.22s real time).
  • Tests covered:
    • process_cmdline
    • run_tiny_async
    • run_tiny_gdk_load
    • run_tiny_bundled_load
    • run_tiny_game
    • run_tiny_game_console_paths
    • run_tiny_game_exit_code
    • run_tiny_game_fixed
    • run_tiny_game_fixed_exit_code
    • run_tiny_game_tls
    • run_tiny_game_eh
    • run_tiny_game_dir

Capturing const bool running = g_running; before signaling SetEvent(g_done) ensures game threads that entered during active run cleanly park on Sleep(INFINITE) and do not prematurely invoke ExitProcess while the host's run_entry is flushing its final messages.

Resolves TEST-874. LGTM.

@undeadhuman37

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #4043 (@AAyberkU), which was opened five minutes earlier with the same end_run change and adds a regression test. @MKmajster, thanks for spotting it. For #4043's reviewers, this is what I measured on main vs the fix (RTX 4070 SUPER, 621.01, Windows 11, MSVC 14.44): gta6rt --game-dir . --run tiny_game.exe with stdout and stderr on one pipe, 12 at a time, cut the last line in about 3 per 1,000 runs before, and in 0 of 6,000 after. ctest -j 8 -R "run_tiny|gta6rt_" --repeat until-fail:60 passed all 60 rounds. This is also the cause of TEST-874's run_tiny_game_dir flake.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested b35b18f on Windows 11 (26200), MSVC (VS 2022 Build Tools) RelWithDebInfo build of this head (CPU only, no GPU involved): ctest -j 4 -R "run_tiny|gta6rt_" --repeat until-fail:30 — pass, 14/14 tests, each repeated up to 30 times with no failure (no truncated game exited ... line).

Run at -j 4 to keep the load on this machine down, not the -j 8 from the description. main was not run here for the before side.

AI-assisted.

@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.

The �nd_run hunk here is functionally identical to #4043: both read g_running before SetEvent(g_done) and park based on that snapshot. #4043 also adds the regression test in process_cmdline_test.cpp and has already been reviewed. Please coordinate with that PR or consolidate the fix so the same change is not kept open twice. I found no distinct code change in this diff.

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.

5 participants