fix(core): 解除 call_event_hook 中 functools.partial 包装#9178
Conversation
在 call_event_hook 中调用插件事件处理器前,解开 handler.handler 可能存在的 functools.partial 包装,避免因 star_manager 绑定导致的参数传递异常。
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Unwrapping the
functools.partialand then calling_handler_fn(event, *args, **kwargs)drops any arguments bound in the partial (e.g.,self), so consider using the unwrapped function only for theinspect.iscoroutinefunctioncheck and still invokinghandler.handlerto preserve its bound arguments. - If the goal is just to handle
inspect.iscoroutinefunctionreturningFalseforfunctools.partial, you can keep the original behavior by doingbase_fn = handler.handler.func if isinstance(handler.handler, functools.partial) else handler.handlerfor the assertion, but stillawait handler.handler(event, *args, **kwargs)for execution.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Unwrapping the `functools.partial` and then calling `_handler_fn(event, *args, **kwargs)` drops any arguments bound in the partial (e.g., `self`), so consider using the unwrapped function only for the `inspect.iscoroutinefunction` check and still invoking `handler.handler` to preserve its bound arguments.
- If the goal is just to handle `inspect.iscoroutinefunction` returning `False` for `functools.partial`, you can keep the original behavior by doing `base_fn = handler.handler.func if isinstance(handler.handler, functools.partial) else handler.handler` for the assertion, but still `await handler.handler(event, *args, **kwargs)` for execution.
## Individual Comments
### Comment 1
<location path="astrbot/core/pipeline/context_utils.py" line_range="95-102" />
<code_context>
for handler in handlers:
try:
- assert inspect.iscoroutinefunction(handler.handler)
+ _handler_fn = handler.handler
+ while isinstance(_handler_fn, functools.partial):
+ _handler_fn = _handler_fn.func
+ assert inspect.iscoroutinefunction(_handler_fn)
logger.debug(
f"hook({hook_type.name}) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}",
)
- await handler.handler(event, *args, **kwargs)
+ await _handler_fn(event, *args, **kwargs)
except BaseException:
logger.error(traceback.format_exc())
</code_context>
<issue_to_address>
**issue (bug_risk):** Unwrapping `functools.partial` for the call drops pre-configured args/kwargs from the handler.
Using `_handler_fn` for invocation drops any args/kwargs bound by `functools.partial`. For instance, if `handler.handler` is `functools.partial(async_fn, fixed_arg=1)`, calling `_handler_fn(event, *args, **kwargs)` skips `fixed_arg`. Consider using `_handler_fn` only to walk through `.func` for the `iscoroutinefunction` check, but still invoking `await handler.handler(event, *args, **kwargs)` to preserve the original partial binding behavior.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
There was a problem hiding this comment.
Code Review
This pull request updates the event hook calling logic in astrbot/core/pipeline/context_utils.py to support event handlers wrapped with functools.partial by unwrapping them before checking if they are coroutine functions. The reviewer suggested a valuable improvement to also handle decorators by using inspect.unwrap in a loop, which prevents assertion failures when handlers are wrapped with custom decorators using functools.wraps.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
Sure! I'm generating a new review now. |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Since you now resolve the underlying function into
_handler_fn, consider using_handler_fnrather thanhandler.handlerin the debug log so the log reflects the actual coroutine being invoked (and avoids confusion if additional wrapper layers are introduced later). - If a handler ends up not being a coroutine after unwrapping the
functools.partiallayers, usingassertwill raise anAssertionErrorthat may be harder to trace in production; consider raising a more explicit exception or logging a clear error instead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since you now resolve the underlying function into `_handler_fn`, consider using `_handler_fn` rather than `handler.handler` in the debug log so the log reflects the actual coroutine being invoked (and avoids confusion if additional wrapper layers are introduced later).
- If a handler ends up not being a coroutine after unwrapping the `functools.partial` layers, using `assert` will raise an `AssertionError` that may be harder to trace in production; consider raising a more explicit exception or logging a clear error instead.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Hey @Aryunr, I've posted a new review for you! |
sourcery-ai评测澄清debug log 用 Gemini-code-assist评测澄清
对 AstrBot 当前运行实例中所有已注册 handler 逐一验证:星标 handler 由
该建议防御的场景在当前 AstrBot 体系下不存在。此条不采纳. |
|
感谢你的详细解释。我理解了在 AstrBot 当前的插件注册和运行机制下, |
改动
修复了
call_event_hook中调用插件事件处理器时,handler.handler可能被star_manager绑定为functools.partial对象,导致参数传递异常的问题。修改文件:
astrbot/core/utils/context_utils.py— 在call_event_hook函数中,调用前通过while isinstance(_handler_fn, functools.partial)解开所有 partial 包装,再执行原始函数。测试结果
已在 AstrBot v4.26.5 + OneBotV11 环境下测试通过:
astrbot_plugin_ai_draw插件on_llm_request中self为 None,调用self.provider报错/画图、/识图功能正常,self正确绑定到插件实例运行日志
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
Bug Fixes:
Closes [Bug]引用已过期/不可访问的消息时,agent_sub_stages.internal 抛异常并直接向用户返回英文错误 "No valid file or URL provided",且不触发任何插件 hook,导致插件无法拦截 #9176
Summary by Sourcery
Bug Fixes: