]> granicus.if.org Git - python/commitdiff
Issue #12692: Fix resource leak in urllib.request.
authorNadeem Vawda <nadeem.vawda@gmail.com>
Sun, 21 Oct 2012 15:37:43 +0000 (17:37 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Sun, 21 Oct 2012 15:37:43 +0000 (17:37 +0200)
Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/NEWS

index 7ae155331ee8152075193e4754c947a7e554c74e..00ee66987422af240d91e0a93db7c65cb261f123 100644 (file)
@@ -302,6 +302,7 @@ class MockHTTPClass:
         self.req_headers = []
         self.data = None
         self.raise_on_endheaders = False
+        self.sock = None
         self._tunnel_headers = {}
 
     def __call__(self, host, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
index 88f3ce1a26a7ba0441519dbcc3db4744af921865..250d89efe05b07cd20b057bc9f494b6a3f5c6205 100644 (file)
@@ -1255,6 +1255,12 @@ class AbstractHTTPHandler(BaseHandler):
             raise URLError(err)
         else:
             r = h.getresponse()
+            # If the server does not send us a 'Connection: close' header,
+            # HTTPConnection assumes the socket should be left open. Manually
+            # mark the socket to be closed when this response object goes away.
+            if h.sock:
+                h.sock.close()
+                h.sock = None
 
         r.url = req.get_full_url()
         # This line replaces the .msg attribute of the HTTPResponse
index 4af7d0a059124a3f258c0566170d51fc2188db8b..fae3f135dc2eb76fb26b8b697d7ec7c5a936f3c2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -59,6 +59,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12692: Fix resource leak in urllib.request when talking to an HTTP
+  server that does not include a "Connection: close" header in its responses.
+
 - Issue #12034: Fix bogus caching of result in check_GetFinalPathNameByHandle.
   Patch by Atsuo Ishimoto.