Skip to content

The agent that grows with you

fix(browser): atexit hooks stay silent when the origin module is unimportable

Davyee62a4b5f68d
diff --git a/tests/tools/test_browser_cleanup.py b/tests/tools/test_browser_cleanup.py
index ca22555d253..5330d2d3160 100644
--- a/tests/tools/test_browser_cleanup.py
+++ b/tests/tools/test_browser_cleanup.py
@@ -203,3 +203,18 @@ class TestAtexitStopSwallowsInterrupt:
monkeypatch.setattr(browser_tool, "_cleanup_running", True)
bt_lifecycle._stop_browser_cleanup_thread() # must not raise
assert browser_tool._cleanup_running is False
+
+
+class TestAtexitOriginUnimportable:
+ def test_hooks_stay_silent_when_origin_fresh_import_fails(self, monkeypatch):
+ """Mid-`hermes update` the on-disk tree can be half-new (new config.py importing
+ a name the old utils.py lacks yet); the fresh import in origin_module then raises
+ and the atexit hooks must stay silent (#112437)."""
+ import tools.browser_tool_origin as origin_mod
+
+ def _boom(_depth=2):
+ raise ImportError("cannot import name 'file_signature' from 'utils'")
+
+ monkeypatch.setattr(origin_mod, "origin_module", _boom)
+ bt_lifecycle._emergency_cleanup_all_sessions() # must not raise
+ bt_lifecycle._stop_browser_cleanup_thread() # must not raise
diff --git a/tools/browser_tool_lifecycle.py b/tools/browser_tool_lifecycle.py
index 6eef8d1a206..b8b2793bed4 100644
--- a/tools/browser_tool_lifecycle.py
+++ b/tools/browser_tool_lifecycle.py
@@ -72,9 +72,14 @@ def _emergency_cleanup_all_sessions():
"""atexit: close this process's sessions, then sweep orphans left by crashed
hermes processes — every clean exit reaps accumulated orphans, not only
processes that used the browser tool."""
- if _bt._cleanup_done:
+ try:
+ if _bt._cleanup_done:
+ return
+ _bt._cleanup_done = True
+ except Exception:
+ # Interpreter shutdown (or a half-updated tree mid-`hermes update` where the
+ # origin's fresh import fails, e.g. #112437): no resolvable state, nothing to clean.
return
- _bt._cleanup_done = True
# Own sessions first so their owner_pid files are gone before the reaper scans.
# Real-profile Chrome is launched directly (not by agent-browser), so the
@@ -422,12 +427,18 @@ def _start_browser_cleanup_thread():
def _stop_browser_cleanup_thread():
"""Stop the background cleanup thread."""
- _bt._cleanup_running = False
- if _bt._cleanup_thread is not None:
+ try:
+ _bt._cleanup_running = False
+ thread = _bt._cleanup_thread
+ except Exception:
+ # Same unimportable-origin case as _emergency_cleanup_all_sessions (#112437):
+ # no resolvable thread state, nothing to stop.
+ return
+ if thread is not None:
# 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)
+ thread.join(timeout=5)
except (SystemExit, KeyboardInterrupt):
pass