The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(terminal): never report a selector setup failure as EOF
Tekniumcb67cb3521fb
diff --git a/tests/tools/test_drain_high_fd.py b/tests/tools/test_drain_high_fd.pyindex dc81bcdb9d6..91dfe9cab18 100644--- a/tests/tools/test_drain_high_fd.py+++ b/tests/tools/test_drain_high_fd.py@@ -37,8 +37,10 @@ def high_fds():passif resource.getrlimit(resource.RLIMIT_NOFILE)[0] < want:pytest.skip("fd limit too low to reproduce")- held = [os.open(os.devnull, os.O_RDONLY) for _ in range(_TARGET_LOW_FDS)]+ held = []try:+ while len(held) < _TARGET_LOW_FDS:+ held.append(os.open(os.devnull, os.O_RDONLY))assert held[-1] >= 1024, "setup failed to push fds above FD_SETSIZE"yieldfinally:@@ -47,18 +49,29 @@ def high_fds():@pytest.mark.platforms("posix")-def test_command_output_survives_pipe_fd_above_fd_setsize(high_fds, tmp_path):- """Foreground ``terminal`` drain and the background-process reader both keep output."""+def test_command_output_survives_pipe_fd_above_fd_setsize(high_fds, tmp_path, monkeypatch):+ """Foreground ``terminal`` drain and the background-process reader both keep output,+ including when the selector itself cannot be created (EMFILE: epoll/kqueue need an fd)."""+ import errno++ from tools.environments import fd_waitfrom tools.environments.local import LocalEnvironmentfrom tools.process_registry import ProcessRegistry+ def _emfile():+ raise OSError(errno.EMFILE, "Too many open files")+env = LocalEnvironment(cwd=str(tmp_path))try:result = env.execute("echo hello-high-fd")+ with monkeypatch.context() as m:+ m.setattr(fd_wait.selectors, "DefaultSelector", _emfile)+ at_fd_limit = env.execute("echo still-here")finally:env.cleanup()assert result["returncode"] == 0assert "hello-high-fd" in result["output"]+ assert "still-here" in at_fd_limit["output"]registry = ProcessRegistry()session = registry.spawn_local("echo bg-high-fd", cwd=str(tmp_path), task_id="high-fd")diff --git a/tools/environments/fd_wait.py b/tools/environments/fd_wait.pyindex dd301d9f384..ef9e11e8a0f 100644--- a/tools/environments/fd_wait.py+++ b/tools/environments/fd_wait.py@@ -6,20 +6,22 @@ that many open fds, and every drain swallowed that ValueError as "fd alreadyclosed", silently discarding output (issue #94928).``selectors.DefaultSelector`` (epoll on Linux, kqueue on macOS) has no FD_SETSIZE-ceiling. ``poll()`` is not a substitute: macOS ``poll()`` does not support device+ceiling. ``poll()`` is not the default: macOS ``poll()`` does not support devicefds and reports a PTY master as POLLNVAL, i.e. "ready" at once, so a PTY read loop-would spin a core.+would spin a core. It stays the fallback because, unlike epoll/kqueue, it needs no+fd of its own."""+import errnoimport selectorsclass FdWaiter:- """Readability wait on one fd. Owns a selector (itself one fd): create it once- per reader lifetime and ``close()`` it when the reader is done."""+ """Readability wait on one fd. Owns a selector (epoll/kqueue are fds themselves):+ create it once per reader lifetime and ``close()`` it when the reader is done."""- def __init__(self, fd: int):- self._selector = selectors.DefaultSelector()+ def __init__(self, fd: int, selector_cls):+ self._selector = selector_cls()try:self._selector.register(fd, selectors.EVENT_READ)except BaseException:@@ -36,9 +38,15 @@ class FdWaiter:def open_fd_waiter(fd: int):- """Return an :class:`FdWaiter` for *fd*, or None when *fd* cannot be watched- (already closed / invalid) and there is nothing to drain."""+ """Return an :class:`FdWaiter` for *fd*, or None only when *fd* is closed/invalid+ and there is nothing to drain."""try:- return FdWaiter(fd)- except (ValueError, OSError):+ return FdWaiter(fd, selectors.DefaultSelector)+ except ValueError:return None+ 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.+ return FdWaiter(fd, selectors.PollSelector)