Skip to content

Commit

Permalink
Clear any pooled proxy connections
Browse files Browse the repository at this point in the history
As well as clearing any pooled direct connections, iterate over any
ProxyManager objects and clear any pooled proxy connections there as well.
  • Loading branch information
bodgit committed Apr 15, 2016
1 parent 4a716e0 commit 2029a8a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions requests/adapters.py
Expand Up @@ -264,10 +264,12 @@ def get_connection(self, url, proxies=None):
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.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_requests.py
Expand Up @@ -1188,6 +1188,17 @@ def test_response_iter_lines_reentrant(self, httpbin):
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:

Expand Down

0 comments on commit 2029a8a

Please sign in to comment.