From: Florimond Manca Date: Mon, 6 Jan 2020 11:13:06 +0000 (+0100) Subject: Release max_connections for keepalive connections when closing the connection pool... X-Git-Tag: 0.11.0~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd57b650a8ed84849901a3a4c54579a9a2c948b2;p=thirdparty%2Fhttpx.git Release max_connections for keepalive connections when closing the connection pool (#721) --- diff --git a/httpx/dispatch/connection_pool.py b/httpx/dispatch/connection_pool.py index 545e2045..3e09b110 100644 --- a/httpx/dispatch/connection_pool.py +++ b/httpx/dispatch/connection_pool.py @@ -206,6 +206,7 @@ class ConnectionPool(Dispatcher): connections = list(self.keepalive_connections) self.keepalive_connections.clear() for connection in connections: + self.max_connections.release() await connection.close() def pop_connection(self, origin: Origin) -> typing.Optional[HTTPConnection]: diff --git a/tests/dispatch/test_connection_pools.py b/tests/dispatch/test_connection_pools.py index 5e1fce3e..0ec12a7c 100644 --- a/tests/dispatch/test_connection_pools.py +++ b/tests/dispatch/test_connection_pools.py @@ -221,3 +221,28 @@ async def test_connection_closed_free_semaphore_on_acquire(server, restart): response = await http.request("GET", server.url) assert response.status_code == 200 + + +@pytest.mark.usefixtures("async_environment") +async def test_connection_pool_closed_close_keepalive_and_free_semaphore(server): + """ + Closing the connection pool should close remaining keepalive connections and + release the max_connections semaphore. + """ + http = ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) + + async with http: + response = await http.request("GET", server.url) + await response.aread() + assert response.status_code == 200 + assert len(http.keepalive_connections) == 1 + + assert len(http.keepalive_connections) == 0 + + # Perform a second round of requests to make sure the max_connections semaphore + # was released properly. + + async with http: + response = await http.request("GET", server.url) + await response.aread() + assert response.status_code == 200