Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
31 changes: 28 additions & 3 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
180 changes: 180 additions & 0 deletions tests/test_pool_stale_connection.py
Original file line number Diff line number Diff line change
@@ -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()