Don't try to close ExecutionEngine.downloader when it doesn't exist. (#6867)

This commit is contained in:
Andrey Rakhmatullin 2025-06-06 16:02:15 +05:00
parent 405d9bc8a2
commit d329eedfef
2 changed files with 16 additions and 1 deletions

View File

@ -170,7 +170,8 @@ class ExecutionEngine:
return self.close_spider(
self.spider, reason="shutdown"
) # will also close downloader
self.downloader.close()
if hasattr(self, "downloader"):
self.downloader.close()
return succeed(None)
def pause(self) -> None:

View File

@ -22,6 +22,7 @@ from unittest.mock import Mock
from urllib.parse import urlparse
import attr
import pytest
from itemadapter import ItemAdapter
from pydispatch import dispatcher
from twisted.internet import defer, reactor
@ -431,6 +432,19 @@ class TestEngine(TestEngineBase):
e = ExecutionEngine(get_crawler(MySpider), lambda _: None)
yield e.close()
def test_close_without_downloader(self):
class CustomException(Exception):
pass
class BadDownloader:
def __init__(self, crawler):
raise CustomException
with pytest.raises(CustomException):
ExecutionEngine(
get_crawler(MySpider, {"DOWNLOADER": BadDownloader}), lambda _: None
)
@defer.inlineCallbacks
def test_start_already_running_exception(self):
e = ExecutionEngine(get_crawler(MySpider), lambda _: None)