Skip to content

Commit

Permalink
Ensure error is raised when reading from closed client response. (#8878)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Aug 26, 2024
1 parent e058cbd commit 50a18d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/8878.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed response reading from closed session to throw an error immediately instead of timing out -- by :user:`Dreamsorcerer`.
3 changes: 3 additions & 0 deletions aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ def end_http_chunk_receiving(self) -> None:
set_result(waiter, None)

async def _wait(self, func_name: str) -> None:
if not self._protocol.connected:
raise RuntimeError("Connection closed.")

# StreamReader uses a future to link the protocol feed_data() method
# to a read coroutine. Running two read coroutines at the same time
# would have an unexpected behaviour. It would not possible to know
Expand Down
17 changes: 17 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3777,3 +3777,20 @@ async def handler(_: web.Request) -> web.Response:
) as exc_info:
await client.get("/")
assert exc_info.value.status == 400


async def test_exception_when_read_outside_of_session(
aiohttp_server: AiohttpServer,
) -> None:
async def handler(request: web.Request) -> web.Response:
return web.Response(body=b"1" * 1000000)

app = web.Application()
app.router.add_get("/", handler)

server = await aiohttp_server(app)
async with aiohttp.ClientSession() as sess:
resp = await sess.get(server.make_url("/"))

with pytest.raises(RuntimeError, match="Connection closed"):
await resp.read()

0 comments on commit 50a18d6

Please sign in to comment.