Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ClientResponse.close releasing the connection instead of closing #7869

Merged
merged 8 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading