fix(cron): handle CancelledError so cancelled jobs report correct status#1894
Merged
xieyxclack merged 2 commits intoagentscope-ai:mainfrom Mar 20, 2026
Merged
Conversation
asyncio.CancelledError inherits from BaseException (not Exception) since Python 3.8, so `except Exception` blocks silently miss it. This caused cancelled cron jobs to report "running" or stale "success" status instead of reflecting the cancellation. Add explicit `except asyncio.CancelledError` handlers before `except Exception` in _execute_once(), _heartbeat_callback(), and executor.execute(). Each handler sets the correct status, logs the event, and re-raises so asyncio cancellation propagates correctly. Also adds "cancelled" to the CronJobState.last_status Literal type. Closes agentscope-ai#1829 This contribution was developed with AI assistance (Claude Code + Codex). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Mar 20, 2026
xieyxclack
approved these changes
Mar 20, 2026
Member
xieyxclack
left a comment
There was a problem hiding this comment.
LGTM. Thank you for your contribution!
Contributor
Author
|
Thanks for the review and merge. |
tudan110
pushed a commit
to tudan110/QwenPaw
that referenced
this pull request
Apr 4, 2026
…tus (agentscope-ai#1894) Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
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.
Summary
When a cron job's asyncio task is cancelled (shutdown, timeout, manual cancellation),
asyncio.CancelledErroris raised. Since Python 3.8,CancelledErrorinherits fromBaseException- notException- so the existingexcept Exceptionblocks in the cron module silently miss it. The result: cancelled jobs report"running"(or a stale"success"from the prior run) instead of reflecting the cancellation.Fix: Add explicit
except asyncio.CancelledErrorhandlers before everyexcept Exceptionin:CronManager._execute_once()- setslast_status = "cancelled"and re-raisesCronManager._heartbeat_callback()- logs and re-raisesCronExecutor.execute()- catches bothTimeoutErrorandCancelledError, logs, and re-raisesAlso adds
"cancelled"to theCronJobState.last_statusLiteral type inmodels.py.This follows the established pattern in
app/runner/runner.py:274andapp/channels/dingtalk/channel.py:1559which already handleCancelledErrorexplicitly.Changes
src/copaw/app/crons/manager.py: Addexcept asyncio.CancelledErrorin_execute_once()and_heartbeat_callback()src/copaw/app/crons/executor.py: Addexcept asyncio.TimeoutErrorandexcept asyncio.CancelledErroraroundwait_for()callsrc/copaw/app/crons/models.py: Add"cancelled"toCronJobState.last_statusLiteralTest plan
ruff checkpasseslast_status == "cancelled"instead of"success"or"running"Closes #1829
This contribution was developed with AI assistance (Claude Code + Codex).