Skip to content

The agent that grows with you

fix(browser): suppress KeyboardInterrupt in atexit cleanup thread stop

LehaoLinc59c1a98e30d
diff --git a/tests/tools/test_browser_cleanup.py b/tests/tools/test_browser_cleanup.py
index ddd168a77b1..599833bf3cd 100644
--- a/tests/tools/test_browser_cleanup.py
+++ b/tests/tools/test_browser_cleanup.py
@@ -187,3 +187,19 @@ class TestInactivityJanitorMultiplex:
assert "t1" not in self.bt._active_sessions
assert "t1" not in self.bt._session_last_activity
assert "t1" not in self.bt._cleanup_failures
+
+
+class TestAtexitStopSwallowsInterrupt:
+ def test_second_ctrl_c_during_join_does_not_propagate(self, monkeypatch):
+ """A second Ctrl+C while the atexit hook waits on the janitor must not escape as a
+ traceback (#10764): the thread is a daemon, the interpreter is already exiting."""
+ from tools import browser_tool
+
+ class _InterruptedJoin:
+ def join(self, timeout=None):
+ raise KeyboardInterrupt
+
+ monkeypatch.setattr(browser_tool, "_cleanup_thread", _InterruptedJoin())
+ monkeypatch.setattr(browser_tool, "_cleanup_running", True)
+ bt_lifecycle._stop_browser_cleanup_thread() # must not raise
+ assert browser_tool._cleanup_running is False
diff --git a/tools/browser_tool_lifecycle.py b/tools/browser_tool_lifecycle.py
index 1a67476422a..6eef8d1a206 100644
--- a/tools/browser_tool_lifecycle.py
+++ b/tools/browser_tool_lifecycle.py
@@ -424,7 +424,12 @@ def _stop_browser_cleanup_thread():
"""Stop the background cleanup thread."""
_bt._cleanup_running = False
if _bt._cleanup_thread is not None:
- _bt._cleanup_thread.join(timeout=5)
+ # A second Ctrl+C during the timed join lands here as KeyboardInterrupt; the janitor is a
+ # daemon thread, so letting it propagate only prints "Exception ignored in atexit callback".
+ try:
+ _bt._cleanup_thread.join(timeout=5)
+ except (SystemExit, KeyboardInterrupt):
+ pass
def _update_session_activity(task_id: str):