Skip to content

The agent that grows with you

fix(process_registry): never close a PTY under its live reader thread

unsupportedpastels6ec30666d2fb
diff --git a/tests/tools/test_process_registry_pty_kill.py b/tests/tools/test_process_registry_pty_kill.py
new file mode 100644
index 00000000000..6bf756baa9b
--- /dev/null
+++ b/tests/tools/test_process_registry_pty_kill.py
@@ -0,0 +1,62 @@
+"""Killing a PTY process must return even when a descendant escaped into its own session.
+
+The escapee keeps the PTY slave open, so the reader thread stays blocked in ``read()`` holding
+the PTY file object's buffer lock. Closing the PTY from the kill path waited on that lock until
+the escapee exited (forever, for a long-lived one), so the kill never returned. Real PTY, real
+processes: a fake PTY cannot hold the lock.
+"""
+
+import shutil
+import sys
+import threading
+import time
+
+import pytest
+
+import tools.process_registry as module
+from tools.process_registry import ProcessRegistry
+
+pytest.importorskip("ptyprocess")
+pytestmark = pytest.mark.skipif(
+ sys.platform == "win32" or shutil.which("setsid") is None, reason="POSIX PTY + setsid")
+
+# The escapee exits on its own (it is reparented to init, outside what a test may signal).
+# Before the fix the kill could only return once it had, so the kill deadline is shorter.
+_ESCAPEE_LIFETIME_S = 8
+_KILL_DEADLINE_S = 5
+
+
+def _wait_for(predicate, timeout):
+ deadline = time.monotonic() + timeout
+ while time.monotonic() < deadline:
+ if predicate():
+ return True
+ time.sleep(0.05)
+ return predicate()
+
+
+def test_kill_returns_while_escaped_descendant_holds_the_pty(tmp_path, monkeypatch):
+ # No systemd scope: stopping one would reap the escapee and hide the hang.
+ monkeypatch.setattr(module, "_is_supervised_gateway_process", lambda: False)
+ pidfile = tmp_path / "escapee.pid"
+ registry = ProcessRegistry()
+ session = registry.spawn_local(
+ f"setsid sh -c 'echo $$ > {pidfile}; exec sleep {_ESCAPEE_LIFETIME_S}' & sleep 120",
+ cwd=str(tmp_path), use_pty=True)
+ assert _wait_for(lambda: pidfile.exists() and pidfile.read_text().strip(), 5)
+
+ result = {}
+ killer = threading.Thread(
+ target=lambda: result.update(registry.kill_process(session.id)), daemon=True)
+ killer.start()
+ killer.join(_KILL_DEADLINE_S)
+ assert not killer.is_alive(), "kill_process blocked closing the PTY under a live reader"
+ assert result["status"] == "killed"
+ assert session.id in registry._finished
+
+ # Once the escapee exits, the reader's read ends and it closes the PTY itself, so the
+ # master FD is still released.
+ reader = session._reader_thread
+ assert reader is not None
+ assert _wait_for(lambda: not reader.is_alive(), _ESCAPEE_LIFETIME_S + 5)
+ assert session._pty.closed
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 7e7780695ef..ca6848b8298 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -1794,6 +1794,15 @@ class ProcessRegistry(ProcessCheckpointMixin):
with suppress(OSError, ValueError): # a stdin flush can hit EPIPE
stream.close()
if session._pty is not None:
+ # A live PTY reader sits in a blocking read holding the PTY file
+ # object's buffer lock, and that read only ends once every holder of
+ # the slave side is gone. A descendant that setsid()s past the kill
+ # keeps it open, so close() here would block forever (under _lock on
+ # the prune path). The reader closes the PTY itself via
+ # _finish_reader once its read ends.
+ reader = session._reader_thread
+ if reader is not None and reader.is_alive() and reader is not threading.current_thread():
+ return
# ptyprocess/pywinpty close() is idempotent (``closed`` flag) and
# closes the master fd exactly once; it raises only if the child
# ignores SIGKILL, which we don't want to surface on the finish path.