fix(arun_many): implement total_timeout and global dispatcher watchdog to prevent hangs (#1914)#1923
Open
DevAbdullah90 wants to merge 1 commit intounclecode:mainfrom
Open
Conversation
…g to prevent hangs (Issue unclecode#1914)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Issue #1914 reported that
arun_many()could hang indefinitely despite having apage_timeoutset. Investigation revealed thatpage_timeoutonly covers the initial navigation (page.goto), but does not protect against hangs in:js_code).Because the dispatchers were simply awaiting the
arun()task, any hang within a single URL would block a concurrency slot indefinitely and could eventually stall the entire batch.Solution
This PR introduces a global watchdog at the dispatcher level and a new configuration parameter to provide strict timeout guarantees for every URL in a batch.
total_timeout(ms) toCrawlerRunConfig. This defines the maximum allowed time for the entire crawl operation (navigation + JS + extraction).MemoryAdaptiveDispatcherandSemaphoreDispatcherto wrap thearuncall in anasyncio.wait_forwatchdog.total_timeoutis not explicitly provided, the watchdog defaults topage_timeout + 30 seconds. This ensures that even legacy configurations benefit from this safety margin.asyncio.TimeoutErrorat the dispatcher level.TIMEOUTstatus withself.crawler.logger.error_status.CrawlResultwith a descriptive message:"Crawl task exceeded total timeout of X seconds".Changes
crawl4ai/async_configs.py: Addedtotal_timeoutparameter.crawl4ai/async_dispatcher.py: Implementedasyncio.wait_forin both dispatchers.tests/unit/test_dispatcher_timeout.py: New unit tests for explicit and fallback timeout enforcement.pyproject.toml: Movedpytestandpytest-asyncioto thedevdependency group.Verification Results
while(true){}hang injs_code.total_timeoutand returned the summary correctly.Fixes #1914
uv run python -m pytest tests/unit/test_dispatcher_timeout.py # 2 passed in 6.65s