From 5dcb36a4083507a1deefa0af85b3924db624e4a4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 22 May 2025 18:37:06 -0500 Subject: [PATCH 1/2] Fix some missing connector closes in tests (#10956) --- tests/test_client_middleware.py | 2 ++ tests/test_proxy_functional.py | 3 +++ tests/test_web_sendfile_functional.py | 1 + 3 files changed, 6 insertions(+) diff --git a/tests/test_client_middleware.py b/tests/test_client_middleware.py index 883d853d2e8..9d49b750333 100644 --- a/tests/test_client_middleware.py +++ b/tests/test_client_middleware.py @@ -793,6 +793,8 @@ async def blocking_middleware( # Check that no connections were leaked assert len(connector._conns) == 0 + await connector.close() + async def test_client_middleware_blocks_connection_without_dns_lookup( aiohttp_server: AiohttpServer, diff --git a/tests/test_proxy_functional.py b/tests/test_proxy_functional.py index 247b2daae13..0a375cf61c7 100644 --- a/tests/test_proxy_functional.py +++ b/tests/test_proxy_functional.py @@ -463,6 +463,7 @@ async def test_proxy_http_acquired_cleanup( assert 0 == len(conn._acquired) await sess.close() + await conn.close() @pytest.mark.skip("we need to reconsider how we test this") @@ -487,6 +488,7 @@ async def request() -> None: assert 0 == len(conn._acquired) await sess.close() + await conn.close() @pytest.mark.skip("we need to reconsider how we test this") @@ -521,6 +523,7 @@ async def request(pid: int) -> ClientResponse: assert {resp.status for resp in responses} == {200} await sess.close() + await conn.close() @pytest.mark.xfail diff --git a/tests/test_web_sendfile_functional.py b/tests/test_web_sendfile_functional.py index eaef4930d09..591d7af7fce 100644 --- a/tests/test_web_sendfile_functional.py +++ b/tests/test_web_sendfile_functional.py @@ -625,6 +625,7 @@ async def test_static_file_ssl( resp.release() await client.close() + await conn.close() async def test_static_file_directory_traversal_attack( From cc234c6df051228245dd01d0ec638ea7067eeb26 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 22 May 2025 21:18:53 -0500 Subject: [PATCH 2/2] Change ClientSession middlewares default to be an empty tuple (#10959) --- CHANGES/10959.feature.rst | 1 + aiohttp/client.py | 2 +- docs/client_reference.rst | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 120000 CHANGES/10959.feature.rst diff --git a/CHANGES/10959.feature.rst b/CHANGES/10959.feature.rst new file mode 120000 index 00000000000..b565aa68ee0 --- /dev/null +++ b/CHANGES/10959.feature.rst @@ -0,0 +1 @@ +9732.feature.rst \ No newline at end of file diff --git a/aiohttp/client.py b/aiohttp/client.py index d615ec181c5..2b1ccb8ee03 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -296,7 +296,7 @@ def __init__( max_line_size: int = 8190, max_field_size: int = 8190, fallback_charset_resolver: _CharsetResolver = lambda r, b: "utf-8", - middlewares: Optional[Sequence[ClientMiddlewareType]] = None, + middlewares: Sequence[ClientMiddlewareType] = (), ) -> None: # We initialise _connector to None immediately, as it's referenced in __del__() # and could cause issues if an exception occurs during initialisation. diff --git a/docs/client_reference.rst b/docs/client_reference.rst index 8101f25a872..cfb0e191196 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -53,7 +53,7 @@ The client session supports the context manager protocol for self closing. trust_env=False, \ requote_redirect_url=True, \ trace_configs=None, \ - middlewares=None, \ + middlewares=(), \ read_bufsize=2**16, \ max_line_size=8190, \ max_field_size=8190, \ @@ -216,7 +216,7 @@ The client session supports the context manager protocol for self closing. :param middlewares: A sequence of middleware instances to apply to all session requests. Each middleware must match the :type:`ClientMiddlewareType` signature. - ``None`` (default) is used when no middleware is needed. + ``()`` (empty tuple, default) is used when no middleware is needed. See :ref:`aiohttp-client-middleware` for more information. .. versionadded:: 3.12