From d93c78f81fcacf8f13c7de7ab41b4a6a147125dc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Oct 2024 17:59:19 -0500 Subject: [PATCH] [PR #9405/b96b01b backport][3.11] Only create the connection closed exception once (#9409) --- aiohttp/client_reqrep.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index c68dfcbd1bd..7664ab15201 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -819,6 +819,9 @@ async def _on_headers_request_sent( await trace.send_request_headers(method, url, headers) +_CONNECTION_CLOSED_EXCEPTION = ClientConnectionError("Connection closed") + + class ClientResponse(HeadersMixin): # Some of these attributes are None when created, @@ -1178,7 +1181,7 @@ def _cleanup_writer(self) -> None: def _notify_content(self) -> None: content = self.content if content and content.exception() is None: - set_exception(content, ClientConnectionError("Connection closed")) + set_exception(content, _CONNECTION_CLOSED_EXCEPTION) self._released = True async def wait_for_close(self) -> None: