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

Avoid passing client writer to response when already finished #9485

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 13 additions & 7 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def _writer(self, writer: Optional["asyncio.Task[None]"]) -> None:
if writer is None:
return
if writer.done():
# The writer is already done, so we can reset it immediately.
self.__reset_writer()
# The writer is already done, so we can clear it immediately.
self.__writer = None
else:
writer.add_done_callback(self.__reset_writer)

Expand Down Expand Up @@ -701,6 +701,7 @@ async def send(self, conn: "Connection") -> "ClientResponse":
await writer.write_headers(status_line, self.headers)
coro = self.write_bytes(writer, conn)

task: Optional["asyncio.Task[None]"]
if sys.version_info >= (3, 12):
# Optimization for Python 3.12, try to write
# bytes immediately to avoid having to schedule
Expand All @@ -709,7 +710,11 @@ async def send(self, conn: "Connection") -> "ClientResponse":
else:
task = self.loop.create_task(coro)

self._writer = task
if task.done():
task = None
else:
self.__writer = task
bdraco marked this conversation as resolved.
Show resolved Hide resolved

response_class = self.response_class
assert response_class is not None
self.response = response_class(
Expand Down Expand Up @@ -785,7 +790,7 @@ def __init__(
method: str,
url: URL,
*,
writer: "asyncio.Task[None]",
writer: "Optional[asyncio.Task[None]]",
continue100: Optional["asyncio.Future[bool]"],
timer: Optional[BaseTimerContext],
request_info: RequestInfo,
Expand All @@ -802,7 +807,8 @@ def __init__(
self._real_url = url
self._url = url.with_fragment(None) if url.raw_fragment else url
self._body: Optional[bytes] = None
self._writer = writer
if writer is not None:
self._writer = writer
self._continue = continue100 # None by default
self._closed = True
self._history: Tuple[ClientResponse, ...] = ()
Expand Down Expand Up @@ -846,8 +852,8 @@ def _writer(self, writer: Optional["asyncio.Task[None]"]) -> None:
if writer is None:
return
if writer.done():
# The writer is already done, so we can reset it immediately.
self.__reset_writer()
# The writer is already done, so we can clear it immediately.
self.__writer = None
else:
writer.add_done_callback(self.__reset_writer)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ async def send(self, conn: Connection) -> ClientResponse:
resp = self.response_class(
self.method,
self.url,
writer=self._writer, # type: ignore[arg-type]
writer=self._writer,
continue100=self._continue,
timer=self._timer,
request_info=self.request_info,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ async def test_response_eof(
"get",
URL("http://def-cl-resp.org"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -346,7 +346,7 @@ async def test_response_eof_after_connection_detach(
"get",
URL("http://def-cl-resp.org"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down
18 changes: 9 additions & 9 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_proxy_server_hostname_default(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -351,7 +351,7 @@ def test_proxy_server_hostname_override(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_https_connect(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -615,7 +615,7 @@ def test_https_connect_certificate_error(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -692,7 +692,7 @@ def test_https_connect_ssl_error(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -767,7 +767,7 @@ def test_https_connect_http_proxy_error(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -843,7 +843,7 @@ def test_https_connect_resp_start_error(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -979,7 +979,7 @@ def test_https_connect_pass_ssl_context(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down Expand Up @@ -1072,7 +1072,7 @@ def test_https_auth(
"get",
URL("http://proxy.example.com"),
request_info=mock.Mock(),
writer=None, # type: ignore[arg-type]
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
Expand Down
Loading