From 80d3f1710a8305aab58f818cf26d304fb07a24b7 Mon Sep 17 00:00:00 2001 From: Battleplus <3559424769@qq.com> Date: Mon, 24 Aug 2026 13:17:40 +0800 Subject: [PATCH] fix(crawler): await inner stream aclose in arun_many (#2083) When the outer result_transformer() generator is closed via aclose(), the inner dispatcher.run_urls_stream() generator was not explicitly closed, so its cleanup (task cancellation, session release) could overlap with session teardown. Explicitly aclose() the inner generator in the finally block before releasing the session. Closes #2083 --- crawl4ai/async_webcrawler.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crawl4ai/async_webcrawler.py b/crawl4ai/async_webcrawler.py index 8216d19bc..3b5e5c579 100644 --- a/crawl4ai/async_webcrawler.py +++ b/crawl4ai/async_webcrawler.py @@ -1107,13 +1107,18 @@ async def maybe_release_session(): if stream: async def result_transformer(): + inner = dispatcher.run_urls_stream( + crawler=self, urls=urls, config=config + ) try: - async for task_result in dispatcher.run_urls_stream( - crawler=self, urls=urls, config=config - ): + async for task_result in inner: yield transform_result(task_result) finally: - # Auto-release session after streaming completes + # Ensure the inner dispatcher stream is fully closed + # before releasing the session — otherwise cleanup of + # active crawl tasks may overlap with session teardown. + # (See #2083: arun_many stream closure leaks tasks.) + await inner.aclose() await maybe_release_session() return result_transformer()