Skip to content

Commit

Permalink
Fixed 7616: EmptyStreamReader.iter_chunks() never ends (#7644)
Browse files Browse the repository at this point in the history
## What do these changes do?

Fixes an issue reported in
[7616](#7616) - An iteration
of the chunks of an EmptyStreamReader with iter_chunks() never ends.
## Are there changes in behavior for the user?

- `EmptyStreamReader` `iter_chunks()` does not get stuck
- First call of `EmptyStreamReader` `readchunk()` now returns `(b"",
False)`. All the subsequent calls return `(b"", True)`. Before this PR
it was always `(b"", True)`.

## Related issue number

[7616](#7616)
  • Loading branch information
mind1m authored Oct 3, 2023
1 parent f021ad9 commit 6639f7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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

0 comments on commit 6639f7d

Please sign in to comment.