Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

test: release terminal endpoints before bounded child cleanup

Rach Pradhan817727365dca
diff --git a/scripts/eval-tier1.sh b/scripts/eval-tier1.sh
index ea5cdb42..fe79146e 100755
--- a/scripts/eval-tier1.sh
+++ b/scripts/eval-tier1.sh
@@ -252,7 +252,7 @@ if wanted tuiguard; then
# Original 17 plus the ESC-split and line-REPL prompt-reflow regressions.
# Each owns its pty/tmp/mock; the pool is the wall-time win. Deadlines
# (#704) are checked first so a wedged probe cannot hang pre-push.
- if python3 scripts/eval/test_tier1_tuiguard.py && python3 scripts/eval/tier1_tuiguard.py zig-out/bin/graff; then :; else
+ if python3 scripts/eval/test_pty_cleanup.py && python3 scripts/eval/test_tier1_tuiguard.py && python3 scripts/eval/tier1_tuiguard.py zig-out/bin/graff; then :; else
record_fail tuiguard
fi
fi
diff --git a/scripts/eval/test_pty_cleanup.py b/scripts/eval/test_pty_cleanup.py
new file mode 100644
index 00000000..d1495042
--- /dev/null
+++ b/scripts/eval/test_pty_cleanup.py
@@ -0,0 +1,49 @@
+import os
+from pathlib import Path
+import sys
+import unittest
+from unittest.mock import patch
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from pty_cleanup import close_pty
+from ptyharness import PtyHarness
+
+
+class CleanupTests(unittest.TestCase):
+ def test_releases_both_endpoints_before_reaping(self):
+ closed = []
+ harness = PtyHarness.__new__(PtyHarness)
+ harness._closed = False
+ harness.exit_status = None
+ harness.pid, harness.master, harness.slave = 42, 10, 11
+ polls = 0
+
+ def wait(pid, flags):
+ nonlocal polls
+ polls += 1
+ if polls == 1:
+ return 0, 0 # Initial liveness check, before teardown.
+ self.assertEqual(closed, [10, 11])
+ self.assertEqual(flags, os.WNOHANG)
+ return pid, 9
+
+ with patch('pty_cleanup.os.kill'), patch('pty_cleanup.os.close', side_effect=closed.append), patch('pty_cleanup.os.waitpid', side_effect=wait):
+ harness.close()
+ self.assertEqual(harness.exit_status, 9)
+ self.assertTrue(harness._closed)
+
+ def test_wedged_child_fails_within_deadline(self):
+ with patch('pty_cleanup.os.kill'), patch('pty_cleanup.os.close'), patch('pty_cleanup.os.waitpid', return_value=(0, 0)), patch('pty_cleanup.time.monotonic', side_effect=[0, 1, 4]), patch('pty_cleanup.time.sleep'):
+ with self.assertRaisesRegex(TimeoutError, 'PTY child'):
+ close_pty(42, (10, 11), timeout=3)
+
+ def test_reaped_child_only_closes_descriptors(self):
+ with patch('pty_cleanup.os.kill') as kill, patch('pty_cleanup.os.close') as close, patch('pty_cleanup.os.waitpid') as wait:
+ close_pty(None, (10, 11))
+ self.assertEqual(close.call_count, 2)
+ kill.assert_not_called()
+ wait.assert_not_called()
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/scripts/pty_cleanup.py b/scripts/pty_cleanup.py
new file mode 100644
index 00000000..1ec4c982
--- /dev/null
+++ b/scripts/pty_cleanup.py
@@ -0,0 +1,32 @@
+"""Release PTY endpoints before waiting for a killed child to exit."""
+import os
+import signal
+import time
+
+
+def close_pty(pid, descriptors, timeout=3.0):
+ if pid is not None:
+ try:
+ os.kill(pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+ # A killed process can still be in terminal teardown. Keeping the parent's
+ # endpoints open while blocking in waitpid can prevent teardown completing.
+ for fd in descriptors:
+ try:
+ os.close(fd)
+ except OSError:
+ pass
+ if pid is None:
+ return None
+ deadline = time.monotonic() + timeout
+ while True:
+ try:
+ done, status = os.waitpid(pid, os.WNOHANG)
+ except ChildProcessError:
+ return None
+ if done:
+ return status
+ if time.monotonic() >= deadline:
+ raise TimeoutError('PTY child did not exit after cleanup')
+ time.sleep(0.01)
diff --git a/scripts/ptyharness.py b/scripts/ptyharness.py
index 1db9a86d..00054cb2 100644
--- a/scripts/ptyharness.py
+++ b/scripts/ptyharness.py
@@ -48,6 +48,7 @@ import sys
import termios
import time
import unicodedata
+from pty_cleanup import close_pty
# DEC private modes whose TERMINAL DEFAULT is enabled: autowrap and cursor
# visible. For those the restore direction is `h`, so "balanced" means "final
@@ -702,11 +703,7 @@ class PtyHarness:
if self.poll() is not None:
break
if self.poll() is None:
- os.kill(self.pid, signal.SIGKILL)
- try:
- os.waitpid(self.pid, 0)
- except ChildProcessError:
- pass
+ self.close()
self.exit_status = None
return None
self.pump(0.3) # the restore tail can land after the exit is reaped
@@ -722,29 +719,18 @@ class PtyHarness:
while time.time() < end and self.poll() is None:
self.pump(0.1)
if self.poll() is None:
- os.kill(self.pid, signal.SIGKILL)
- try:
- os.waitpid(self.pid, 0)
- except ChildProcessError:
- pass
+ self.close()
def close(self) -> None:
if self._closed:
return
- if self.poll() is None:
- try:
- os.kill(self.pid, signal.SIGKILL)
- os.waitpid(self.pid, 0)
- except OSError:
- pass
- except ChildProcessError:
- pass
- for fd in (self.master, self.slave):
- try:
- os.close(fd)
- except OSError:
- pass
- self._closed = True
+ pending = self.pid if self.poll() is None else None
+ try:
+ status = close_pty(pending, (self.master, self.slave))
+ if status is not None:
+ self.exit_status = status
+ finally:
+ self._closed = True
def __enter__(self):
return self
diff --git a/scripts/test-tui-chrome-width.py b/scripts/test-tui-chrome-width.py
index 186264f4..11debbb9 100755
--- a/scripts/test-tui-chrome-width.py
+++ b/scripts/test-tui-chrome-width.py
@@ -25,6 +25,7 @@ import sys
import tempfile
import time
import unicodedata
+from pty_cleanup import close_pty
# Absolutized BEFORE the fork: the child chdirs into its scratch workspace.
BIN = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else "zig-out/bin/graff")
@@ -93,7 +94,6 @@ def fresh_ws():
def reap(pid, fd):
- import signal
try:
os.write(fd, b"\x1b") # cancel the background op first
drain(fd, 0.5)
@@ -101,15 +101,7 @@ def reap(pid, fd):
drain(fd, 2.0)
except OSError:
pass
- for sig in (signal.SIGTERM, signal.SIGKILL):
- try:
- os.kill(pid, sig)
- except OSError:
- pass
- try:
- os.waitpid(pid, 0)
- except OSError:
- pass
+ close_pty(pid, (fd,))
def plain(b):