Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGES/7616.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``EmptyStreamReader.iter_chunks()`` never ending -- by :user:`mind1m`
6 changes: 5 additions & 1 deletion aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def _read_nowait(self, n: int) -> bytes:

class EmptyStreamReader(StreamReader): # lgtm [py/missing-call-to-init]
def __init__(self) -> None:
pass
self._read_eof_chunk = False

def __repr__(self) -> str:
return "<%s>" % self.__class__.__name__
Expand Down Expand Up @@ -535,6 +535,10 @@ async def readany(self) -> bytes:
return b""

async def readchunk(self) -> Tuple[bytes, bool]:
if not self._read_eof_chunk:
self._read_eof_chunk = True
return (b"", False)

return (b"", True)

async def readexactly(self, n: int) -> bytes:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,12 +1109,22 @@ async def test_empty_stream_reader() -> None:
assert await s.read() == b""
assert await s.readline() == b""
assert await s.readany() == b""
assert await s.readchunk() == (b"", False)
assert await s.readchunk() == (b"", True)
with pytest.raises(asyncio.IncompleteReadError):
await s.readexactly(10)
assert s.read_nowait() == b""


async def test_empty_stream_reader_iter_chunks() -> None:
s = streams.EmptyStreamReader()

# check that iter_chunks() does not cause infinite loop
iter_chunks = s.iter_chunks()
with pytest.raises(StopAsyncIteration):
await iter_chunks.__anext__()


@pytest.fixture
async def buffer(loop: Any):
return streams.DataQueue(loop)
Expand Down