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

Close client connections when writer is unexpectedly cancelled #8041

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions aiohttp/client_reqrep.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern that occurred to me is what if this is part of a task that is being cancelled by the user? In that case we don't want to suppress.

Ideally, if we can detect if the cancellation is from our client timeout or similar, we'd suppress it, otherwise we'd reraise it. Or something along those lines...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, but might also need something to handle ClientTimeout. Can't remember exactly how the logic works, but if we hit the timeout at this point, then we probably don't actually want to time out, as we already received the response before the timeout.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think thats handled by

if exc.errno is None and isinstance(exc, asyncio.TimeoutError):

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Dict,
Iterable,
Expand Down Expand Up @@ -709,6 +710,7 @@
_closed = True # to allow __del__ for non-initialized properly response
_released = False
__writer = None
__writer_cancelled_internally = False

def __init__(
self,
Expand Down Expand Up @@ -767,6 +769,7 @@
if self.__writer is not None:
self.__writer.remove_done_callback(self.__reset_writer)
self.__writer = writer
self.__writer_cancelled_internally = False
if writer is not None:
writer.add_done_callback(self.__reset_writer)

Expand Down Expand Up @@ -1006,13 +1009,31 @@
else:
self._writer.add_done_callback(lambda f: self._release_connection())

async def _wait_for_writer(self, writer: Awaitable[None]) -> None:
"""Wait for the writer to finish.

If we cancel the writer, we will suppress the CancelledError
since we are going to close the connection anyway, but if
did not cancel the writer, we need to raise the exception.
"""
try:
await writer
except asyncio.CancelledError:

Check warning on line 1021 in aiohttp/client_reqrep.py

View check run for this annotation

Codecov / codecov/patch

aiohttp/client_reqrep.py#L1021

Added line #L1021 was not covered by tests
# Writer was cancelled, ensure the connection is closed
# since we won't be able to write the rest of the body
# we need to close the connection since we can't reuse it
self.close()

Check warning on line 1025 in aiohttp/client_reqrep.py

View check run for this annotation

Codecov / codecov/patch

aiohttp/client_reqrep.py#L1025

Added line #L1025 was not covered by tests
if not self.__writer_cancelled_internally:
raise

Check warning on line 1027 in aiohttp/client_reqrep.py

View check run for this annotation

Codecov / codecov/patch

aiohttp/client_reqrep.py#L1027

Added line #L1027 was not covered by tests

async def _wait_released(self) -> None:
if self._writer is not None:
await self._writer
await self._wait_for_writer(self._writer)
self._release_connection()

def _cleanup_writer(self) -> None:
if self._writer is not None:
self.__writer_cancelled_internally = True
self._writer.cancel()
self._session = None

Expand All @@ -1025,7 +1046,7 @@

async def wait_for_close(self) -> None:
if self._writer is not None:
await self._writer
await self._wait_for_writer(self._writer)
self.release()

async def read(self) -> bytes:
Expand Down