Don't insert 'None' into ConnectionPool if it was empty
MODIFY
src/urllib3/connectionpool.py
src/urllib3/connectionpool.py
@@ -698,9 +698,11 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
# Everything went great!
clean_exit = True
- except queue.Empty:
- # Timed out by queue.
- raise EmptyPoolError(self, "No pool connections are available.")
+ except EmptyPoolError:
+ # Didn't get a connection from the pool, no need to clean up
+ clean_exit = True
+ release_this_conn = False
+ raise
except (
TimeoutError,

MODIFY
test/test_connectionpool.py
test/test_connectionpool.py
@@ -2,6 +2,7 @@ from __future__ import absolute_import
import ssl
import pytest
+from mock import Mock
from urllib3.connectionpool import (
connection_from_url,
@@ -279,7 +280,6 @@ class TestConnectionPool(object):
# Make sure that all of the exceptions return the connection
# to the pool
- _test(Empty, EmptyPoolError)
_test(BaseSSLError, MaxRetryError, SSLError)
_test(CertificateError, MaxRetryError, SSLError)
@@ -292,6 +292,15 @@ class TestConnectionPool(object):
pool.request("GET", "/", retries=1, pool_timeout=SHORT_TIMEOUT)
assert pool.pool.qsize() == POOL_SIZE
+ def test_empty_does_not_put_conn(self):
+ """Do not put None back in the pool if the pool was empty"""
+
+ with HTTPConnectionPool(host="localhost", maxsize=1, block=True) as pool:
+ pool._get_conn = Mock(side_effect=EmptyPoolError(pool, "Pool is empty"))
+ pool._put_conn = Mock(side_effect=AssertionError("Unexpected _put_conn"))
+ with pytest.raises(EmptyPoolError):
+ pool.request("GET", "/")
+
def test_assert_same_host(self):
with connection_from_url("http://google.com:80") as c:
with pytest.raises(HostChangedError):

Don't insert 'None' into ConnectionPool if it was empty