From 95de838176b7de8aa4de1873d0a5ead5e3caf8ef Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 11:11:20 -0500 Subject: [PATCH 1/9] Add test environment for problem 2e6eba86-5af3-4080-9a66-c77ca8c82117-v1 --- aiohttp/Dockerfile | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 aiohttp/Dockerfile diff --git a/aiohttp/Dockerfile b/aiohttp/Dockerfile new file mode 100644 index 00000000000..1236133dfb2 --- /dev/null +++ b/aiohttp/Dockerfile @@ -0,0 +1,8 @@ +FROM public.ecr.aws/x8v8d7g8/mars-base:latest + +WORKDIR /app +COPY . . + +RUN pip install -e .[test] pytest aiohttp +CMD ["/bin/bash"] + From 9810d14494d70c62a0f99e7577b82ce8f61717fc Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 11:37:11 -0500 Subject: [PATCH 2/9] Fix(client): Prevent Unclosed connector warning for external connectors --- aiohttp/client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 7a4ad715362..09acf059904 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -319,6 +319,8 @@ def __init__( if connector is None: connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout) + else: + connector_owner = False # Initialize these three attrs before raising any exception, # they are used in __del__ self._connector = connector @@ -1274,7 +1276,8 @@ async def close(self) -> None: if not self.closed: if self._connector is not None and self._connector_owner: await self._connector.close() - self._connector = None + else: + self._connector = None @property def closed(self) -> bool: From c60da99ad33a80092ccdcc31c802d86e3c4842cf Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 11:54:05 -0500 Subject: [PATCH 3/9] Chore: Add contributor and changelog for connector fix --- CHANGES/2e6eba86.bugfix.rst | 1 + CONTRIBUTORS.txt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 CHANGES/2e6eba86.bugfix.rst diff --git a/CHANGES/2e6eba86.bugfix.rst b/CHANGES/2e6eba86.bugfix.rst new file mode 100644 index 00000000000..eef32e074e4 --- /dev/null +++ b/CHANGES/2e6eba86.bugfix.rst @@ -0,0 +1 @@ +Fixed an issue where ``aiohttp.ClientSession`` would raise an ``Unclosed connector`` warning for externally provided connectors -- by :user:`Ruke3663`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 46547b871de..076ef189180 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -318,6 +318,7 @@ Roman Markeloff Roman Podoliaka Roman Postnov Rong Zhang +Ruke 3663 Samir Akarioh Samuel Colvin Samuel Gaist @@ -396,7 +397,6 @@ Willem de Groot William Grzybowski William S. Wilson Ong -wouter bolsterlee Xavier Halloran Xi Rui Xiang Li @@ -416,6 +416,7 @@ Yuvi Panda Zainab Lawal Zeal Wierslee Zlatan Sičanica +wouter bolsterlee Łukasz Setla Марк Коренберг Семён Марьясин From ecab4fb30b0856171ed850972ac199479245bc28 Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 12:02:59 -0500 Subject: [PATCH 4/9] Fix: Correct sorting of CONTRIBUTORS.txt to pass linter --- CONTRIBUTORS.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 076ef189180..5f0e92bdb69 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -397,6 +397,7 @@ Willem de Groot William Grzybowski William S. Wilson Ong +wouter bolsterlee Xavier Halloran Xi Rui Xiang Li @@ -416,7 +417,6 @@ Yuvi Panda Zainab Lawal Zeal Wierslee Zlatan Sičanica -wouter bolsterlee Łukasz Setla Марк Коренберг Семён Марьясин From d27635b812c80d22bd89ab180db62e5d7171e408 Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 12:12:14 -0500 Subject: [PATCH 5/9] Fix: Ensure connector is not set to None when not owned --- aiohttp/client.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 09acf059904..f31b68007e8 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -320,7 +320,14 @@ def __init__( if connector is None: connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout) else: + if connector_owner: + warnings.warn( + "connector_owner is deprecated and will be removed in aiohttp 4.0.", + DeprecationWarning, + stacklevel=2, + ) connector_owner = False + # Initialize these three attrs before raising any exception, # they are used in __del__ self._connector = connector @@ -335,6 +342,8 @@ def __init__( if connector._loop is not loop: raise RuntimeError("Session and connector have to use same event loop") + self._connector_owner = connector_owner + if cookie_jar is None: cookie_jar = CookieJar() self._cookie_jar = cookie_jar @@ -342,7 +351,6 @@ def __init__( if cookies: self._cookie_jar.update_cookies(cookies) - self._connector_owner = connector_owner self._default_auth = auth self._version = version self._json_serialize = json_serialize @@ -1276,8 +1284,8 @@ async def close(self) -> None: if not self.closed: if self._connector is not None and self._connector_owner: await self._connector.close() - else: - self._connector = None + + self._connector = None @property def closed(self) -> bool: From 40654d216a25909e7af0d79c58ea7bcdc6233bd7 Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 12:33:04 -0500 Subject: [PATCH 6/9] Fix: Correct property to not depend on connector state --- aiohttp/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index f31b68007e8..1266826b341 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -1293,7 +1293,7 @@ def closed(self) -> bool: A readonly property. """ - return self._connector is None or self._connector.closed + return self._connector is None @property def connector(self) -> BaseConnector | None: From 4d95043375b2765b98a3ea7d3875084c142c4f16 Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 12:44:26 -0500 Subject: [PATCH 7/9] Fix: Revert incorrect property logic and refine method --- aiohttp/client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 1266826b341..9082fef9bf8 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -1284,7 +1284,6 @@ async def close(self) -> None: if not self.closed: if self._connector is not None and self._connector_owner: await self._connector.close() - self._connector = None @property @@ -1293,7 +1292,7 @@ def closed(self) -> bool: A readonly property. """ - return self._connector is None + return self._connector is None or self._connector.closed @property def connector(self) -> BaseConnector | None: From 01eafba0bb9b51c012d2e89a5980d468b312acd7 Mon Sep 17 00:00:00 2001 From: Ruke3663 Date: Mon, 20 Oct 2025 12:53:12 -0500 Subject: [PATCH 8/9] Fix: Finalize connector ownership logic to pass all CI checks --- aiohttp/client.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 9082fef9bf8..c1bed55d936 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -318,15 +318,14 @@ def __init__( ) if connector is None: + connector_owner = True connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout) - else: - if connector_owner: - warnings.warn( - "connector_owner is deprecated and will be removed in aiohttp 4.0.", - DeprecationWarning, - stacklevel=2, - ) - connector_owner = False + elif connector_owner: + warnings.warn( + "connector_owner is deprecated and will be removed in aiohttp 4.0.", + DeprecationWarning, + stacklevel=2, + ) # Initialize these three attrs before raising any exception, # they are used in __del__ @@ -1292,7 +1291,7 @@ def closed(self) -> bool: A readonly property. """ - return self._connector is None or self._connector.closed + return self._connector is None @property def connector(self) -> BaseConnector | None: From 862818073aab917a9b4093821b0d3ee67fe49aa4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:14:30 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- aiohttp/Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/aiohttp/Dockerfile b/aiohttp/Dockerfile index 1236133dfb2..fdba6a07c4c 100644 --- a/aiohttp/Dockerfile +++ b/aiohttp/Dockerfile @@ -5,4 +5,3 @@ COPY . . RUN pip install -e .[test] pytest aiohttp CMD ["/bin/bash"] -