mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
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>
This commit is contained in:
parent
690df4c16c
commit
9de0cf20fa
3 changed files with 43 additions and 15 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue