From b96b01b13287608497497944c3a3148a75f8e6b1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Oct 2024 16:54:13 -0500 Subject: [PATCH] Only create the connection closed exception once (#9405) --- 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 c077e2f498f..6468b2de3cd 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -751,6 +751,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, # but will be set by the start() method. @@ -1106,7 +1109,7 @@ def _notify_content(self) -> None: content = self.content # content can be None here, but the types are cheated elsewhere. if content and content.exception() is None: # type: ignore[truthy-bool] - set_exception(content, ClientConnectionError("Connection closed")) + set_exception(content, _CONNECTION_CLOSED_EXCEPTION) self._released = True async def wait_for_close(self) -> None: