Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
GH-103472: close response in HTTPConnection._tunnel#103473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gpshead merged 8 commits into python:main from graingert:close-response-in-http-client-tunnelMay 2, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
86952ce GH-103472: close response in HTTPConnection._tunnel
graingert 6508762 📜🤖 Added by blurb_it.
blurb-it[bot] f88793c Update Misc/NEWS.d/next/Library/2023-04-12-13-04-16.gh-issue-103472.C…
graingert 959db96 Merge branch 'main' into close-response-in-http-client-tunnel
graingert 5500734 Merge branch 'main' into close-response-in-http-client-tunnel
graingert 2aa460c test that HTTPConnection._tunnel closes the response on exceptions
graingert c2171c7 minor wording and ReST update to NEWS
gpshead bd079b1 word wrap news.
gpshead File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -941,23 +941,26 @@ def _tunnel(self): | ||
| del headers | ||
| response = self.response_class(self.sock, method=self._method) | ||
| (version, code, message) = response._read_status() | ||
| try: | ||
| (version, code, message) = response._read_status() | ||
| if code != http.HTTPStatus.OK: | ||
| self.close() | ||
| raise OSError(f"Tunnel connection failed:{code}{message.strip()}") | ||
| while True: | ||
| line = response.fp.readline(_MAXLINE + 1) | ||
| if len(line) > _MAXLINE: | ||
| raise LineTooLong("header line") | ||
| if not line: | ||
| # for sites which EOF without sending a trailer | ||
| break | ||
| if line in (b'\r\n', b'\n', b''): | ||
| break | ||
| if code != http.HTTPStatus.OK: | ||
| self.close() | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| raise OSError(f"Tunnel connection failed:{code}{message.strip()}") | ||
| while True: | ||
| line = response.fp.readline(_MAXLINE + 1) | ||
| if len(line) > _MAXLINE: | ||
| raise LineTooLong("header line") | ||
| if not line: | ||
| # for sites which EOF without sending a trailer | ||
| break | ||
| if line in (b'\r\n', b'\n', b''): | ||
| break | ||
| if self.debuglevel > 0: | ||
| print('header:', line.decode()) | ||
| if self.debuglevel > 0: | ||
| print('header:', line.decode()) | ||
| finally: | ||
| response.close() | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| def connect(self): | ||
| """Connect to the host and port specified in __init__.""" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2390,6 +2390,29 @@ def test_tunnel_debuglog(self): | ||
| 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') | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 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) | ||
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2023-04-12-13-04-16.gh-issue-103472.C6bOHv.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Avoid a potential :exc:`ResourceWarning` in :class:`http.client.HTTPConnection` | ||
| by closing the proxy / tunnel's CONNECT response explicitly. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.