fix(shared): stop run_game masking crashes with sys.exit in finally - #851
Merged
Conversation
sys.exit() in the finally block raised SystemExit, which replaced any in-flight exception. Real crashes (display init failure, asset load failure, bugs in game.run()) were silently discarded and the process exited 0, reporting success. Confirmed on a Raspberry Pi running Batocera: Force Field failed pygame.display.set_mode and exited 0 with no traceback; the underlying pygame.error was only recoverable by constructing the Game class directly, bypassing run_game. Log the exception with a traceback and re-raise it, and move sys.exit() out of finally so it only runs on the success path. Affects every game routed through run_game -- Force Field, Duum, Zombie Survival. Adds regression tests (written failing first) asserting that exceptions from both game construction and game.run() propagate out of run_game rather than becoming a clean exit. These deliberately do not patch sys.exit -- patching it is what hid the bug from the existing tests.
dieterolson
enabled auto-merge (squash)
July 27, 2026 15:10
Contributor
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
run_game()insrc/games/shared/game_launcher.pycalledsys.exit()inside afinallyblock:sys.exit()raisesSystemExit, which replaces any in-flight exception. Any real crash — asset load failure, display init failure, a bug ingame.run()— was silently discarded and the process exited 0, reporting success.Confirmed empirically on a Raspberry Pi running Batocera: Force Field failed to create its display and exited 0 with no traceback whatsoever. The underlying error (
pygame.error: <driver> not availablefrompygame.display.set_modeinsrc/games/shared/fps_game_base.py:133) was only recoverable by constructing the Game class directly, bypassingrun_game.This affects every game routed through
run_game()— Force Field, Duum, and Zombie Survival at minimum.Fix
Log the exception with a traceback and re-raise it, and move
sys.exit()out offinallyso it only runs on the success path:sys.exit()is kept on the success path rather than dropped entirely, so normal-exit andKeyboardInterruptbehavior is unchanged — both pre-existing tests pass unmodified. The contract is documented in aRaises:block per the repo's DbC convention.Tests
Three regression tests added (written failing first, per the repo's TDD convention):
game.run()propagates out ofrun_game()set_modefails in__init__exc_infobefore being re-raisedThese deliberately do not patch
sys.exit— patching it is precisely what hid this bug from the existing test suite.Verification
tests/shared/test_game_launcher.py11/11 passing.pre-commit run --fileson both changed files: all hooks passed.pygame.errorraised from__init__): now prints a full traceback and exits 1. Before this change: exit 0, silent.