Skip to content

Commit 804e16c

Browse files
Ensure error is raised when reading from closed client response. (#8878) (#8902)
1 parent e2631c7 commit 804e16c

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

CHANGES/8878.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed response reading from closed session to throw an error immediately instead of timing out -- by :user:`Dreamsorcerer`.

aiohttp/streams.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ def end_http_chunk_receiving(self) -> None:
296296
set_result(waiter, None)
297297

298298
async def _wait(self, func_name: str) -> None:
299+
if not self._protocol.connected:
300+
raise RuntimeError("Connection closed.")
301+
299302
# StreamReader uses a future to link the protocol feed_data() method
300303
# to a read coroutine. Running two read coroutines at the same time
301304
# would have an unexpected behaviour. It would not possible to know

tests/test_client_functional.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
SocketTimeoutError,
3030
TooManyRedirects,
3131
)
32-
from aiohttp.pytest_plugin import AiohttpClient, TestClient
32+
from aiohttp.pytest_plugin import AiohttpClient, AiohttpServer, TestClient
3333
from aiohttp.test_utils import unused_port
3434

3535

@@ -3645,3 +3645,20 @@ async def handler(_: web.Request) -> web.Response:
36453645
session = await aiohttp_client(app, raise_for_status=None) # type: ignore[arg-type]
36463646

36473647
await session.get("/")
3648+
3649+
3650+
async def test_exception_when_read_outside_of_session(
3651+
aiohttp_server: AiohttpServer,
3652+
) -> None:
3653+
async def handler(request: web.Request) -> web.Response:
3654+
return web.Response(body=b"1" * 1000000)
3655+
3656+
app = web.Application()
3657+
app.router.add_get("/", handler)
3658+
3659+
server = await aiohttp_server(app)
3660+
async with aiohttp.ClientSession() as sess:
3661+
resp = await sess.get(server.make_url("/"))
3662+
3663+
with pytest.raises(RuntimeError, match="Connection closed"):
3664+
await resp.read()

0 commit comments

Comments
 (0)