From 2de19bd9880f3d844eff7134cea63acb3d87358a Mon Sep 17 00:00:00 2001 From: Brian Krabach Date: Wed, 1 Apr 2026 13:00:00 -0700 Subject: [PATCH] fix: clean exit on Ctrl+C in muxplex service logs subprocess.run with check=True raised CalledProcessError then KeyboardInterrupt on Ctrl+C, printing an ugly traceback. Fix: remove check=True, catch KeyboardInterrupt, exit silently. --- muxplex/service.py | 10 ++++++++-- muxplex/tests/test_service.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/muxplex/service.py b/muxplex/service.py index bbad5f3..fadc0ff 100644 --- a/muxplex/service.py +++ b/muxplex/service.py @@ -152,7 +152,10 @@ def _systemd_status() -> None: def _systemd_logs() -> None: - subprocess.run(["journalctl", "--user", "-u", "muxplex", "-f"], check=True) + try: + subprocess.run(["journalctl", "--user", "-u", "muxplex", "-f"]) + except KeyboardInterrupt: + pass # --------------------------------------------------------------------------- @@ -205,7 +208,10 @@ def _launchd_status() -> None: def _launchd_logs() -> None: - subprocess.run(["tail", "-f", "/tmp/muxplex.log"], check=True) + try: + subprocess.run(["tail", "-f", "/tmp/muxplex.log"]) + except KeyboardInterrupt: + pass # --------------------------------------------------------------------------- diff --git a/muxplex/tests/test_service.py b/muxplex/tests/test_service.py index d1c23bc..b835416 100644 --- a/muxplex/tests/test_service.py +++ b/muxplex/tests/test_service.py @@ -522,3 +522,32 @@ def test_prompt_host_missing_host_key_no_keyerror(monkeypatch): # Must not raise KeyError svc._prompt_host_if_localhost() + + +# --------------------------------------------------------------------------- +# Bug fix: Ctrl+C handling in logs functions (clean exit on KeyboardInterrupt) +# --------------------------------------------------------------------------- + + +def test_systemd_logs_handles_keyboard_interrupt(monkeypatch): + """service logs must exit cleanly on Ctrl+C.""" + import muxplex.service as svc + + def mock_run(*args, **kwargs): + raise KeyboardInterrupt() + + monkeypatch.setattr(subprocess, "run", mock_run) + # Should not raise + svc._systemd_logs() + + +def test_launchd_logs_handles_keyboard_interrupt(monkeypatch): + """service logs must exit cleanly on Ctrl+C on macOS.""" + import muxplex.service as svc + + def mock_run(*args, **kwargs): + raise KeyboardInterrupt() + + monkeypatch.setattr(subprocess, "run", mock_run) + # Should not raise + svc._launchd_logs()