Skip to content

Commit 25ef450

Browse files
authored
Fix ClientResponse.close releasing the connection instead of closing (#7869)
1 parent 3a21134 commit 25ef450

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGES/7869.bugfix

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,9 @@ def close(self) -> None:
967967
return
968968

969969
self._cleanup_writer()
970-
self._release_connection()
970+
if self._connection is not None:
971+
self._connection.close()
972+
self._connection = None
971973

972974
def release(self) -> Any:
973975
if not self._released:

tests/test_client_functional.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from aiohttp import Fingerprint, ServerFingerprintMismatch, hdrs, web
2121
from aiohttp.abc import AbstractResolver
2222
from aiohttp.client_exceptions import TooManyRedirects
23+
from aiohttp.pytest_plugin import AiohttpClient, TestClient
2324
from aiohttp.test_utils import unused_port
2425

2526

@@ -3175,6 +3176,39 @@ async def handler(request):
31753176
await client.get("/")
31763177

31773178

3179+
async def test_read_timeout_closes_connection(aiohttp_client: AiohttpClient) -> None:
3180+
request_count = 0
3181+
3182+
async def handler(request):
3183+
nonlocal request_count
3184+
request_count += 1
3185+
if request_count < 3:
3186+
await asyncio.sleep(0.5)
3187+
return web.Response(body=f"request:{request_count}")
3188+
3189+
app = web.Application()
3190+
app.add_routes([web.get("/", handler)])
3191+
3192+
timeout = aiohttp.ClientTimeout(total=0.1)
3193+
client: TestClient = await aiohttp_client(app, timeout=timeout)
3194+
with pytest.raises(asyncio.TimeoutError):
3195+
await client.get("/")
3196+
3197+
# Make sure its really closed
3198+
assert not client.session.connector._conns
3199+
3200+
with pytest.raises(asyncio.TimeoutError):
3201+
await client.get("/")
3202+
3203+
# Make sure its really closed
3204+
assert not client.session.connector._conns
3205+
result = await client.get("/")
3206+
assert await result.read() == b"request:3"
3207+
3208+
# Make sure its not closed
3209+
assert client.session.connector._conns
3210+
3211+
31783212
async def test_read_timeout_on_prepared_response(aiohttp_client: Any) -> None:
31793213
async def handler(request):
31803214
resp = aiohttp.web.StreamResponse()

0 commit comments

Comments
 (0)