diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 5ef74e649..bfa9619e0 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -119,6 +119,7 @@ def __init__( # and the set of connections that are servicing those requests. self._connections: list[AsyncConnectionInterface] = [] self._requests: list[AsyncPoolRequest] = [] + self._retired_connections: set[AsyncConnectionInterface] = set() # We only mutate the state of the connection pool within an 'optional_thread_lock' # context. This holds a threading lock unless we're running in async mode, @@ -241,7 +242,18 @@ async def handle_async_request(self, request: Request) -> Response: # handle a request, but then become unavailable. # # In this case we clear the connection and try again. - pool_request.clear_connection() + # Retire persistently unavailable connections without + # disrupting another request that is still using them. + with self._optional_thread_lock: + pool_request.clear_connection() + still_in_use = any( + request.connection is connection + for request in self._requests + ) + if connection in self._connections and ( + connection.is_available() or not still_in_use + ): + self._retired_connections.add(connection) else: break # pragma: nocover @@ -282,6 +294,14 @@ def _assign_requests_to_connections(self) -> list[AsyncConnectionInterface]: # First we handle cleaning up any connections that are closed, # have expired their keep-alive, or surplus idle connections. for connection in list(self._connections): + if connection in self._retired_connections: + if not any( + request.connection is connection for request in self._requests + ): + self._connections.remove(connection) + self._retired_connections.remove(connection) + closing_connections.append(connection) + continue if connection.is_closed(): # log: "removing closed connection" self._connections.remove(connection) @@ -305,10 +325,14 @@ def _assign_requests_to_connections(self) -> list[AsyncConnectionInterface]: available_connections = [ connection for connection in self._connections - if connection.can_handle_request(origin) and connection.is_available() + if connection not in self._retired_connections + and connection.can_handle_request(origin) + and connection.is_available() ] idle_connections = [ - connection for connection in self._connections if connection.is_idle() + connection + for connection in self._connections + if connection not in self._retired_connections and connection.is_idle() ] # There are three cases for how we may be able to handle the request: @@ -350,6 +374,7 @@ async def aclose(self) -> None: with self._optional_thread_lock: closing_connections = list(self._connections) self._connections = [] + self._retired_connections.clear() await self._close_connections(closing_connections) async def __aenter__(self) -> AsyncConnectionPool: diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 4b26f9c63..d0fccf520 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -119,6 +119,7 @@ def __init__( # and the set of connections that are servicing those requests. self._connections: list[ConnectionInterface] = [] self._requests: list[PoolRequest] = [] + self._retired_connections: set[ConnectionInterface] = set() # We only mutate the state of the connection pool within an 'optional_thread_lock' # context. This holds a threading lock unless we're running in async mode, @@ -241,7 +242,18 @@ def handle_request(self, request: Request) -> Response: # handle a request, but then become unavailable. # # In this case we clear the connection and try again. - pool_request.clear_connection() + # Retire persistently unavailable connections without + # disrupting another request that is still using them. + with self._optional_thread_lock: + pool_request.clear_connection() + still_in_use = any( + request.connection is connection + for request in self._requests + ) + if connection in self._connections and ( + connection.is_available() or not still_in_use + ): + self._retired_connections.add(connection) else: break # pragma: nocover @@ -282,6 +294,14 @@ def _assign_requests_to_connections(self) -> list[ConnectionInterface]: # First we handle cleaning up any connections that are closed, # have expired their keep-alive, or surplus idle connections. for connection in list(self._connections): + if connection in self._retired_connections: + if not any( + request.connection is connection for request in self._requests + ): + self._connections.remove(connection) + self._retired_connections.remove(connection) + closing_connections.append(connection) + continue if connection.is_closed(): # log: "removing closed connection" self._connections.remove(connection) @@ -305,10 +325,14 @@ def _assign_requests_to_connections(self) -> list[ConnectionInterface]: available_connections = [ connection for connection in self._connections - if connection.can_handle_request(origin) and connection.is_available() + if connection not in self._retired_connections + and connection.can_handle_request(origin) + and connection.is_available() ] idle_connections = [ - connection for connection in self._connections if connection.is_idle() + connection + for connection in self._connections + if connection not in self._retired_connections and connection.is_idle() ] # There are three cases for how we may be able to handle the request: @@ -350,6 +374,7 @@ def close(self) -> None: with self._optional_thread_lock: closing_connections = list(self._connections) self._connections = [] + self._retired_connections.clear() self._close_connections(closing_connections) def __enter__(self) -> ConnectionPool: diff --git a/tests/test_pool_stale_connection.py b/tests/test_pool_stale_connection.py new file mode 100644 index 000000000..0f3546cc8 --- /dev/null +++ b/tests/test_pool_stale_connection.py @@ -0,0 +1,180 @@ +from unittest.mock import AsyncMock, Mock + +import pytest + +import httpcore + + +def make_connection(*, stale=False, asynchronous=False): + connection = Mock() + connection.is_closed.return_value = False + connection.has_expired.return_value = False + connection.is_idle.return_value = False + connection.is_available.return_value = True + connection.can_handle_request.return_value = True + if asynchronous: + connection.handle_async_request = AsyncMock( + side_effect=( + [ + httpcore.ConnectionNotAvailable(), + AssertionError("unavailable connection was reused"), + ] + if stale + else None + ), + return_value=None if stale else httpcore.Response(200, content=b"ok"), + ) + connection.aclose = AsyncMock() + else: + connection.handle_request.side_effect = ( + [ + httpcore.ConnectionNotAvailable(), + AssertionError("unavailable connection was reused"), + ] + if stale + else None + ) + if not stale: + connection.handle_request.return_value = httpcore.Response( + 200, content=b"ok" + ) + return connection + + +def test_sync_connection_not_available_is_not_reassigned(monkeypatch): + stale = make_connection(stale=True) + ready = make_connection() + with httpcore.ConnectionPool(max_connections=1) as pool: + pool._connections.append(stale) + monkeypatch.setattr(pool, "create_connection", lambda origin: ready) + response = pool.request("GET", "http://example.com/") + assert response.content == b"ok" + assert stale.handle_request.call_count == 1 + assert stale not in pool.connections + stale.close.assert_called_once() + + +@pytest.mark.anyio +async def test_async_connection_not_available_is_not_reassigned(monkeypatch): + stale = make_connection(stale=True, asynchronous=True) + ready = make_connection(asynchronous=True) + async with httpcore.AsyncConnectionPool(max_connections=1) as pool: + pool._connections.append(stale) + monkeypatch.setattr(pool, "create_connection", lambda origin: ready) + response = await pool.request("GET", "http://example.com/") + assert response.content == b"ok" + assert stale.handle_async_request.await_count == 1 + assert stale not in pool.connections + stale.aclose.assert_awaited_once() + + +def test_sync_unavailable_connection_does_not_block_pool_capacity(monkeypatch): + stale = make_connection(stale=True) + stale.is_available.return_value = False + ready = make_connection() + connections = iter([stale, ready]) + with httpcore.ConnectionPool(max_connections=1) as pool: + monkeypatch.setattr(pool, "create_connection", lambda origin: next(connections)) + response = pool.request( + "GET", "http://example.com/", extensions={"timeout": {"pool": 0.01}} + ) + assert response.content == b"ok" + assert stale not in pool.connections + stale.close.assert_called_once() + + +@pytest.mark.anyio +async def test_async_unavailable_connection_does_not_block_pool_capacity(monkeypatch): + stale = make_connection(stale=True, asynchronous=True) + stale.is_available.return_value = False + ready = make_connection(asynchronous=True) + connections = iter([stale, ready]) + async with httpcore.AsyncConnectionPool(max_connections=1) as pool: + monkeypatch.setattr(pool, "create_connection", lambda origin: next(connections)) + response = await pool.request( + "GET", "http://example.com/", extensions={"timeout": {"pool": 0.01}} + ) + assert response.content == b"ok" + assert stale not in pool.connections + stale.aclose.assert_awaited_once() + + +def test_sync_rejected_multiplexed_connection_closes_after_inflight_stream(monkeypatch): + from concurrent.futures import ThreadPoolExecutor + from threading import Event + + rejected = Event() + stale = make_connection(stale=True) + ready = make_connection() + calls = 0 + + def handle(request): + nonlocal calls + calls += 1 + if calls == 1: + return httpcore.Response(200, content=b"first") + rejected.set() + raise httpcore.ConnectionNotAvailable() + + stale.handle_request.side_effect = handle + connections = iter([stale, ready]) + with httpcore.ConnectionPool(max_connections=1) as pool: + monkeypatch.setattr(pool, "create_connection", lambda origin: next(connections)) + first = pool.handle_request(httpcore.Request("GET", "http://example.com/")) + with ThreadPoolExecutor(max_workers=1) as executor: + future = executor.submit( + pool.request, + "GET", + "http://example.com/", + extensions={"timeout": {"pool": 1}}, + ) + assert rejected.wait(1) + assert stale.close.call_count == 0 + first.close() + assert future.result(timeout=2).content == b"ok" + stale.close.assert_called_once() + + +@pytest.mark.anyio +async def test_async_rejected_multiplexed_connection_closes_after_inflight_stream( + monkeypatch, +): + import anyio + + rejected = anyio.Event() + stale = make_connection(stale=True, asynchronous=True) + ready = make_connection(asynchronous=True) + calls = 0 + + async def handle(request): + nonlocal calls + calls += 1 + if calls == 1: + return httpcore.Response(200, content=b"first") + rejected.set() + raise httpcore.ConnectionNotAvailable() + + stale.handle_async_request.side_effect = handle + connections = iter([stale, ready]) + results = [] + async with httpcore.AsyncConnectionPool(max_connections=1) as pool: + monkeypatch.setattr(pool, "create_connection", lambda origin: next(connections)) + first = await pool.handle_async_request( + httpcore.Request("GET", "http://example.com/") + ) + + async def send_second(): + response = await pool.request( + "GET", "http://example.com/", extensions={"timeout": {"pool": 1}} + ) + results.append(response.content) + + async with anyio.create_task_group() as group: + group.start_soon(send_second) + with anyio.fail_after(1): + await rejected.wait() + assert stale.aclose.await_count == 0 + await first.aclose() + + assert results == [b"ok"] + stale.aclose.assert_awaited_once()