FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-140691: Close FTP connection on error in urllib.request by codenamenam · Pull Request #140694 · python/cpython · GitHub

Repository navigation

gh-140691: Close FTP connection on error in urllib.request - #140694

Closed
codenamenam wants to merge 0 commit into
python:mainfrom
codenamenam:fix-issue-140691
Closed

codenamenam wants to merge 0 commit into
python:mainfrom
codenamenam:fix-issue-140691

Conversation

codenamenam commented Oct 28, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

When FTP request fails after the control connection has been established (e.g., timeout during file retrieval), the connection
was not being closed, causing a ResourceWarning.

This fix ensures that the FTP connection is properly closed in the exception handler of ftp_open().

The new test uses mocking to simulate FTP timeout errors and verifies that:

  1. The connection's close() method is called
  2. No ResourceWarning is raised

Comment thread Lib/urllib/request.py
Comment thread Lib/test/test_urllib2.py Outdated
Comment thread Lib/test/test_urllib2.py Outdated
mock_fw = mock.MagicMock()
mock_fw.retrfile.side_effect = ftplib.error_temp("Connection timed out")

with mock.patch.object(urllib.request.FTPHandler, 'connect_ftp',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We want to test what happens when fw.retrfile fails, so need to mock urllib.request.ftpwrapper.

The mock here is mocking out a lot more than necessary, should only need to mock the class urllib.request.ftpwrapper method retrfile to raise an exception

Comment thread Lib/test/test_urllib2.py Outdated

handler = urllib.request.FTPHandler()

with warnings.catch_warnings(record=True) as warning_list:

cmaloney Oct 28, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is part of the testing needed, will also need to use test.support to check that there are no "unraisable exceptions" (which is what this one ends up being when it isn't correctly cleaned up). Look at other tests for examples

Comment thread Lib/test/test_urllib2.py Outdated
close_called = []
original_close = urllib.request.ftpwrapper.close

def tracked_close(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The mock library tools that can use to check counts called: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_called_once

Comment thread Lib/urllib/request.py Outdated
Comment on lines +1558 to +1559
if fw is not None:
fw.close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This will close fw, but leave the closed connection in CacheFTPHandler's cache.

encukou commented Oct 28, 2025

Copy link
Copy Markdown
Member

Thanks for tackling this! I left some notes in the issue. I'll let you take it from here but let me know if I should look into this more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL