Skip to content

fix(xhs): let Ctrl+C stop the crawl instead of being retried - #982

Open
ChrisDoufu wants to merge 1 commit into
NanmiCoder:mainfrom
ChrisDoufu:pr/xhs-ctrl-c-not-retried
Open

ChrisDoufu wants to merge 1 commit into
NanmiCoder:mainfrom
ChrisDoufu:pr/xhs-ctrl-c-not-retried

Conversation

@ChrisDoufu

Copy link
Copy Markdown

Context

The Xiaohongshu client uses tenacity to retry a request that fails. request() and get_note_by_id_from_html() retry on all exceptions, except the exceptions in a list. asyncio.CancelledError is not in that list.

Problem

Ctrl+C does not stop the crawl.

  1. When you push Ctrl+C, asyncio cancels the crawl task.
  2. The request that runs at that time gets CancelledError.
  3. Tenacity catches this error and sends the request again.
  4. After three attempts, tenacity raises RetryError.
  5. get_note_detail_async_task() catches RetryError and continues with the HTML fallback.

Thus, the crawl continues after Ctrl+C.

Change

Add asyncio.CancelledError to the list of exceptions that tenacity does not retry. The change is in request() and in get_note_by_id_from_html(). Now Ctrl+C stops the crawl immediately.

Test

tests/test_xhs_raw_response_errors.py::test_ctrl_c_is_not_retried makes the HTTP client raise CancelledError. The test makes sure that request() raises CancelledError after one attempt. The test fails before this change and passes after it. All 12 tests in the file pass.

🤖 Generated with Claude Code

The tenacity retry on request() and get_note_by_id_from_html() retries every
exception except a listed few, and asyncio.CancelledError was not among them.
Pressing Ctrl+C cancelled the task, tenacity caught the CancelledError and sent
the request again, and in the note path the final RetryError was swallowed, so
the crawl kept running. CancelledError is now excluded from retries.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant