diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-09-06 18:41:39 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-09-06 18:41:39 (GMT) |
commit | f54c350160c16cdaf9f692e0f61ef062d4f379f4 (patch) | |
tree | 2d75c60bdc81c8837372c9e61e5f9f471c5f57a5 /Lib/urllib | |
parent | 1d52096d1466ed771c22a2d97e8cae6935fcdf8e (diff) | |
download | cpython-f54c350160c16cdaf9f692e0f61ef062d4f379f4.zip cpython-f54c350160c16cdaf9f692e0f61ef062d4f379f4.tar.gz cpython-f54c350160c16cdaf9f692e0f61ef062d4f379f4.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.py | 25 |
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 |