Repository navigation
fix(browser): reap orphaned Chrome processes after daemon exit on Windows #43577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1290,6 +1290,94 @@ def _write_owner_pid(socket_dir: str, session_name: str) -> None: | |
| session_name, exc) | ||
|
|
||
|
|
||
| def _reap_orphaned_chrome_processes() -> int: | ||
| """Kill orphaned headless Chrome processes from dead agent-browser daemons. | ||
|
|
||
| On Windows, when an agent-browser daemon exits (gateway restart, crash), | ||
| its Chromium child processes get reparented to Explorer.exe. The main | ||
| reaper ``_reap_orphaned_browser_sessions`` handles daemons that are | ||
| *still alive*, but cannot reach Chrome children whose daemon already | ||
| exited. This function catches those orphans by scanning for Chrome | ||
| processes whose ``--user-data-dir`` points at a stale | ||
| ``agent-browser-chrome-*`` temp directory. | ||
|
|
||
| Safe to call from the cleanup thread or on startup. | ||
| """ | ||
| if os.name != "nt": | ||
| # On POSIX, process-tree SIGTERM cascades reliably; this is a | ||
| # Windows-specific reparenting problem. | ||
| return 0 | ||
|
|
||
| try: | ||
| import subprocess, tempfile, re | ||
| from gateway.status import _pid_exists | ||
|
|
||
| # Only reap when there are NO active browser sessions in this | ||
| # process. If sessions exist, Chrome processes likely belong to | ||
| # them and should not be touched. | ||
| with _cleanup_lock: | ||
| if _active_sessions: | ||
| return 0 | ||
|
|
||
| tmpdir = tempfile.gettempdir() | ||
| result = subprocess.run( | ||
| [ | ||
| "wmic", "process", | ||
| "where", "name='chrome.exe'", | ||
| "get", "processid,parentprocessid,commandline", | ||
| "/format:csv", | ||
| ], | ||
| capture_output=True, timeout=15, | ||
| creationflags=0x08000000, # CREATE_NO_WINDOW | ||
| ) | ||
| stdout = result.stdout.decode("utf-8", errors="replace") | ||
|
|
||
| # Collect PIDs of main (non-child-type) Chrome processes that use | ||
| # an agent-browser-chrome user-data-dir. | ||
| udd_re = re.compile( | ||
| r'--user-data-dir="?([^"]*agent-browser-chrome-[^" ]+)') | ||
| main_pids: list[int] = [] | ||
| for line in stdout.splitlines(): | ||
| line = line.strip() | ||
| if not line or line.startswith("Node"): | ||
| continue | ||
| parts = line.split(",", 3) | ||
| if len(parts) < 4: | ||
| continue | ||
| try: | ||
| pid = int(parts[3]) | ||
| cmdline = parts[1] | ||
| except (ValueError, IndexError): | ||
| continue | ||
| m = udd_re.search(cmdline) | ||
| if not m: | ||
| continue | ||
| # Only match main browser processes (have --headless, no --type) | ||
| if "--headless" in cmdline and "--type=" not in cmdline: | ||
| main_pids.append(pid) | ||
|
|
||
| if not main_pids: | ||
| return 0 | ||
|
|
||
| reaped = 0 | ||
| from tools.process_registry import ProcessRegistry | ||
| for pid in main_pids: | ||
| try: | ||
| ProcessRegistry._terminate_host_pid(pid) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in c78a324. Selection now uses the returned ParentProcessId and fails closed: a matching Chrome process is only reaped when its recorded parent is confirmed dead. Added a cross-platform test proving a live parent from a concurrent Hermes session is skipped. |
||
| reaped += 1 | ||
| except (ProcessLookupError, PermissionError, OSError): | ||
| pass | ||
|
|
||
| if reaped: | ||
| logger.info( | ||
| "Reaped %d orphaned Chrome process tree(s) whose daemon " | ||
| "already exited (Windows reparenting workaround)", reaped) | ||
| return reaped | ||
| except Exception as exc: | ||
| logger.debug("Orphan Chrome reap skipped: %s", exc) | ||
| return 0 | ||
|
|
||
|
|
||
| def _reap_orphaned_browser_sessions(): | ||
| """Scan for orphaned agent-browser daemon processes from previous runs. | ||
|
|
||
|
|
@@ -1418,6 +1506,13 @@ def _browser_cleanup_thread_worker(): | |
| except Exception as e: | ||
| logger.warning("Orphan reap error: %s", e) | ||
|
|
||
| # Second pass: kill orphaned Chrome processes whose daemon already | ||
| # exited (Windows reparenting workaround — see _reap_orphaned_chrome_processes). | ||
| try: | ||
| _reap_orphaned_chrome_processes() | ||
| except Exception as e: | ||
| logger.warning("Orphan Chrome reap error: %s", e) | ||
|
|
||
| while _cleanup_running: | ||
| try: | ||
| _cleanup_inactive_browser_sessions() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is WMIC CSV output, but
CommandLineis a quoted free-form field and may contain commas. Parse withcsv.reader; otherwise a comma shiftsparts[3]and silently prevents correct PID selection.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c78a324. WMIC output is now parsed with csv.reader in a pure selection helper, with a cross-platform test covering a quoted CommandLine field containing a comma.