Skip to content

fix(crawlers)!: Raise when run() is cancelled or times out - #2493

Open
Mantisus wants to merge 2 commits into
apify:v2from
Mantisus:fix-run-cancellation
Open

Mantisus wants to merge 2 commits into
apify:v2from
Mantisus:fix-run-cancellation

Conversation

@Mantisus

Copy link
Copy Markdown
Collaborator

Description

  • crawler.run() raises CancelledError when the task that awaits it is cancelled, so a timeout around it, such as asyncio.wait_for() or asyncio.timeout(), raises TimeoutError instead of returning the final statistics. The requests in progress still finish first.
  • Ctrl+C still ends the crawl gracefully and returns the statistics.

Testing

  • Unit test that cancelling the task that awaits run() raises CancelledError.

✍️ Drafted by Claude Code

@Mantisus Mantisus self-assigned this Nov 22, 2026
@Mantisus
Mantisus requested a review from vdusek November 22, 2026 22:01
@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 22, 2026
@vdusek vdusek changed the title fix(crawlers)!: Raise on a timeout or cancellation around run() fix(crawlers)!: Raise when run() is cancelled or times out Nov 23, 2026
# SIGINT cancels only `run_task`, and the crawl then ends gracefully. A cancellation of the task that
# awaits `run()`, such as a timeout around it, propagates.
current_task = asyncio.current_task()
if current_task is not None and current_task.cancelling() > 0:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: cancelling() > 0 also counts an earlier cancel that the caller caught without uncancel(), and Ctrl+C would then raise. Reading cancelling() before await run_task and re-raising only when it went up, like asyncio.timeout does, avoids this. Checking it after a normal return too would also catch a cancel that the pool's cleanup suppresses (_autoscaled_pool.py:134).

✍️ Drafted by Claude Code

Comment thread docs/06_upgrading/upgrading_to_v2.md Resolved
@Mantisus
Mantisus requested a review from vdusek November 23, 2026 16:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants