Repository navigation
fix(adaptive-crawler): Skip transient static failures in rendering type detection - #2438
chalakbilla wants to merge 5 commits into
Conversation
…ient static failure
… add integration tests
There was a problem hiding this comment.
The PR description needs a fix. It has broken control characters where backticks were (\tests/..., ^Gsyncio), it mentions a _TRANSIENT_STATIC_ERRORS constant that is not in the code, and the issue link should have a colon (Closes: #2096).
✍️ Drafted by Claude Code
| exception = exception.wrapped_exception | ||
|
|
||
| transient_errors: tuple[type[Exception], ...] = ( | ||
| SessionError, |
There was a problem hiding this comment.
Important: In the static pipeline, SessionError means the site blocked the HTTP client (a blocked status code or a block detected in the content). Line 534 also skips every ContextPipelineInitializationError, including a 403 wrapped as HttpClientStatusCodeError. When the browser run then succeeds, that is a repeatable signal for client only, not a transient failure. With no stored results, DefaultRenderingTypePredictor.predict() keeps returning detection_probability_recommendation=1, so on a site that blocks plain HTTP every request runs both crawls. A local repro with 6 URLs, a static run that always gets 403, and a browser run that succeeds: this PR stores 0 results and detection stays at 1; master stores 4 and detection drops to 0.1. Could you treat only fetch failures as inconclusive (connection errors, timeouts, ProxyError, RequestThrottledError) and keep blocks and status code errors as client only?
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Fixed. Blocks (SessionError) and status code errors now count as client only. With your repro, 4 results are stored and detection drops to 0.1.
The rest of ContextPipelineInitializationError is still inconclusive, because the HTTP clients don't raise builtin ConnectionError. A reset shows up as impit.ConnectError or httpx2.ConnectError, so listing exception types would bring the original bug back. If the parser or a pre-nav hook fails on every static page, detection never drops, but that's broken enough to show up in the logs anyway.
| transient_errors: tuple[type[Exception], ...] = ( | ||
| SessionError, | ||
| RequestThrottledError, | ||
| TimeoutError, |
There was a problem hiding this comment.
Important: On Python 3.10, asyncio.TimeoutError is not a subclass of TimeoutError. The handler timeout raised by wait_for in _crawl_one is still stored as client only there, and test_transient_failure_skips_rendering_type_detection[asyncio-timeout-error] fails on 3.10 (checked locally; 3.10 is in the CI matrix). Please add asyncio.TimeoutError to the tuple.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Done. The new test passes on 3.10.
| TimeoutError, | ||
| ConnectionError, | ||
| ) | ||
| if 'UserHandlerTimeoutError' in globals(): |
There was a problem hiding this comment.
Suggestion: 'UserHandlerTimeoutError' in globals() is always true, because the class is imported at the top of the module. Please drop the check and define the tuple once as a module-level constant that includes UserHandlerTimeoutError.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Gone, together with UserHandlerTimeoutError.
| return True | ||
|
|
||
| if isinstance(exception, RequestHandlerError): | ||
| exception = exception.wrapped_exception |
There was a problem hiding this comment.
Question: The docstring says that an exception raised by the request handler itself is evidence that the page needs a browser. After this unwrap, a UserHandlerTimeoutError, ConnectionError or RequestThrottledError raised in the handler counts as inconclusive. Which behavior is intended? The docstring and the code should match.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
RequestHandlerError isn't unwrapped anymore, so anything the handler raises counts as client only, as the docstring says. The only handler-side case still inconclusive is the timeout of the whole static run from _crawl_one.
| crawler, predictor = _make_crawler_and_predictor() | ||
| pw_result = RequestHandlerRunResult(key_value_store_getter=AsyncMock()) | ||
|
|
||
| async def mock_crawl_one( |
There was a problem hiding this comment.
Nit: The four unit tests repeat this mock_crawl_one stub, and the new integration tests already cover the same cases. Could you merge them into one parametrized test that patches _crawl_one with AsyncMock(side_effect=...), or drop them?
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Dropped them. The integration tests are now one parametrized test that feeds http_client.crawl realistic outcomes: an impit.ConnectError, a timeout, a ProxyError, a 403 and a 404.
…-failure-detection # Conflicts: # src/crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawler.py
Description
When the static sub-crawler fails during rendering type detection because of a transient infrastructure error (connection error, timeout, proxy failure),
static_run.resultevaluates toNoneand the request was previously labelled as'client only'. That label sticks in the predictor and lowers the detection probability recommendation for similar URLs, routing them through the heavier browser path even though the failure was unrelated to the page.This change:
ProxyError,RequestThrottledError, andasyncio.TimeoutErroron Python 3.10) as inconclusive: the predictor is not trained (store_resultis not called) and a debug message is logged.SessionError) and HTTP status errors (such as 401/403/404) as'client only', because when the browser succeeds they are genuine evidence the page needs client-side rendering.'client only'. The only inconclusive handler-side case is the timeout of the whole static run.Issues
Testing
Added a parameterized integration test (
test_detection_run_static_fetch_failure) that feeds realistic static failures (connection error, timeout,ProxyError, 401/404 responses) and checks whetherstore_resultis called and with which label.Checklist