[Python-checkins] cpython: Issue #12692: Fix resource leak in urllib.request.

nadeem.vawda python-checkins at python.org
Sun Oct 21 17:44:13 CEST 2012


http://hg.python.org/cpython/rev/92656b5df2f2
changeset:   79876:92656b5df2f2
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Sun Oct 21 17:37:43 2012 +0200
summary:
  Issue #12692: Fix resource leak in urllib.request.

files:
  Lib/test/test_urllib2.py |  1 +
  Lib/urllib/request.py    |  6 ++++++
  Misc/NEWS                |  3 +++
  3 files changed, 10 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -302,6 +302,7 @@
         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):
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1255,6 +1255,12 @@
             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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -59,6 +59,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list