Skip to content

Commit 5fa260a

Browse files
authored
Fix ClientResponse.close releasing the connection instead of closing (#7869) (#7873)
(cherry picked from commit 25ef450)
1 parent cede54b commit 5fa260a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

CHANGES/7869.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ClientResponse.close releasing the connection instead of closing

aiohttp/client_reqrep.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,9 @@ def close(self) -> None:
10331033
return
10341034

10351035
self._cleanup_writer()
1036-
self._release_connection()
1036+
if self._connection is not None:
1037+
self._connection.close()
1038+
self._connection = None
10371039

10381040
def release(self) -> Any:
10391041
if not self._released:

tests/test_client_functional.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from aiohttp import Fingerprint, ServerFingerprintMismatch, hdrs, web
2020
from aiohttp.abc import AbstractResolver
2121
from aiohttp.client_exceptions import TooManyRedirects
22+
from aiohttp.pytest_plugin import AiohttpClient, TestClient
2223
from aiohttp.test_utils import unused_port
2324

2425

@@ -3186,7 +3187,40 @@ async def handler(request):
31863187
await client.get("/")
31873188

31883189

3189-
async def test_read_timeout_on_prepared_response(aiohttp_client) -> None:
3190+
async def test_read_timeout_closes_connection(aiohttp_client: AiohttpClient) -> None:
3191+
request_count = 0
3192+
3193+
async def handler(request):
3194+
nonlocal request_count
3195+
request_count += 1
3196+
if request_count < 3:
3197+
await asyncio.sleep(0.5)
3198+
return web.Response(body=f"request:{request_count}")
3199+
3200+
app = web.Application()
3201+
app.add_routes([web.get("/", handler)])
3202+
3203+
timeout = aiohttp.ClientTimeout(total=0.1)
3204+
client: TestClient = await aiohttp_client(app, timeout=timeout)
3205+
with pytest.raises(asyncio.TimeoutError):
3206+
await client.get("/")
3207+
3208+
# Make sure its really closed
3209+
assert not client.session.connector._conns
3210+
3211+
with pytest.raises(asyncio.TimeoutError):
3212+
await client.get("/")
3213+
3214+
# Make sure its really closed
3215+
assert not client.session.connector._conns
3216+
result = await client.get("/")
3217+
assert await result.read() == b"request:3"
3218+
3219+
# Make sure its not closed
3220+
assert client.session.connector._conns
3221+
3222+
3223+
async def test_read_timeout_on_prepared_response(aiohttp_client: Any) -> None:
31903224
async def handler(request):
31913225
resp = aiohttp.web.StreamResponse()
31923226
await resp.prepare(request)

0 commit comments

Comments
 (0)