Skip to content

Commit

Permalink
Fix ClientResponse.close releasing the connection instead of closing (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 22, 2023
1 parent 3a21134 commit 25ef450
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/7869.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ClientResponse.close releasing the connection instead of closing
4 changes: 3 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,9 @@ def close(self) -> None:
return

self._cleanup_writer()
self._release_connection()
if self._connection is not None:
self._connection.close()
self._connection = None

def release(self) -> Any:
if not self._released:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from aiohttp import Fingerprint, ServerFingerprintMismatch, hdrs, web
from aiohttp.abc import AbstractResolver
from aiohttp.client_exceptions import TooManyRedirects
from aiohttp.pytest_plugin import AiohttpClient, TestClient
from aiohttp.test_utils import unused_port


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


async def test_read_timeout_closes_connection(aiohttp_client: AiohttpClient) -> None:
request_count = 0

async def handler(request):
nonlocal request_count
request_count += 1
if request_count < 3:
await asyncio.sleep(0.5)
return web.Response(body=f"request:{request_count}")

app = web.Application()
app.add_routes([web.get("/", handler)])

timeout = aiohttp.ClientTimeout(total=0.1)
client: TestClient = await aiohttp_client(app, timeout=timeout)
with pytest.raises(asyncio.TimeoutError):
await client.get("/")

# Make sure its really closed
assert not client.session.connector._conns

with pytest.raises(asyncio.TimeoutError):
await client.get("/")

# Make sure its really closed
assert not client.session.connector._conns
result = await client.get("/")
assert await result.read() == b"request:3"

# Make sure its not closed
assert client.session.connector._conns


async def test_read_timeout_on_prepared_response(aiohttp_client: Any) -> None:
async def handler(request):
resp = aiohttp.web.StreamResponse()
Expand Down

0 comments on commit 25ef450

Please sign in to comment.