From 2029a8a93113dbcd0fab98f987933794d6ac3094 Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Wed, 13 Apr 2016 16:59:55 +0100 Subject: [PATCH] Clear any pooled proxy connections As well as clearing any pooled direct connections, iterate over any ProxyManager objects and clear any pooled proxy connections there as well. --- requests/adapters.py | 6 ++++-- requirements.txt | 1 + tests/test_requests.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index fe9f5338..db62c09c 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -264,10 +264,12 @@ class HTTPAdapter(BaseAdapter): def close(self): """Disposes of any internal state. - Currently, this just closes the PoolManager, which closes pooled - connections. + Currently, this closes the PoolManager and any active ProxyManager, + which closes any pooled connections. """ self.poolmanager.clear() + for proxy in self.proxy_manager.values(): + proxy.clear() def request_url(self, request, proxies): """Obtain the url to use when making the final request. diff --git a/requirements.txt b/requirements.txt index 3d29de0c..1305d3f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ Pygments==2.1.1 pytest==2.8.7 pytest-cov==2.2.1 pytest-httpbin==0.2.0 +pytest-mock==0.11.0 pytz==2015.7 six==1.10.0 snowballstemmer==1.2.1 diff --git a/tests/test_requests.py b/tests/test_requests.py index 04a27a44..ebef0ba3 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1188,6 +1188,17 @@ class TestRequests: next(r.iter_lines()) assert len(list(r.iter_lines())) == 3 + def test_session_close_proxy_clear(self, mocker): + proxies = { + 'one': mocker.Mock(), + 'two': mocker.Mock(), + } + session = requests.Session() + mocker.patch.dict(session.adapters['http://'].proxy_manager, proxies) + session.close() + proxies['one'].clear.assert_called_once_with() + proxies['two'].clear.assert_called_once_with() + class TestCaseInsensitiveDict: