about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-07-04 18:58:44 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-07-04 18:58:44 +0300
commitb0fedba6317d5af1768403d79b2ef61b7d78230b (patch)
tree804514a8fb83a40e7ddbde78f9832bac38a8a25c
parent37ed5375459b04a8974063b67ebaaeca5c2ab0c5 (diff)
downloadfungus-launcher-b0fedba6317d5af1768403d79b2ef61b7d78230b.tar.gz
fungus-launcher-b0fedba6317d5af1768403d79b2ef61b7d78230b.tar.xz
properly deal with game process errors (broke after refactoring/split)
-rw-r--r--fungus/game_launcher.py34
-rw-r--r--fungus/run_manager.py42
2 files changed, 65 insertions, 11 deletions
diff --git a/fungus/game_launcher.py b/fungus/game_launcher.py
index 17c0e7d..7ce0f6e 100644
--- a/fungus/game_launcher.py
+++ b/fungus/game_launcher.py
@@ -102,6 +102,7 @@ class GameLauncher:
         on_proton_missing: Callable[[], None] | None = None,
         on_proton_path_missing: Callable[[str], None] | None = None,
         on_spawn_error: Callable[[str], None] | None = None,
+        on_process_fail: Callable[[int, list[str]], None] | None = None,
         on_splash_close: Callable[[], None] | None = None,
         on_process_started: Callable[[], None] | None = None,
     ):
@@ -118,6 +119,7 @@ class GameLauncher:
         self._on_proton_missing = on_proton_missing
         self._on_proton_path_missing = on_proton_path_missing
         self._on_spawn_error = on_spawn_error
+        self._on_process_fail = on_process_fail
         self._on_splash_close = on_splash_close
         self._on_process_started = on_process_started
         self.load_config()
@@ -289,6 +291,7 @@ class GameLauncher:
 
         # ── Inner command ───────────────────────────────────────────
         inner_cmd: list[str] = [umu_run, game.path]
+        # inner_cmd: list[str] = ["/bin/bash", "-c", f"{umu_run} \"{game.path}\" ; exit 100"]
         if game.game_arguments:
             inner_cmd.append(game.game_arguments)
 
@@ -437,8 +440,16 @@ class GameLauncher:
         if self.process and self.process.poll() is None:
             self._kill_children()
 
-    def cleanup(self) -> None:
-        """Kill children, save playtime, call on_process_exit."""
+    def cleanup(self, exit_code: int = 0) -> None:
+        """Kill children, save playtime, and dispatch the appropriate callback.
+
+        Parameters
+        ----------
+        exit_code : int
+            The process exit status.  Zero means success; non-zero
+            triggers the failure callback instead of
+            ``on_process_exit``.
+        """
         # Stop the watchdog thread
         self._cleanup_event.set()
 
@@ -461,13 +472,22 @@ class GameLauncher:
             except Exception:
                 pass
 
-        if self._on_process_exit:
-            self._on_process_exit()
+        if exit_code == 0:
+            if self._on_process_exit:
+                self._on_process_exit()
+        else:
+            log.warning("Process exited with code %d", exit_code)
+            if self._on_process_fail:
+                self._on_process_fail(exit_code, self.error_output)
 
     # ── Watchdog ──────────────────────────────────────────────────────
 
     def _watchdog_loop(self) -> None:
-        """Background thread: kill orphaned bwrap processes."""
+        """Background thread: kill orphaned bwrap processes.
+
+        Respects ``_is_cleanup`` to avoid interfering with the
+        child-watch cleanup path.
+        """
         while not self._cleanup_event.is_set():
             self._cleanup_event.wait(timeout=5.0)
             if self._cleanup_event.is_set():
@@ -490,7 +510,9 @@ class GameLauncher:
 
     def _kill_children(self) -> None:
         """Kill the bwrap process and all its descendants (SIGTERM → SIGKILL)."""
-        if self.process and self.process.poll() is None:
+        if not self.process:
+            return
+        if self.process.poll() is None:
             try:
                 parent = psutil.Process(self.process.pid)
                 children = parent.children(recursive=True)
diff --git a/fungus/run_manager.py b/fungus/run_manager.py
index b53f1b8..61eeada 100644
--- a/fungus/run_manager.py
+++ b/fungus/run_manager.py
@@ -7,6 +7,7 @@ All Gtk/Glib/D-Bus coupling lives here.  For pure business logic, see
 
 from __future__ import annotations
 
+import os
 import sys
 from threading import Thread
 
@@ -121,6 +122,28 @@ def handle_command(
             GLib.idle_add(s._destroy)
         GLib.idle_add(Gtk.main_quit)
 
+    def on_process_fail(exit_code: int, error_output: list[str]) -> None:
+        """Handle a non-zero process exit."""
+        log.debug("on_process_fail: exit_code=%d", exit_code)
+        s = splash_container[0]
+        if s is not None:
+            GLib.idle_add(s._destroy)
+        if error_output:
+            summary = "\n".join(error_output[-20:])
+        else:
+            summary = "No error output captured."
+
+        # Use a lambda because GLib.idle_add doesn't support kwargs.
+        GLib.idle_add(
+            lambda: create_error_dialog(
+                title="Fungus Launcher",
+                primary=f"The game exited with code {exit_code}.",
+                secondary="Check the terminal output for details.",
+                error_text=summary,
+                quit_app=True,
+            )
+        )
+
     def on_sandbox_warning(exec_path: str, valid_dirs: list[str]) -> None:
         GLib.idle_add(SandboxWarningDialog, exec_path, valid_dirs)
 
@@ -162,6 +185,7 @@ def handle_command(
         on_output_line=on_output_line,
         on_error=on_error,
         on_process_exit=on_process_exit,
+        on_process_fail=on_process_fail,
         on_sandbox_warning=on_sandbox_warning,
         on_steam_runtime_missing=on_steam_runtime_missing,
         on_proton_missing=on_proton_missing,
@@ -202,14 +226,22 @@ def handle_command(
         def _on_child_exit(pid: int, condition: int) -> bool:
             """GLib child watch callback — fires when the bwrap process exits.
 
-            Calls launcher.cleanup() to stop the watchdog, save playtime,
-            close the splash window, and quit GTK.
+            Extracts the exit code from *condition* and dispatches
+            cleanup appropriately: success (0) → ``on_process_exit``,
+            failure (non-zero) → ``on_process_fail``.
             """
+            if os.WIFEXITED(condition):
+                exit_code = os.WEXITSTATUS(condition)
+            elif os.WIFSIGNALED(condition):
+                exit_code = -os.WTERMSIG(condition)
+            else:
+                exit_code = condition
+
             log.debug(
-                "_on_child_exit: pid=%d condition=%d — calling launcher.cleanup()",
-                pid, condition,
+                "_on_child_exit: pid=%d condition=%d → exit_code=%d",
+                pid, condition, exit_code,
             )
-            launcher.cleanup()
+            launcher.cleanup(exit_code)
             return False
 
         GLib.child_watch_add(