Skip to content

The agent that grows with you

fix(acp): a CLI that closes its input but keeps running fails fast

Tekniumceefcf5f97e5
diff --git a/agent/acp_client.py b/agent/acp_client.py
index 6caff6882ca..43dafecb5ab 100644
--- a/agent/acp_client.py
+++ b/agent/acp_client.py
@@ -15,6 +15,7 @@ Subclasses customise the launch defaults and vendor quirks through class attribu
from __future__ import annotations
import contextlib
+import errno
import json
import logging
import os
@@ -454,9 +455,13 @@ class ACPClient:
try:
proc.stdin.write(json.dumps({"jsonrpc": "2.0", "id": request_id, "method": method, "params": params}) + "\n")
proc.stdin.flush()
- except BrokenPipeError: # the CLI stopped reading (dying): report its exit text, not a bare EPIPE
+ except OSError as exc: # the CLI stopped reading: EPIPE, or EINVAL on a Windows pipe
+ if not isinstance(exc, BrokenPipeError) and exc.errno != errno.EINVAL:
+ raise
with contextlib.suppress(subprocess.TimeoutExpired):
- proc.wait(timeout=5)
+ proc.wait(timeout=5) # dying: report its exit text below, not a bare EPIPE
+ if proc.poll() is None: # alive but deaf: fail now, not after the whole request timeout
+ raise
deadline = session_deadline
while time.monotonic() < deadline and proc.poll() is None:
try:
diff --git a/tests/agent/test_acp_client_invariants.py b/tests/agent/test_acp_client_invariants.py
index 737328f6e4f..92644551906 100644
--- a/tests/agent/test_acp_client_invariants.py
+++ b/tests/agent/test_acp_client_invariants.py
@@ -335,6 +335,37 @@ def test_cli_that_stops_reading_is_reported_with_its_exit_text(tmp_path):
with client._session(30):
pass
+_DEAF_ACP_CLI = """import os, sys, time
+os.close(0) # stops reading but keeps running
+open(sys.argv[1], "w").close()
+time.sleep(120)
+"""
+
+
+def test_cli_that_stops_reading_but_keeps_running_fails_fast(tmp_path):
+ """Alive with its stdin closed: the request can never be read, so the write's broken pipe is the
+ answer; waiting for a reply would hang for the whole request timeout."""
+ server = tmp_path / "deaf_acp.py"
+ server.write_text(_DEAF_ACP_CLI, encoding="utf-8")
+ closed = tmp_path / "stdin-closed"
+ client = _AcmeClient(command=sys.executable, args=[str(server), str(closed)], acp_cwd=str(tmp_path))
+ real_spawn = client._spawn
+
+ def spawn_then_wait_for_the_close():
+ proc = real_spawn()
+ deadline = time.monotonic() + 30
+ while not closed.exists() and time.monotonic() < deadline:
+ time.sleep(0.01)
+ return proc
+
+ client._spawn = spawn_then_wait_for_the_close
+ started = time.monotonic()
+ with pytest.raises(OSError):
+ with client._session(900):
+ pass
+ assert time.monotonic() - started < 60
+
+
def test_model_discovery_does_not_allow_file_requests(tmp_path):
"""``list_models`` opens a session only to read the catalog: an fs request on that path is refused."""
target = tmp_path / "should-not-be-read.txt"