-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-140691: urllib.request: Close FTP control socket if data socket can't connect #140835
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
Open
encukou
wants to merge
11
commits into
python:main
Choose a base branch
from
encukou:ftp-warnings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−19
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd3c98d
Add a test for unclosed socket
encukou b0b636a
Close FTP control connection if transfer connection fails
encukou 97c2336
Remove closed connections from cache
encukou 3cbf28b
Add blurb
encukou 20703c4
Add the walltime resource
encukou 5ee397e
Merge in the main branch
encukou 57634d3
Close non-keepalive connections when discarding them
encukou d329345
Show WarningMessage details in __repr__
encukou 9ca07e8
Investigating: turn warnings to errors to get tracebacks
encukou cd9d5f8
import warnings
encukou c477938
Don't retry -- network errors skip the test, anyway
encukou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| import contextlib | ||
| import errno | ||
| import unittest | ||
| from unittest import mock | ||
| import warnings | ||
| from test import support | ||
| from test.support import os_helper | ||
| from test.support import socket_helper | ||
| from test.support import ResourceDenied | ||
| from test.support.warnings_helper import check_no_resource_warning | ||
|
|
||
| import os | ||
| import socket | ||
|
|
@@ -143,6 +147,45 @@ def test_ftp(self): | |
| ] | ||
| self._test_urls(urls, self._extra_handlers()) | ||
|
|
||
| @support.requires_resource('walltime') | ||
| def test_ftp_no_leak(self): | ||
| # gh-140691: When the data connection (but not control connection) | ||
| # cannot be made established, we shouldn't leave an open socket object. | ||
|
|
||
| class MockError(OSError): | ||
cmaloney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass | ||
|
|
||
| orig_create_connection = socket.create_connection | ||
| def patched_create_connection(address, *args, **kwargs): | ||
| """Simulate REJECTing connections to ports other than 21""" | ||
| host, port = address | ||
| if port != 21: | ||
| raise MockError() | ||
| return orig_create_connection(address, *args, **kwargs) | ||
|
|
||
| url = 'ftp://www.pythontest.net/README' | ||
| entry = url, None, urllib.error.URLError | ||
| no_cache_handlers = [urllib.request.FTPHandler()] | ||
| cache_handlers = self._extra_handlers() | ||
| with mock.patch('socket.create_connection', patched_create_connection): | ||
| with check_no_resource_warning(self): | ||
| # Try without CacheFTPHandler | ||
| warnings.filterwarnings('error', category=ResourceWarning) | ||
| self._test_urls([entry], handlers=no_cache_handlers, | ||
| retry=False) | ||
| with check_no_resource_warning(self): | ||
| # Try with CacheFTPHandler (uncached) | ||
| warnings.filterwarnings('error', category=ResourceWarning) | ||
| self._test_urls([entry], cache_handlers, retry=False) | ||
| with check_no_resource_warning(self): | ||
| # Try with CacheFTPHandler (cached) | ||
| warnings.filterwarnings('error', category=ResourceWarning) | ||
| self._test_urls([entry], cache_handlers, retry=False) | ||
| # Try without the mock: the handler should not use a closed connection | ||
| with check_no_resource_warning(self): | ||
| warnings.filterwarnings('error', category=ResourceWarning) | ||
| self._test_urls([url], cache_handlers, retry=False) | ||
|
|
||
| def test_file(self): | ||
| TESTFN = os_helper.TESTFN | ||
| f = open(TESTFN, 'w') | ||
|
|
@@ -234,18 +277,16 @@ def _test_urls(self, urls, handlers, retry=True): | |
| else: | ||
| req = expected_err = None | ||
|
|
||
| if expected_err: | ||
| context = self.assertRaises(expected_err) | ||
| else: | ||
| context = contextlib.nullcontext() | ||
|
|
||
| with socket_helper.transient_internet(url): | ||
| try: | ||
| f = None | ||
| with context: | ||
| f = urlopen(url, req, support.INTERNET_TIMEOUT) | ||
| # urllib.error.URLError is a subclass of OSError | ||
| except OSError as err: | ||
| if expected_err: | ||
| msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % | ||
| (expected_err, url, req, type(err), err)) | ||
| self.assertIsInstance(err, expected_err, msg) | ||
| else: | ||
| raise | ||
|
Comment on lines
-242
to
-247
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (The new |
||
| else: | ||
| if f is not None: | ||
| try: | ||
| with time_out, \ | ||
| socket_peer_reset, \ | ||
|
|
||
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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2025-10-31-15-06-26.gh-issue-140691.JzHGtg.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,3 @@ | ||
| In :mod:`urllib.request`, when opening a FTP URL fails because a data | ||
| connection cannot be made, the control connection's socket is now closed to | ||
| avoid a :exc:`ResourceWarning`. |
Oops, something went wrong.
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.