]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Release max_connections for keepalive connections when closing the connection pool...
authorFlorimond Manca <florimond.manca@gmail.com>
Mon, 6 Jan 2020 11:13:06 +0000 (12:13 +0100)
committerTom Christie <tom@tomchristie.com>
Mon, 6 Jan 2020 11:13:06 +0000 (11:13 +0000)
httpx/dispatch/connection_pool.py
tests/dispatch/test_connection_pools.py

index 545e20452147a0a0fd2a494cfa3ab63ecfa064a3..3e09b110175ec56102d470ecc53c072054c03b9c 100644 (file)
@@ -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]:
index 5e1fce3e0748fcb2aa88fc5223022d05079ccb23..0ec12a7c6cc855b1096537c0d649724beeb9f508 100644 (file)
@@ -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