fix: improve FTPDownloadHandler connection cleanup

Two improvements to the FTP connection cleanup in download_request():

1. Store the FTPClient on self.client so that callers (and test fixtures)
   can access it for cleanup after the download completes. The existing
   dh fixture teardown already checks for dh.client to call
   loseConnection() as a safety net.

2. Replace assert client.transport with a safe if-check before calling
   loseConnection(). The assert could crash if the transport is None in
   edge cases (e.g. connection lost during authentication).

Fixes scrapy/scrapy#7602

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
C1-BA-B1-F3 2026-06-26 03:45:34 +08:00
parent 8f01ef73d2
commit 44089d6bef
1 changed files with 3 additions and 2 deletions

View File

@ -108,6 +108,7 @@ class FTPDownloadHandler(BaseDownloadHandler):
client: FTPClient = await maybe_deferred_to_future(
creator.connectTCP(parsed_url.hostname, parsed_url.port or 21)
)
self.client = client
filepath = unquote(parsed_url.path)
protocol = ReceivedDataProtocol(request.meta.get("ftp_local_filename"))
try:
@ -121,8 +122,8 @@ class FTPDownloadHandler(BaseDownloadHandler):
raise
finally:
protocol.close()
assert client.transport
client.transport.loseConnection()
if 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)