Skip to content

Commit 22ea5ac

Browse files
committed
fix(stdio): settle writer close and disconnection
1 parent 4fed584 commit 22ea5ac

2 files changed

Lines changed: 131 additions & 20 deletions

File tree

‎src/acp/stdio.py‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import platform
88
import sys
99
from asyncio import transports as aio_transports
10+
from asyncio.streams import FlowControlMixin
1011
from collections.abc import AsyncIterator, Callable, Mapping
1112
from contextlib import asynccontextmanager
1213
from pathlib import Path
@@ -28,26 +29,24 @@
2829
]
2930

3031

31-
class _WritePipeProtocol(asyncio.BaseProtocol):
32+
class _WritePipeProtocol(FlowControlMixin):
3233
def __init__(self) -> None:
33-
self._loop = asyncio.get_running_loop()
34-
self._paused = False
35-
self._drain_waiter: asyncio.Future[None] | None = None
34+
loop = asyncio.get_running_loop()
35+
super().__init__(loop=loop)
36+
self._closed: asyncio.Future[None] = loop.create_future()
37+
# Callers may handle a failed drain without also awaiting wait_closed.
38+
self._closed.add_done_callback(lambda future: None if future.cancelled() else future.exception())
3639

37-
def pause_writing(self) -> None: # type: ignore[override]
38-
self._paused = True
39-
if self._drain_waiter is None:
40-
self._drain_waiter = self._loop.create_future()
40+
def connection_lost(self, exc: Exception | None) -> None:
41+
super().connection_lost(exc)
42+
if not self._closed.done():
43+
if exc is None:
44+
self._closed.set_result(None)
45+
else:
46+
self._closed.set_exception(exc)
4147

42-
def resume_writing(self) -> None: # type: ignore[override]
43-
self._paused = False
44-
if self._drain_waiter is not None and not self._drain_waiter.done():
45-
self._drain_waiter.set_result(None)
46-
self._drain_waiter = None
47-
48-
async def _drain_helper(self) -> None:
49-
if self._paused and self._drain_waiter is not None:
50-
await self._drain_waiter
48+
def _get_close_waiter(self, stream: asyncio.StreamWriter) -> asyncio.Future[None]:
49+
return self._closed
5150

5251

5352
def _start_stdin_feeder(loop: asyncio.AbstractEventLoop, reader: asyncio.StreamReader) -> None:
@@ -68,17 +67,20 @@ def blocking_read() -> None:
6867

6968

7069
class _StdoutTransport(asyncio.BaseTransport):
71-
def __init__(self) -> None:
70+
def __init__(self, protocol: _WritePipeProtocol) -> None:
7271
self._is_closing = False
72+
self._protocol = protocol
7373

7474
def write(self, data: bytes) -> None: # type: ignore[override]
7575
if self._is_closing:
7676
return
7777
try:
7878
sys.stdout.buffer.write(data)
7979
sys.stdout.buffer.flush()
80-
except Exception:
80+
except Exception as exc:
8181
_logger.exception("Error writing to stdout")
82+
self._is_closing = True
83+
self._protocol.connection_lost(exc)
8284

8385
def can_write_eof(self) -> bool: # type: ignore[override]
8486
return False
@@ -87,9 +89,12 @@ def is_closing(self) -> bool: # type: ignore[override]
8789
return self._is_closing
8890

8991
def close(self) -> None: # type: ignore[override]
92+
if self._is_closing:
93+
return
9094
self._is_closing = True
9195
with contextlib.suppress(Exception):
9296
sys.stdout.flush()
97+
self._protocol.connection_lost(None)
9398

9499
def abort(self) -> None: # type: ignore[override]
95100
self.close()
@@ -108,7 +113,7 @@ async def _windows_stdio_streams(
108113
_start_stdin_feeder(loop, reader)
109114

110115
write_protocol = _WritePipeProtocol()
111-
transport = _StdoutTransport()
116+
transport = _StdoutTransport(write_protocol)
112117
writer = asyncio.StreamWriter(cast(aio_transports.WriteTransport, transport), write_protocol, None, loop)
113118
return reader, writer
114119

@@ -132,6 +137,10 @@ async def _posix_stdio_streams(
132137
async def stdio_streams(limit: int | None = None) -> tuple[asyncio.StreamReader, asyncio.StreamWriter]:
133138
"""Create stdio asyncio streams; on Windows use a thread feeder + custom stdout transport.
134139
140+
The writer supports ``close()`` followed by ``await wait_closed()``.
141+
``drain()`` reports a disconnected output pipe, including while waiting for
142+
buffered output to be consumed.
143+
135144
Args:
136145
limit: Optional buffer limit for the stdin reader.
137146
"""
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import asyncio
2+
import io
3+
import subprocess
4+
import sys
5+
import textwrap
6+
from types import SimpleNamespace
7+
8+
import pytest
9+
10+
from acp.stdio import stdio_streams
11+
12+
13+
@pytest.mark.skipif(sys.platform == "win32", reason="native POSIX pipe transport")
14+
def test_stdio_writer_flushes_and_waits_for_close() -> None:
15+
script = textwrap.dedent("""
16+
import asyncio
17+
from acp.stdio import stdio_streams
18+
19+
async def main():
20+
_, writer = await stdio_streams()
21+
writer.write(b"x" * (1024 * 1024))
22+
await writer.drain()
23+
writer.close()
24+
await asyncio.wait_for(writer.wait_closed(), timeout=2)
25+
26+
asyncio.run(main())
27+
""")
28+
result = subprocess.run( # noqa: S603
29+
[sys.executable, "-c", script], input=b"", capture_output=True, timeout=10
30+
)
31+
assert result.returncode == 0, result.stderr.decode()
32+
assert result.stdout == b"x" * (1024 * 1024)
33+
34+
35+
@pytest.mark.skipif(sys.platform == "win32", reason="native POSIX pipe transport")
36+
def test_stdio_writer_drain_fails_when_reader_closes_under_backpressure() -> None:
37+
script = textwrap.dedent("""
38+
import asyncio
39+
import contextlib
40+
import sys
41+
from acp.stdio import stdio_streams
42+
43+
async def main():
44+
_, writer = await stdio_streams()
45+
writer.write(b"x" * (1024 * 1024))
46+
print("ready", file=sys.stderr, flush=True)
47+
try:
48+
try:
49+
await asyncio.wait_for(writer.drain(), timeout=2)
50+
except ConnectionError:
51+
pass
52+
else:
53+
raise AssertionError("drain succeeded after the reader closed")
54+
finally:
55+
writer.close()
56+
with contextlib.suppress(ConnectionError):
57+
await asyncio.wait_for(writer.wait_closed(), timeout=2)
58+
59+
asyncio.run(main())
60+
""")
61+
with subprocess.Popen( # noqa: S603
62+
[sys.executable, "-c", script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
63+
) as child:
64+
assert child.stdout is not None
65+
assert child.stderr is not None
66+
try:
67+
assert child.stderr.readline() == b"ready\n"
68+
child.stdout.close()
69+
assert child.wait(timeout=10) == 0, child.stderr.read().decode()
70+
finally:
71+
if child.poll() is None:
72+
child.kill()
73+
child.wait(timeout=5)
74+
75+
76+
@pytest.mark.asyncio
77+
@pytest.mark.parametrize("write_error", [False, True])
78+
async def test_windows_stdio_writer_reports_close(monkeypatch: pytest.MonkeyPatch, write_error: bool) -> None:
79+
class Output(io.BytesIO):
80+
def write(self, data: bytes) -> int:
81+
if write_error:
82+
raise BrokenPipeError("reader closed")
83+
return super().write(data)
84+
85+
output = Output()
86+
# Exercise the public Windows adapter on controlled standard handles.
87+
monkeypatch.setattr("acp.stdio.platform.system", lambda: "Windows")
88+
monkeypatch.setattr(sys, "stdin", SimpleNamespace(buffer=io.BytesIO()))
89+
monkeypatch.setattr(sys, "stdout", SimpleNamespace(buffer=output, flush=output.flush))
90+
_, writer = await stdio_streams()
91+
writer.write(b"hello")
92+
if write_error:
93+
with pytest.raises(ConnectionError):
94+
await writer.drain()
95+
with pytest.raises(BrokenPipeError):
96+
await asyncio.wait_for(writer.wait_closed(), timeout=2)
97+
else:
98+
await writer.drain()
99+
writer.close()
100+
writer.close()
101+
await asyncio.wait_for(writer.wait_closed(), timeout=2)
102+
assert output.getvalue() == b"hello"

0 commit comments

Comments
 (0)