From: Ordanis Sanchez Date: Sun, 18 Aug 2019 19:44:24 +0000 (-0400) Subject: Release semaphore if connection is dropped (#230) X-Git-Tag: 0.7.2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a960a2a8fd273960993551f88617f22bfeb85035;p=thirdparty%2Fhttpx.git Release semaphore if connection is dropped (#230) --- diff --git a/httpx/dispatch/connection_pool.py b/httpx/dispatch/connection_pool.py index 374c76a3..3090a9a5 100644 --- a/httpx/dispatch/connection_pool.py +++ b/httpx/dispatch/connection_pool.py @@ -123,6 +123,7 @@ class ConnectionPool(AsyncDispatcher): connection = self.keepalive_connections.pop_by_origin(origin) if connection is not None and connection.is_connection_dropped(): + self.max_connections.release() connection = None if connection is None: diff --git a/tests/dispatch/test_connection_pools.py b/tests/dispatch/test_connection_pools.py index 76572f1e..1bd564b0 100644 --- a/tests/dispatch/test_connection_pools.py +++ b/tests/dispatch/test_connection_pools.py @@ -171,3 +171,21 @@ async def test_keepalive_http2_connection_closed_by_server_is_reestablished(serv await response.read() assert len(http.active_connections) == 0 assert len(http.keepalive_connections) == 1 + + +@pytest.mark.asyncio +async def test_connection_closed_free_semaphore_on_acquire(server): + """ + Verify that max_connections semaphore is released + properly on a disconnected connection. + """ + async with httpx.ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http: + response = await http.request("GET", "http://127.0.0.1:8000/") + await response.read() + + # Close the connection so we're forced to recycle it + await server.shutdown() + await server.startup() + + response = await http.request("GET", "http://127.0.0.1:8000/") + assert response.status_code == 200