Skip to content

The agent that grows with you

fix(terminal): no poll() fallback for a macOS PTY

Teknium0b8dee00fafe
diff --git a/hermes_cli/pty_bridge.py b/hermes_cli/pty_bridge.py
index 26acca60331..0c3f47b7a8d 100644
--- a/hermes_cli/pty_bridge.py
+++ b/hermes_cli/pty_bridge.py
@@ -155,7 +155,11 @@ class PtyBridge:
# every OS app-switch).
spawn_env[PTY_HOST_ENV] = PTY_HOST_DASHBOARD
proc = ptyprocess.PtyProcess.spawn(list(argv), cwd=cwd, env=spawn_env, dimensions=(rows, cols)) # type: ignore[union-attr]
- return cls(proc)
+ try:
+ return cls(proc)
+ except OSError: # no safe readiness wait for the master (fd_wait): don't leak the child
+ proc.close(force=True)
+ raise
@property
def pid(self) -> int:
diff --git a/tests/tools/test_drain_high_fd.py b/tests/tools/test_drain_high_fd.py
index 91dfe9cab18..d235cf1a32f 100644
--- a/tests/tools/test_drain_high_fd.py
+++ b/tests/tools/test_drain_high_fd.py
@@ -81,10 +81,22 @@ def test_command_output_survives_pipe_fd_above_fd_setsize(high_fds, tmp_path, mo
@pytest.mark.platforms("posix")
-def test_pty_bridge_reads_and_waits_above_fd_setsize(high_fds):
- """The PTY bridge delivers output at a high fd, and an idle read blocks for its timeout."""
+def _assert_idle_read_waits(bridge):
+ started = time.monotonic()
+ assert bridge.read(timeout=0.5) == b""
+ assert time.monotonic() - started >= 0.3, "idle PTY read returned without waiting"
+
+
+def test_pty_bridge_reads_and_waits_above_fd_setsize(high_fds, monkeypatch):
+ """The PTY bridge delivers output at a high fd and an idle read blocks for its timeout;
+ when the selector cannot be created (EMFILE), the bridge either still waits (poll
+ fallback) or refuses to spawn, never a busy-looping bridge (macOS poll on a PTY)."""
+ import errno
+
pytest.importorskip("ptyprocess")
from hermes_cli.pty_bridge import PtyBridge
+ from tools.environments import fd_wait
bridge = PtyBridge.spawn(["/bin/sh", "-c", "echo pty-high-fd; exec sleep 30"])
try:
@@ -96,9 +108,19 @@ def test_pty_bridge_reads_and_waits_above_fd_setsize(high_fds):
assert chunk is not None, f"PTY reported EOF before output (got {bytes(buf)!r})"
buf.extend(chunk)
assert b"pty-high-fd" in buf
-
- started = time.monotonic()
- assert bridge.read(timeout=0.5) == b""
- assert time.monotonic() - started >= 0.3, "idle PTY read returned without waiting"
+ _assert_idle_read_waits(bridge)
finally:
bridge.close()
+
+ def _emfile():
+ raise OSError(errno.EMFILE, "Too many open files")
+
+ monkeypatch.setattr(fd_wait.selectors, "DefaultSelector", _emfile)
+ try:
+ fallback = PtyBridge.spawn(["/bin/sh", "-c", "exec sleep 30"])
+ except OSError:
+ return # no safe wait for this device on this OS: the spawn is refused
+ try:
+ _assert_idle_read_waits(fallback)
+ finally:
+ fallback.close()
diff --git a/tools/environments/fd_wait.py b/tools/environments/fd_wait.py
index ef9e11e8a0f..674f14a0dd3 100644
--- a/tools/environments/fd_wait.py
+++ b/tools/environments/fd_wait.py
@@ -13,7 +13,10 @@ fd of its own.
"""
import errno
+import os
import selectors
+import stat
+import sys
class FdWaiter:
@@ -39,7 +42,7 @@ class FdWaiter:
def open_fd_waiter(fd: int):
"""Return an :class:`FdWaiter` for *fd*, or None only when *fd* is closed/invalid
- and there is nothing to drain."""
+ and there is nothing to drain. Raises OSError when no safe wait exists."""
try:
return FdWaiter(fd, selectors.DefaultSelector)
except ValueError:
@@ -47,6 +50,9 @@ def open_fd_waiter(fd: int):
except OSError as exc:
if exc.errno == errno.EBADF:
return None
- # The selector itself could not be set up (EMFILE: the process is at its fd limit)
- # while the pipe is still live. poll() needs no extra fd; never report that as EOF.
+ # The selector itself could not be set up (EMFILE: the process is at its fd limit)
+ # while the fd is still live. poll() needs no extra fd; never report that as EOF.
+ # On macOS it is no wait at all for a device (PTY): raise rather than spin.
+ if sys.platform == "darwin" and stat.S_ISCHR(os.fstat(fd).st_mode):
+ raise
return FdWaiter(fd, selectors.PollSelector)