Skip to content

Commit

Permalink
Fix ClientResponse.close releasing the connection instead of closing (#…
Browse files Browse the repository at this point in the history
…7869) (#7874)

(cherry picked from commit 25ef450)
  • Loading branch information
bdraco committed Nov 23, 2023
1 parent 3c63ca0 commit 45147c4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/7869.bugfix
@@ -0,0 +1 @@
Fix ClientResponse.close releasing the connection instead of closing
4 changes: 3 additions & 1 deletion aiohttp/client_reqrep.py
Expand Up @@ -1033,7 +1033,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
36 changes: 35 additions & 1 deletion tests/test_client_functional.py
Expand Up @@ -19,6 +19,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 @@ -3186,7 +3187,40 @@ async def handler(request):
await client.get("/")


async def test_read_timeout_on_prepared_response(aiohttp_client) -> None:
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()
await resp.prepare(request)
Expand Down

0 comments on commit 45147c4

Please sign in to comment.