From 57afd5937c37862cfaa8d06c43e51eb06cb907e1 Mon Sep 17 00:00:00 2001 From: hodbn Date: Tue, 28 Apr 2020 05:57:16 -0700 Subject: [PATCH] [1.25] Don't insert 'None' into ConnectionPool if it was empty --- src/urllib3/connectionpool.py | 8 +++++--- test/test_connectionpool.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/urllib3/connectionpool.py b/src/urllib3/connectionpool.py index 5f044dbd90..174fe6c2e1 100644 --- a/src/urllib3/connectionpool.py +++ b/src/urllib3/connectionpool.py @@ -698,9 +698,11 @@ def urlopen( # 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, diff --git a/test/test_connectionpool.py b/test/test_connectionpool.py index 3cd215304f..615fdfc0c1 100644 --- a/test/test_connectionpool.py +++ b/test/test_connectionpool.py @@ -2,6 +2,7 @@ import ssl import pytest +from mock import Mock from urllib3.connectionpool import ( connection_from_url, @@ -279,7 +280,6 @@ def _test(exception, expect, reason=None): # 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 @@ def _test(exception, expect, reason=None): 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):