diff -r 4476b7493ee4 -r 12c84e740ed6 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Sat Mar 22 20:38:11 2014 +0100 +++ b/Lib/test/test_httplib.py Sun Mar 23 03:14:32 2014 +0000 @@ -669,6 +669,22 @@ body = b'x' * conn.mss conn.request('POST', '/', body) self.assertGreater(sock.sendall_calls, 1) + + def test_error_leak(self): + """Test that the socket is not leaked if getresponse() fails""" + conn = client.HTTPConnection('example.com') + response = None + class Response(client.HTTPResponse): + def __init__(self, *pos, **kw): + nonlocal response + response = self # Avoid garbage collector closing the socket + client.HTTPResponse.__init__(self, *pos, **kw) + conn.response_class = Response + conn.sock = FakeSocket('') # Emulate server dropping connection + conn.request('GET', '/') + self.assertRaises(client.BadStatusLine, conn.getresponse) + self.assertTrue(response.fp is None or response.fp.closed, + 'Socket reader still open') class OfflineTest(TestCase): def test_responses(self):