From b1ccb277dad54287d4e9bcde9113a1bf429eea81 Mon Sep 17 00:00:00 2001 From: Adrian Chaves Date: Fri, 19 Jun 2026 12:40:20 +0200 Subject: [PATCH] Make _schedule_coro() behavior the same in asyncio and non-asyncio --- scrapy/utils/defer.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scrapy/utils/defer.py b/scrapy/utils/defer.py index 29a34d4ef..699b51505 100644 --- a/scrapy/utils/defer.py +++ b/scrapy/utils/defer.py @@ -531,9 +531,14 @@ def _schedule_coro(coro: Coroutine[Any, Any, Any]) -> None: alternative is calling :func:`scrapy.utils.defer.deferred_from_coro`, keeping the result, and adding proper exception handling (e.g. errbacks) to it. + + In both asyncio and non-asyncio modes the coroutine starts in the next + event-loop iteration, never in the current call stack. """ if not is_asyncio_available(): - Deferred.fromCoroutine(coro) + from twisted.internet import reactor + + reactor.callLater(0, Deferred.fromCoroutine, coro) return loop = asyncio.get_event_loop() loop.create_task(coro) # noqa: RUF006