fix(t2i): run Shiki runtime injection in executor to avoid blocking e…#8013
Open
lingyun14beta wants to merge 3 commits intoAstrBotDevs:masterfrom
Open
fix(t2i): run Shiki runtime injection in executor to avoid blocking e…#8013lingyun14beta wants to merge 3 commits intoAstrBotDevs:masterfrom
lingyun14beta wants to merge 3 commits intoAstrBotDevs:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- Consider using
asyncio.get_running_loop()instead ofget_event_loop()insiderender_custom_templateto avoid edge cases where no default loop is set or when called in different asyncio contexts. - Since
_prepare_template_syncdoes not useself, you might make it a@staticmethodor a standalone helper function to clarify that it has no dependency on instance state. - Because
_prepare_template_syncruns in a thread pool, it may be safer to avoid any potential in-place mutations oftmpl_data(e.g., ensure callers treat the returned dict as the canonical one and do not reuse the original object concurrently).
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using `asyncio.get_running_loop()` instead of `get_event_loop()` inside `render_custom_template` to avoid edge cases where no default loop is set or when called in different asyncio contexts.
- Since `_prepare_template_sync` does not use `self`, you might make it a `@staticmethod` or a standalone helper function to clarify that it has no dependency on instance state.
- Because `_prepare_template_sync` runs in a thread pool, it may be safer to avoid any potential in-place mutations of `tmpl_data` (e.g., ensure callers treat the returned dict as the canonical one and do not reuse the original object concurrently).
## Individual Comments
### Comment 1
<location path="astrbot/core/utils/t2i/network_strategy.py" line_range="159-164" />
<code_context>
- tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data
- tmpl_str = inject_shiki_runtime(tmpl_str)
+ # 在线程池中执行 Shiki 注入,避免 1.2MB JS 处理阻塞事件循环
+ import asyncio
+ loop = asyncio.get_event_loop()
+ tmpl_str, tmpl_data = await loop.run_in_executor(
+ None, self._prepare_template_sync, tmpl_str, tmpl_data
+ )
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Use `asyncio.get_running_loop()` in async context instead of `get_event_loop()`
Within `async def`, `asyncio.get_running_loop()` avoids deprecation/`RuntimeError` issues in newer Python and makes it explicit that we’re already in a running loop. You can simplify to:
```python
loop = asyncio.get_running_loop()
tmpl_str, tmpl_data = await loop.run_in_executor(
None, self._prepare_template_sync, tmpl_str, tmpl_data
)
```
This preserves behavior while matching current asyncio best practices.
```suggestion
# 在线程池中执行 Shiki 注入,避免 1.2MB JS 处理阻塞事件循环
import asyncio
loop = asyncio.get_running_loop()
tmpl_str, tmpl_data = await loop.run_in_executor(
None, self._prepare_template_sync, tmpl_str, tmpl_data
)
```
</issue_to_address>
### Comment 2
<location path="astrbot/core/utils/t2i/network_strategy.py" line_range="162" />
<code_context>
+ # 在线程池中执行 Shiki 注入,避免 1.2MB JS 处理阻塞事件循环
+ import asyncio
+ loop = asyncio.get_event_loop()
+ tmpl_str, tmpl_data = await loop.run_in_executor(
+ None, self._prepare_template_sync, tmpl_str, tmpl_data
+ )
</code_context>
<issue_to_address>
**question (bug_risk):** Consider the thread-safety of `get_shiki_runtime()` and `inject_shiki_runtime()` when moving them into a thread pool
Because `_prepare_template_sync` now runs in a thread pool, `get_shiki_runtime()` and `inject_shiki_runtime()` may execute concurrently on multiple worker threads. If they touch global state, caches, or other non–thread-safe objects, this can introduce race conditions. Please verify (or update) these helpers to be thread-safe in this new context.
</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 offloads Shiki runtime injection and template preparation to a thread pool using run_in_executor to prevent heavy processing from blocking the event loop. The review feedback suggests using asyncio.get_running_loop() for safer loop retrieval, removing a redundant local import of asyncio, and decorating the new _prepare_template_sync method with @staticmethod since it does not access instance state.
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 #8011 在 Windows 上使用自部署 astrbot-t2i-service 时,AstrBot 在文转图时会完全卡死,所有消息停止处理。
Modifications / 改动点
新增 _prepare_template_sync() 方法承载 Shiki runtime 注入的同步逻辑,
并在 render_custom_template() 中通过 loop.run_in_executor() 在线程池中异步执行,
避免阻塞事件循环。代码高亮功能保留。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
注意:此 PR 需配合 astrbot-t2i-service的pr 同时合并
😊 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
Avoid blocking the async T2I rendering flow by offloading Shiki runtime template preparation to a background thread.
Bug Fixes:
Enhancements: