Skip to content

The agent that grows with you

ci(process): the detached-spawn ratchet also catches process_group= and kwargs["start_new_session"] = ...

kshitijk4poorc9460ed4959c
diff --git a/agent/shell_hooks.py b/agent/shell_hooks.py
index f3ac817de7a..967d53e7370 100644
--- a/agent/shell_hooks.py
+++ b/agent/shell_hooks.py
@@ -493,6 +493,7 @@ def _spawn(spec: ShellHookSpec, stdin_json: str) -> dict[str, Any]:
t0 = time.monotonic()
# Own process group on POSIX so a timed-out hook's descendants are reaped with it (Windows: kill_process_tree
# / taskkill /T). Hooks that finish in time keep detached helpers alive.
+ # detached-spawn: ok — awaited hook; its timeout kills the whole group (kill_process_tree)
popen_kwargs: dict[str, Any] = {"creationflags": windows_hide_flags()} if IS_WINDOWS else {"process_group": 0}
# HERMES_HOME follows the routed profile (the import-time environ holds the launch profile's), and
# under multiplexing os.environ carries the DEFAULT profile's secrets, which a secondary's hook
diff --git a/hermes_cli/_subprocess_compat.py b/hermes_cli/_subprocess_compat.py
index f92821494f2..a2f985af619 100644
--- a/hermes_cli/_subprocess_compat.py
+++ b/hermes_cli/_subprocess_compat.py
@@ -843,6 +843,7 @@ def bounded_probe_run(
``Get-CimInstance Win32_Process`` gateway scan hit exactly this during ``hermes update`` on slow-WMI
machines (#87134); the git probes hit it first (#68609 / #66037).
"""
+ # detached-spawn: ok — awaited probe; a timeout or failure tree-kills its group (kill_and_drain)
_popen_kwargs: dict = {"creationflags": windows_hide_flags()} if IS_WINDOWS else {"process_group": 0}
job = None
try:
diff --git a/hermes_cli/update_abort_recovery.py b/hermes_cli/update_abort_recovery.py
index 44d300c9b2d..231a2266cd0 100644
--- a/hermes_cli/update_abort_recovery.py
+++ b/hermes_cli/update_abort_recovery.py
@@ -128,6 +128,7 @@ def _run_fresh_recovery_process(
kwargs["creationflags"] = (
getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) | getattr(subprocess, "DETACHED_PROCESS", 0))
else:
+ # detached-spawn: ok — awaited with a timeout; the gateway restart it runs must not share the updater's group
kwargs["start_new_session"] = True
try:
return subprocess.run(command, **kwargs, check=False)
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 32a257193c0..cd5d2a84e7e 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -586,6 +586,7 @@ def _run_logged_subprocess(cmd, *, cwd=None, env=None):
child_env = dict(os.environ if env is None else env)
child_env.setdefault("PYTHONUNBUFFERED", "1")
+ # detached-spawn: ok — awaited update step; a cancel tree-kills its group (kill_process_tree)
spawn = {"creationflags": windows_hide_flags()} if os.name == "nt" else {"process_group": 0}
proc = subprocess.Popen(
cmd, cwd=cwd, env=child_env, stdin=subprocess.DEVNULL,
diff --git a/hermes_cli/update_restart_recovery.py b/hermes_cli/update_restart_recovery.py
index 533a383aace..c91badef559 100644
--- a/hermes_cli/update_restart_recovery.py
+++ b/hermes_cli/update_restart_recovery.py
@@ -136,6 +136,7 @@ def _run_profile_restart(profile: str, *, run: Callable[..., Any]) -> bool:
if os.name == "nt":
kwargs["creationflags"] = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) | getattr(subprocess, "DETACHED_PROCESS", 0)
else:
+ # detached-spawn: ok — awaited with a timeout; the gateway restart it runs must not share the updater's group
kwargs["start_new_session"] = True
# `--interrupt`: the update owns this restart (drain-first, 31637312899); a live session must not
# strand the profile on pre-update code the way a refusal would. `--detach`: this bounded call
diff --git a/scripts/check_detached_spawns.py b/scripts/check_detached_spawns.py
index 0664efd2df8..fe5090d1e34 100644
--- a/scripts/check_detached_spawns.py
+++ b/scripts/check_detached_spawns.py
@@ -12,7 +12,9 @@ per-process owner-death supervisor.
Flags, in Python files of the production trees (tests, evals, scripts and nix checks excluded):
start_new_session=True / start_new_session=<anything but False>, or a ``"start_new_session"``
- dict key with a non-False value
+ dict key / ``kwargs["start_new_session"] = ...`` item with a non-False value
+ process_group=<anything but None> (``process_group=0`` puts the child in a new group), in the
+ same three shapes
any reference to os.setsid / os.setpgid / os.setpgrp (call or ``preexec_fn=os.setsid``)
a call to ``windows_detach_popen_kwargs()`` (``start_new_session=True`` on POSIX)
@@ -48,13 +50,21 @@ _DETACH_OS_ATTRS = {"setsid", "setpgid", "setpgrp"}
# Helpers that return detaching Popen kwargs (``start_new_session=True`` on POSIX).
_DETACH_HELPERS = {"windows_detach_popen_kwargs"}
# Cheap text prefilter: only files mentioning one of these are parsed.
-_NEEDLES = ("start_new_session", "setsid", "setpgid", "setpgrp", *_DETACH_HELPERS)
+_NEEDLES = ("start_new_session", "process_group", "setsid", "setpgid", "setpgrp", *_DETACH_HELPERS)
def _is_false(node: ast.AST) -> bool:
return isinstance(node, ast.Constant) and node.value in (False, None, 0)
+def _detaches(name: object, value: ast.AST) -> bool:
+ """Whether Popen keyword ``name`` set to ``value`` detaches the child into its own group."""
+ if name == "start_new_session":
+ return not _is_false(value)
+ # process_group=0 is a new group; only the default (None) leaves the child in ours.
+ return name == "process_group" and not (isinstance(value, ast.Constant) and value.value is None)
+
+
def _is_os_detach(node: ast.AST) -> bool:
return (isinstance(node, ast.Attribute) and node.attr in _DETACH_OS_ATTRS
and isinstance(node.value, ast.Name) and node.value.id == "os")
@@ -75,12 +85,17 @@ def detach_lines(text: str) -> list[int]:
return []
lines: set[int] = set()
for node in ast.walk(tree):
- if isinstance(node, ast.keyword) and node.arg == "start_new_session" and not _is_false(node.value):
+ if isinstance(node, ast.keyword) and _detaches(node.arg, node.value):
lines.add(node.value.lineno)
elif isinstance(node, ast.Dict):
for key, value in zip(node.keys, node.values):
- if isinstance(key, ast.Constant) and key.value == "start_new_session" and not _is_false(value):
+ if isinstance(key, ast.Constant) and _detaches(key.value, value):
lines.add(key.lineno)
+ elif isinstance(node, ast.Assign): # kwargs["start_new_session"] = True
+ for target in node.targets:
+ if (isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Constant)
+ and _detaches(target.slice.value, node.value)):
+ lines.add(target.lineno)
elif _is_os_detach(node) or _is_detach_helper_call(node):
lines.add(node.lineno)
return sorted(lines)
diff --git a/tests/scripts/test_check_detached_spawns.py b/tests/scripts/test_check_detached_spawns.py
index 275b3bab421..0b44aea359a 100644
--- a/tests/scripts/test_check_detached_spawns.py
+++ b/tests/scripts/test_check_detached_spawns.py
@@ -22,6 +22,10 @@ def _load():
"subprocess.Popen(argv, start_new_session=True)",
"subprocess.Popen(argv, start_new_session=flag)",
'kwargs = {"start_new_session": True}',
+ 'kwargs["start_new_session"] = True',
+ "subprocess.Popen(argv, process_group=0)",
+ 'kwargs = {"process_group": 0}',
+ 'kwargs["process_group"] = 0',
"subprocess.Popen(argv, preexec_fn=os.setsid)",
"subprocess.Popen(argv, preexec_fn=lambda: os.setpgid(0, 0))",
"os.setpgrp()",
@@ -39,6 +43,8 @@ def test_detached_shapes_are_flagged(src):
"subprocess.Popen(argv)",
"subprocess.Popen(argv, start_new_session=False)",
'kwargs = {"start_new_session": False}',
+ 'kwargs["start_new_session"] = False',
+ "subprocess.Popen(argv, process_group=None)",
"spawn_owned(argv, purpose='x')",
"# start_new_session=True in a comment",
'doc = "os.setsid in a string"',