Skip to content

The agent that grows with you

fix(managed-worker): owner close() signals the worker's process group

Teknium9136cc42a425
diff --git a/gateway/session_managed_worker.py b/gateway/session_managed_worker.py
index e34a837f257..0769eb6f52c 100644
--- a/gateway/session_managed_worker.py
+++ b/gateway/session_managed_worker.py
@@ -294,17 +294,31 @@ class ManagedWorker:
except psutil.Error:
pass
+ def _signal_group(self, kill):
+ """POSIX: signal the worker's whole process group (it leads one, WORKER_POPEN), so the tool
+ children a killed worker never reaps with _die_with_owner go too. Only while the leader is
+ unreaped and still leads its group; otherwise (Windows, gone) the handle alone."""
+ if os.name != 'nt' and self.process.poll() is None:
+ import signal
+ try:
+ if os.getpgid(self.process.pid) == self.process.pid:
+ os.killpg(self.process.pid, signal.SIGKILL if kill else signal.SIGTERM) # windows-footgun: ok — POSIX only
+ return
+ except (ProcessLookupError, PermissionError):
+ pass
+ (self.process.kill if kill else self.process.terminate)()
+
def close(self):
self.closed.set()
self._demand.release() # a reader parked between frames exits; one mid-read ends at EOF
if self.process.poll() is None:
self._signal_worker(kill=False)
- self.process.terminate()
+ self._signal_group(kill=False)
try:
self.process.wait(timeout=5)
except subprocess.TimeoutExpired:
self._signal_worker(kill=True)
- self.process.kill()
+ self._signal_group(kill=True)
self.process.wait(timeout=5)
else:
self._signal_worker(kill=True)
diff --git a/tests/gateway/test_managed_worker_group_close.py b/tests/gateway/test_managed_worker_group_close.py
new file mode 100644
index 00000000000..b7a04dce687
--- /dev/null
+++ b/tests/gateway/test_managed_worker_group_close.py
@@ -0,0 +1,52 @@
+"""Owner-initiated managed-worker teardown reaps the worker's process group (N55).
+
+The worker leads its own process group (WORKER_POPEN start_new_session). When the owner ends it
+(Stop not acknowledged, lost/cancelled worker, gateway shutdown), close() signalled the worker PID
+only; the worker died before it could see stdin EOF, so _die_with_owner's group kill never ran and
+its tool children were orphaned outside every group the gateway's supervisors reach.
+Real processes and the real close().
+"""
+import os
+import subprocess
+import sys
+import time
+
+import psutil
+import pytest
+
+from gateway.session_managed_worker import WORKER_POPEN, ManagedWorker
+
+_WORKER = (
+ "import subprocess, sys, time\n"
+ "child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(120)'])\n"
+ "print(child.pid, flush=True)\n"
+ "time.sleep(120)\n")
+
+
+def _gone(pid):
+ try:
+ return psutil.Process(pid).status() == psutil.STATUS_ZOMBIE
+ except psutil.NoSuchProcess:
+ return True
+
+
[email protected]("linux", "macos")
[email protected]_system_guard_bypass # cleanup may kill our own grandchild after init adopted it (pre-fix)
+def test_close_reaps_the_workers_tool_children():
+ process = subprocess.Popen([sys.executable, '-c', _WORKER], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL, **WORKER_POPEN)
+ grandchild = int(process.stdout.readline())
+ try:
+ assert os.getpgid(grandchild) == process.pid
+ ManagedWorker(process).close()
+ assert process.poll() is not None
+ deadline = time.monotonic() + 5
+ while not _gone(grandchild) and time.monotonic() < deadline:
+ time.sleep(.05)
+ assert _gone(grandchild), "the worker's tool child outlived the owner's close()"
+ finally:
+ if not _gone(grandchild): # an orphan (pre-fix) is reparented out of the test's subtree
+ os.kill(grandchild, 9)
+ if process.poll() is None:
+ process.kill()
+ process.wait(timeout=5)