]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Release semaphore if connection is dropped (#230)
authorOrdanis Sanchez <ordanisanchez@gmail.com>
Sun, 18 Aug 2019 19:44:24 +0000 (15:44 -0400)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Sun, 18 Aug 2019 19:44:24 +0000 (14:44 -0500)
httpx/dispatch/connection_pool.py
tests/dispatch/test_connection_pools.py

index 374c76a384329d7e73e876c77abaafc6295b3e2c..3090a9a514dddca61b247c66a148f859bfd5acdf 100644 (file)
@@ -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:
index 76572f1e868a5767ebac6696e9c47f5b5aa5b10f..1bd564b030c7032665e9c851a4680443e8aa969c 100644 (file)
@@ -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