Fix FTPDownloadHandler connection leak

The FTPDownloadHandler.download_request() method creates FTPClient
instances but never closes them, leading to resource leaks. Additionally,
the ReceivedDataProtocol instance is not closed in the error path.

This fix:
1. Closes the FTP client connection after successful file retrieval
2. Closes both the protocol and client connection in the error path
3. Uses client.transport.loseConnection() to properly close the connection

Fixes #7602

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1795771535y-cell 2026-06-18 19:58:51 +08:00
parent af30cfea12
commit 0d4dfae494
No known key found for this signature in database
1 changed files with 5 additions and 0 deletions

View File

@ -114,12 +114,17 @@ class FTPDownloadHandler(BaseDownloadHandler):
await maybe_deferred_to_future(client.retrieveFile(filepath, protocol))
except CommandFailed as e:
message = str(e)
# Close protocol and client on error to prevent resource leaks
protocol.close()
client.transport.loseConnection()
if m := _CODE_RE.search(message):
ftpcode = m.group()
httpcode = self.CODE_MAPPING.get(ftpcode, self.CODE_MAPPING["default"])
return Response(url=request.url, status=httpcode, body=message.encode())
raise
protocol.close()
# Close the FTP client connection to prevent resource leaks
client.transport.loseConnection()
headers = {"local filename": protocol.filename or b"", "size": protocol.size}
body = protocol.filename or protocol.body.read()
respcls = responsetypes.from_args(url=request.url, body=body)