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/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()