summaryrefslogtreecommitdiffstats
path: root/Lib/urllib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-09-06 18:43:49 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-09-06 18:43:49 (GMT)
commit91453026ffab6f0b3278417dc6348f3310d70be0 (patch)
treea41533f76b143e563b01666d0c09a7e2ec6780c0 /Lib/urllib
parentd8a1447c9980be5f1d8ae806f7aecd814b1cd6f6 (diff)
parentf54c350160c16cdaf9f692e0f61ef062d4f379f4 (diff)
downloadcpython-91453026ffab6f0b3278417dc6348f3310d70be0.zip
cpython-91453026ffab6f0b3278417dc6348f3310d70be0.tar.gz
cpython-91453026ffab6f0b3278417dc6348f3310d70be0.tar.bz2
Issue #19524: Fixed resource leak in the HTTP connection when an invalid
response is received. Patch by Martin Panter.
Diffstat (limited to 'Lib/urllib')
-rw-r--r--Lib/urllib/request.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index a17c868..67c7566 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1170,18 +1170,21 @@ class AbstractHTTPHandler(BaseHandler):
h.set_tunnel(req._tunnel_host, headers=tunnel_headers)
try:
- h.request(req.get_method(), req.selector, req.data, headers)
- except OSError as err: # timeout error
- h.close()
- raise URLError(err)
- else:
+ try:
+ h.request(req.get_method(), req.selector, req.data, headers)
+ except OSError as err: # timeout error
+ raise URLError(err)
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
+ except:
+ h.close()
+ raise
+
+ # 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