Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/acp/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import platform
import sys
from asyncio import transports as aio_transports
from asyncio.streams import FlowControlMixin
from collections.abc import AsyncIterator, Callable, Mapping
from contextlib import asynccontextmanager
from pathlib import Path
Expand All @@ -28,26 +29,24 @@
]


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

def pause_writing(self) -> None: # type: ignore[override]
self._paused = True
if self._drain_waiter is None:
self._drain_waiter = self._loop.create_future()
def connection_lost(self, exc: Exception | None) -> None:
super().connection_lost(exc)
if not self._closed.done():
if exc is None:
self._closed.set_result(None)
else:
self._closed.set_exception(exc)

def resume_writing(self) -> None: # type: ignore[override]
self._paused = False
if self._drain_waiter is not None and not self._drain_waiter.done():
self._drain_waiter.set_result(None)
self._drain_waiter = None

async def _drain_helper(self) -> None:
if self._paused and self._drain_waiter is not None:
await self._drain_waiter
def _get_close_waiter(self, stream: asyncio.StreamWriter) -> asyncio.Future[None]:
return self._closed


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


class _StdoutTransport(asyncio.BaseTransport):
def __init__(self) -> None:
def __init__(self, protocol: _WritePipeProtocol) -> None:
self._is_closing = False
self._protocol = protocol

def write(self, data: bytes) -> None: # type: ignore[override]
if self._is_closing:
return
try:
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
except Exception:
except Exception as exc:
_logger.exception("Error writing to stdout")
self._is_closing = True
self._protocol.connection_lost(exc)

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

def close(self) -> None: # type: ignore[override]
if self._is_closing:
return
self._is_closing = True
with contextlib.suppress(Exception):
sys.stdout.flush()
self._protocol.connection_lost(None)

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

write_protocol = _WritePipeProtocol()
transport = _StdoutTransport()
transport = _StdoutTransport(write_protocol)
writer = asyncio.StreamWriter(cast(aio_transports.WriteTransport, transport), write_protocol, None, loop)
return reader, writer

Expand All @@ -132,6 +137,10 @@ async def _posix_stdio_streams(
async def stdio_streams(limit: int | None = None) -> tuple[asyncio.StreamReader, asyncio.StreamWriter]:
"""Create stdio asyncio streams; on Windows use a thread feeder + custom stdout transport.

The writer supports ``close()`` followed by ``await wait_closed()``.
``drain()`` reports a disconnected output pipe, including while waiting for
buffered output to be consumed.

Args:
limit: Optional buffer limit for the stdin reader.
"""
Expand Down
102 changes: 102 additions & 0 deletions tests/real_user/test_stdio_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import asyncio
import io
import subprocess
import sys
import textwrap
from types import SimpleNamespace

import pytest

from acp.stdio import stdio_streams


@pytest.mark.skipif(sys.platform == "win32", reason="native POSIX pipe transport")
def test_stdio_writer_flushes_and_waits_for_close() -> None:
script = textwrap.dedent("""
import asyncio
from acp.stdio import stdio_streams

async def main():
_, writer = await stdio_streams()
writer.write(b"x" * (1024 * 1024))
await writer.drain()
writer.close()
await asyncio.wait_for(writer.wait_closed(), timeout=2)

asyncio.run(main())
""")
result = subprocess.run( # noqa: S603
[sys.executable, "-c", script], input=b"", capture_output=True, timeout=10
)
assert result.returncode == 0, result.stderr.decode()
assert result.stdout == b"x" * (1024 * 1024)


@pytest.mark.skipif(sys.platform == "win32", reason="native POSIX pipe transport")
def test_stdio_writer_drain_fails_when_reader_closes_under_backpressure() -> None:
script = textwrap.dedent("""
import asyncio
import contextlib
import sys
from acp.stdio import stdio_streams

async def main():
_, writer = await stdio_streams()
writer.write(b"x" * (1024 * 1024))
print("ready", file=sys.stderr, flush=True)
try:
try:
await asyncio.wait_for(writer.drain(), timeout=2)
except ConnectionError:
pass
else:
raise AssertionError("drain succeeded after the reader closed")
finally:
writer.close()
with contextlib.suppress(ConnectionError):
await asyncio.wait_for(writer.wait_closed(), timeout=2)

asyncio.run(main())
""")
with subprocess.Popen( # noqa: S603
[sys.executable, "-c", script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) as child:
assert child.stdout is not None
assert child.stderr is not None
try:
assert child.stderr.readline() == b"ready\n"
child.stdout.close()
assert child.wait(timeout=10) == 0, child.stderr.read().decode()
finally:
if child.poll() is None:
child.kill()
child.wait(timeout=5)


@pytest.mark.asyncio
@pytest.mark.parametrize("write_error", [False, True])
async def test_windows_stdio_writer_reports_close(monkeypatch: pytest.MonkeyPatch, write_error: bool) -> None:
class Output(io.BytesIO):
def write(self, data: bytes) -> int:
if write_error:
raise BrokenPipeError("reader closed")
return super().write(data)

output = Output()
# Exercise the public Windows adapter on controlled standard handles.
monkeypatch.setattr("acp.stdio.platform.system", lambda: "Windows")
monkeypatch.setattr(sys, "stdin", SimpleNamespace(buffer=io.BytesIO()))
monkeypatch.setattr(sys, "stdout", SimpleNamespace(buffer=output, flush=output.flush))
_, writer = await stdio_streams()
writer.write(b"hello")
if write_error:
with pytest.raises(ConnectionError):
await writer.drain()
with pytest.raises(BrokenPipeError):
await asyncio.wait_for(writer.wait_closed(), timeout=2)
else:
await writer.drain()
writer.close()
writer.close()
await asyncio.wait_for(writer.wait_closed(), timeout=2)
assert output.getvalue() == b"hello"