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
…) (#8901)
  • Loading branch information
Dreamsorcerer authored Aug 26, 2024
1 parent 68629b5 commit 6c7aebb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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 @@ -296,6 +296,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
19 changes: 18 additions & 1 deletion tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
SocketTimeoutError,
TooManyRedirects,
)
from aiohttp.pytest_plugin import AiohttpClient, TestClient
from aiohttp.pytest_plugin import AiohttpClient, AiohttpServer, TestClient
from aiohttp.test_utils import unused_port


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

await session.get("/")


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 6c7aebb

Please sign in to comment.