From a7948975ce8c92d9300bc5301f1c5df9891f1f0e Mon Sep 17 00:00:00 2001 From: Juan Diaz Date: Fri, 17 Jul 2026 09:44:23 +0200 Subject: [PATCH] Fix: Avoid traceback when ctr+c is used to finish the tunnel --- src/sbx/cli.py | 4 ++++ tests/test_cli.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/sbx/cli.py b/src/sbx/cli.py index 1409117..d881a17 100644 --- a/src/sbx/cli.py +++ b/src/sbx/cli.py @@ -5,6 +5,7 @@ import re import shlex import shutil +import signal import sqlite3 import subprocess import sys @@ -2214,6 +2215,9 @@ def main(argv: Sequence[str] | None = None) -> int: return 2 try: return args.func(args) + except KeyboardInterrupt: + # Shell convention: signal exits are 128 + signal number. + return 128 + signal.SIGINT except ConfigError as exc: print(f"sbx: {exc}", file=sys.stderr) return 2 diff --git a/tests/test_cli.py b/tests/test_cli.py index f29a511..9296588 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1431,6 +1431,15 @@ def test_network_forward_accepts_explicit_name(monkeypatch: pytest.MonkeyPatch) } +def test_network_forward_ctrl_c_returns_130(monkeypatch: pytest.MonkeyPatch) -> None: + def interrupted(vm_id: str, forward: tuple[str, int, int]) -> int: + raise KeyboardInterrupt + + monkeypatch.setattr(cli.network, "_foreground_port_forward", interrupted) + + assert cli.main(["network", "forward", "vm2", "3005"]) == 130 + + def test_network_commands_default_to_configured_name( monkeypatch: pytest.MonkeyPatch, tmp_path: Path,