fix(ftp): close FTP client connection and protocol on all code paths

FTPDownloadHandler.download_request() creates an FTPClient via
ClientCreator.connectTCP() but never calls loseConnection(), leaking
the TCP connection. Additionally, the ReceivedDataProtocol is not
closed on the CommandFailed error path, leaking file handles or
memory buffers.

Move protocol.close() into a finally block and add
client.loseConnection() there so both resources are always cleaned
up regardless of success or failure.

Fixes #7602
This commit is contained in:
MUHAMED FAZAL PS 2026-06-23 21:38:39 +05:30
parent b78ab3d6c8
commit c9ab27e281
1 changed files with 3 additions and 1 deletions

View File

@ -119,7 +119,9 @@ class FTPDownloadHandler(BaseDownloadHandler):
httpcode = self.CODE_MAPPING.get(ftpcode, self.CODE_MAPPING["default"])
return Response(url=request.url, status=httpcode, body=message.encode())
raise
protocol.close()
finally:
protocol.close()
client.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)