From 7bfc2e95e95ca062f04845a7851223cf1ce99982 Mon Sep 17 00:00:00 2001 From: Sriniketh24 Date: Thu, 25 Jun 2026 15:55:50 +0530 Subject: [PATCH] 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 --- scrapy/core/downloader/handlers/ftp.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scrapy/core/downloader/handlers/ftp.py b/scrapy/core/downloader/handlers/ftp.py index 6258067c1..9064aa53a 100644 --- a/scrapy/core/downloader/handlers/ftp.py +++ b/scrapy/core/downloader/handlers/ftp.py @@ -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)