Skip to content

Conversation

@codenamenam
Copy link
Contributor

@codenamenam codenamenam commented Oct 28, 2025

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

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
Contributor

Choose a reason for hiding this comment

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

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


handler = urllib.request.FTPHandler()

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

@cmaloney cmaloney Oct 28, 2025

Choose a reason for hiding this comment

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

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

close_called = []
original_close = urllib.request.ftpwrapper.close

def tracked_close(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

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 on lines 1558 to 1559
if fw is not None:
fw.close()
Copy link
Member

Choose a reason for hiding this comment

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

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

@encukou
Copy link
Member

encukou commented Oct 28, 2025

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.

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