Skip to content

Commit 41647f4

Browse files
authored
Fixed feed_data after feed_eof assertion errors on asyncio (#752)
Fixes #490.
1 parent 38890e6 commit 41647f4

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

docs/versionhistory.rst

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
2929
- Fixed ``to_process.run_sync()`` failing to initialize if ``__main__.__file__`` pointed
3030
to a file in a nonexistent directory
3131
(`#696 <https://github.com/agronholm/anyio/issues/696>`_)
32+
- Fixed ``AssertionError: feed_data after feed_eof`` on asyncio when a subprocess is
33+
closed early, before its output has been read
34+
(`#490 <https://github.com/agronholm/anyio/issues/490>`_)
3235
- Fixed ``TaskInfo.has_pending_cancellation()`` on asyncio not respecting shielded
3336
scopes (`#771 <https://github.com/agronholm/anyio/issues/771>`_; PR by @gschaffner)
3437
- Fixed ``SocketStream.receive()`` returning ``bytearray`` instead of ``bytes`` when

src/anyio/_backends/_asyncio.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ async def receive(self, max_bytes: int = 65536) -> bytes:
930930
raise EndOfStream
931931

932932
async def aclose(self) -> None:
933-
self._stream.feed_eof()
933+
self._stream.set_exception(ClosedResourceError())
934934
await AsyncIOBackend.checkpoint()
935935

936936

tests/test_subprocesses.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
import pytest
1414
from pytest import FixtureRequest
1515

16-
from anyio import CancelScope, ClosedResourceError, open_process, run_process
16+
from anyio import (
17+
CancelScope,
18+
ClosedResourceError,
19+
create_task_group,
20+
open_process,
21+
run_process,
22+
)
1723
from anyio.streams.buffered import BufferedByteReceiveStream
1824

1925
pytestmark = pytest.mark.anyio
@@ -289,3 +295,33 @@ async def test_py39_arguments(
289295
pytest.skip(f"the {argname!r} argument is not supported by uvloop yet")
290296

291297
raise
298+
299+
300+
async def test_close_early() -> None:
301+
"""Regression test for #490."""
302+
code = dedent("""\
303+
import sys
304+
for _ in range(100):
305+
sys.stdout.buffer.write(bytes(range(256)))
306+
""")
307+
308+
async with await open_process([sys.executable, "-c", code]):
309+
pass
310+
311+
312+
async def test_close_while_reading() -> None:
313+
code = dedent("""\
314+
import time
315+
316+
time.sleep(3)
317+
""")
318+
319+
async with await open_process(
320+
[sys.executable, "-c", code]
321+
) as process, create_task_group() as tg:
322+
assert process.stdout
323+
tg.start_soon(process.stdout.aclose)
324+
with pytest.raises(ClosedResourceError):
325+
await process.stdout.receive()
326+
327+
process.terminate()

0 commit comments

Comments
 (0)