Skip to content

Commit

Permalink
Merge pull request #291 from vincentbesanceney/fix_connleak
Browse files Browse the repository at this point in the history
Ensure connections are properly closed on objects destruction.
  • Loading branch information
shazow committed Dec 3, 2013
2 parents 929f158 + 1922dda commit 7845233
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions urllib3/_collections.py
Expand Up @@ -51,6 +51,9 @@ def __init__(self, maxsize=10, dispose_func=None):
self._container = self.ContainerCls()
self.lock = RLock()

def __del__(self):
self.clear()

def __getitem__(self, key):
# Re-insert the item, moving it to the end of the eviction line.
with self.lock:
Expand Down
3 changes: 3 additions & 0 deletions urllib3/connectionpool.py
Expand Up @@ -167,6 +167,9 @@ def __init__(self, host, port=None, strict=False,
self.num_connections = 0
self.num_requests = 0

def __del__(self):
self.close()

def _new_conn(self):
"""
Return a fresh :class:`httplib.HTTPConnection`.
Expand Down
3 changes: 3 additions & 0 deletions urllib3/poolmanager.py
Expand Up @@ -68,6 +68,9 @@ def __init__(self, num_pools=10, headers=None, **connection_pool_kw):
self.pools = RecentlyUsedContainer(num_pools,
dispose_func=lambda p: p.close())

def __del__(self):
self.clear()

def _new_pool(self, scheme, host, port):
"""
Create a new :class:`ConnectionPool` based on host, port and scheme.
Expand Down

0 comments on commit 7845233

Please sign in to comment.