diff options
author | Thomas Grainger <tagrain@gmail.com> | 2023-05-02 03:59:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-02 03:59:42 (GMT) |
commit | 9de0cf20fa0485e327e57cc0864c7476da85cfad (patch) | |
tree | a6f4c32b5d1bebbebecd337b63f4c2c91bea30c2 /Lib/test/test_httplib.py | |
parent | 690df4c16ca4f0054d27a6148da9e6af809a2658 (diff) | |
download | cpython-9de0cf20fa0485e327e57cc0864c7476da85cfad.zip cpython-9de0cf20fa0485e327e57cc0864c7476da85cfad.tar.gz cpython-9de0cf20fa0485e327e57cc0864c7476da85cfad.tar.bz2 |
GH-103472: close response in HTTPConnection._tunnel (#103473)
Avoid a potential `ResourceWarning` in `http.client.HTTPConnection`
by closing the proxy / tunnel's CONNECT response explicitly.
---------
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/test/test_httplib.py')
-rw-r--r-- | Lib/test/test_httplib.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index b4f4e2b..37f77fe 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -2390,6 +2390,29 @@ class TunnelTests(TestCase): lines = output.getvalue().splitlines() self.assertIn('header: {}'.format(expected_header), lines) + def test_tunnel_leak(self): + sock = None + + def _create_connection(address, timeout=None, source_address=None): + nonlocal sock + sock = FakeSocket( + 'HTTP/1.1 404 NOT FOUND\r\n\r\n', + host=address[0], + port=address[1], + ) + return sock + + self.conn._create_connection = _create_connection + self.conn.set_tunnel('destination.com') + exc = None + try: + self.conn.request('HEAD', '/', '') + except OSError as e: + # keeping a reference to exc keeps response alive in the traceback + exc = e + self.assertIsNotNone(exc) + self.assertTrue(sock.file_closed) + if __name__ == '__main__': unittest.main(verbosity=2) |