feat: support timeouts in the send_request context helper - #2174
Merged
Conversation
Expose the HTTP clients' existing per-request `timeout` support through `SendRequestFunction` so request handlers can bound how long an extra HTTP call may take. Threads the value through `context.send_request()` into `HttpClient.send_request`. Also fixes `PlaywrightHttpClient` which passed `timedelta.total_seconds()` to Playwright's `APIRequestContext.fetch`, which expects milliseconds — previously any timeout would be ~1000x too short. Adds regression tests for both the forwarding and the unit conversion. Closes apify#2138 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
Author
|
Follow-up audit found and fixed a Playwright edge case: |
vdusek
approved these changes
Aug 24, 2026
vdusek
added a commit
that referenced
this pull request
Aug 24, 2026
…ignoring it (#2187) Follow-up to #2174: a `timedelta(0)` timeout is falsy, so `if timeout else None`-style checks silently dropped it and treated it as "no timeout" instead of "expire immediately". - `ImpitHttpClient` and `HttpxHttpClient.stream()` only needed the `is not None` fix — both already enforce an explicit `0` timeout correctly on their own. - `CurlImpersonateHttpClient` needed more: `curl_cffi` (via `libcurl`) treats an explicit `timeout=0` as *no timeout*, so the same fix alone doesn't change observable behavior there. Added an explicit guard in `crawl`, `send_request`, and `stream` that raises `asyncio.TimeoutError` immediately for a non-positive timeout, verified against a real slow endpoint. - `PlaywrightHttpClient` needed the same guard as `CurlImpersonateHttpClient`: Playwright's `APIRequestContext.fetch` also treats an explicit `timeout=0` as "disable timeout". #2174 originally carried this fix; it's included here instead so all four HTTP clients are covered in one place. - Added a regression test covering all four clients. *✍️ Drafted by Claude Code*
…imeout # Conflicts: # src/crawlee/crawlers/_playwright/_playwright_http_client.py # tests/unit/crawlers/_playwright/test_playwright_http_client.py
Pijukatel
approved these changes
Aug 25, 2026
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.
Closes #2138
Summary
SendRequestFunction(used viacontext.send_request()in request handlers) only acceptedurl,method,payloadandheaders, so a handler could not bound how long an extra HTTP call may take. All four HTTP client implementations (HttpxHttpClient,CurlImpersonateHttpClient,ImpitHttpClient,PlaywrightHttpClient) already support a per-requesttimeout: timedelta | None— this PR just exposes it through the public contract.Changes
src/crawlee/_types.py: addtimeout: timedelta | None = Noneto theSendRequestFunctionprotocol + docstring.src/crawlee/crawlers/_basic/_basic_crawler.py: threadtimeoutthrough the_prepare_send_request_functionclosure intoHttpClient.send_request.src/crawlee/crawlers/_playwright/_playwright_http_client.py: fix —PlaywrightHttpClientpassedtimeout.total_seconds()to Playwright'sAPIRequestContext.fetch, which expects milliseconds. Without this fix, any timeout passed viacontext.send_request()on a Playwright crawler would be ~1000x too short (e.g.timedelta(seconds=12)→ 12ms).tests/unit/crawlers/_basic/test_basic_crawler.py: regression test —context.send_request('/slow?delay=2', timeout=timedelta(milliseconds=100))raisesasyncio.TimeoutError; plus a unit test assertingtimeoutis forwarded toHttpClient.send_request.tests/unit/crawlers/_playwright/test_playwright_http_client.py: regression test asserting the seconds→milliseconds conversion.Validation
pytest tests/unit/crawlers/_basic/test_basic_crawler.py→ 112 passed, 1 skippedruff check+ruff format --checkclean on all touched filesNote: no new dependency;
timeoutuses the existingtimedeltaconvention across the Python HTTP clients (the JS implementation names ittimeoutMillis, but the Python clients are timedelta-based).