Fix FTPDownloadHandler not closing FTP connection after download

download_request() created an FTPClient but never closed the control
connection. Call client.transport.loseConnection() in a finally block so
the TCP connection is always torn down after the transfer completes or
fails.

Also, the except CommandFailed path never called protocol.close(), which
could leave an open file handle for local-filename downloads. Add
protocol.close() at the top of the except block.

Fixes #7602

Co-authored-by: Claude
This commit is contained in:
Sriniketh24 2026-06-25 15:55:50 +05:30
parent dd4549e6f9
commit 7bfc2e95e9
1 changed files with 4 additions and 1 deletions

View File

@ -119,7 +119,10 @@ 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()
assert client.transport
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)