Open
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- Catching bare
ExceptioninFutureTaskTool.callmakes it harder to distinguish expected scheduling failures from unexpected bugs; consider catching a more specific exception type (or wrapping in a custom error) so callers can react appropriately. - Returning the raw exception message directly in the user-facing string (
f"error: failed to schedule task: {e}") may leak internal details; consider mapping known failures to clearer, localized messages and logging the detailed error instead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Catching bare `Exception` in `FutureTaskTool.call` makes it harder to distinguish expected scheduling failures from unexpected bugs; consider catching a more specific exception type (or wrapping in a custom error) so callers can react appropriately.
- Returning the raw exception message directly in the user-facing string (`f"error: failed to schedule task: {e}"`) may leak internal details; consider mapping known failures to clearer, localized messages and logging the detailed error instead.
## Individual Comments
### Comment 1
<location path="astrbot/core/tools/cron_tools.py" line_range="115-124" />
<code_context>
- run_once=run_once,
- run_at=run_at_dt,
- )
+ try:
+ job = await cron_mgr.add_active_job(
+ name=name,
+ cron_expression=str(cron_expression) if cron_expression else None,
+ payload=payload,
+ description=note,
+ run_once=run_once,
+ run_at=run_at_dt,
+ )
+ except Exception as e:
+ return f"error: failed to schedule task: {e}"
next_run = job.next_run_time or run_at_dt
suffix = (
</code_context>
<issue_to_address>
**🚨 issue (security):** Catching broad Exception and returning a plain error string may leak details and break return-type expectations.
This path now exposes the raw exception message to users and may also change the return type of `call` from a structured value to a plain string. Please either catch more specific exception types and return a sanitized, generic error message, or re-raise (possibly as a domain-specific exception) so that upstream code can handle formatting and typing consistently.
</issue_to_address>
### Comment 2
<location path="astrbot/core/cron/manager.py" line_range="184-186" />
<code_context>
)
except Exception as e:
logger.error(f"Failed to schedule cron job {job.job_id}: {e!s}")
+ raise
def _get_next_run_time(self, job_id: str):
</code_context>
<issue_to_address>
**suggestion:** Re-raising after logging at error level may cause duplicated error logs and could use `logger.exception` for better context.
Because `_schedule_job` now re-raises, callers will likely log the failure too, so this `logger.error` risks duplicate top-level error entries and omits the traceback.
To refine this:
- Use `logger.exception("Failed to schedule cron job %s", job.job_id)` if this is the primary logging site, or
- Lower the log level here (or remove this log) and let the caller handle error-level logging.
```suggestion
except Exception:
logger.exception("Failed to schedule cron job %s", job.job_id)
raise
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces error propagation in the cron job scheduling manager and adds error handling for task creation within the cron tools. The review feedback points out that re-raising exceptions in _schedule_job may inadvertently break the startup synchronization loop for other tasks and suggests extending similar error handling to the task editing operation to ensure robustness against invalid user input.
astrbot/core/cron/manager.py
Outdated
| ) | ||
| except Exception as e: | ||
| logger.error(f"Failed to schedule cron job {job.job_id}: {e!s}") | ||
| raise |
Contributor
astrbot/core/tools/cron_tools.py
Outdated
Comment on lines
+115
to
+125
| try: | ||
| job = await cron_mgr.add_active_job( | ||
| name=name, | ||
| cron_expression=str(cron_expression) if cron_expression else None, | ||
| payload=payload, | ||
| description=note, | ||
| run_once=run_once, | ||
| run_at=run_at_dt, | ||
| ) | ||
| except Exception as e: | ||
| return f"error: failed to schedule task: {e}" |
Contributor
9e9e630 to
1a3a42a
Compare
1a3a42a to
3dc5e3c
Compare
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.
fix #7439
Modifications / 改动点
_schedule_job() 原本在调度失败时仅记录错误后静默返回,现改为重新抛出异常;FutureTaskTool.call() 新增 try/except
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Propagate cron job scheduling failures instead of silently swallowing them and surface a user-visible error message when task creation fails.
Bug Fixes: