Skip to content

fix(t2i): run Shiki runtime injection in executor to avoid blocking e…#8013

Open
lingyun14beta wants to merge 3 commits intoAstrBotDevs:masterfrom
lingyun14beta:fix/t2i-shiki-inject-blocks-event-loop
Open

fix(t2i): run Shiki runtime injection in executor to avoid blocking e…#8013
lingyun14beta wants to merge 3 commits intoAstrBotDevs:masterfrom
lingyun14beta:fix/t2i-shiki-inject-blocks-event-loop

Conversation

@lingyun14beta
Copy link
Copy Markdown
Contributor

@lingyun14beta lingyun14beta commented May 5, 2026

Fix #8011 在 Windows 上使用自部署 astrbot-t2i-service 时,AstrBot 在文转图时会完全卡死,所有消息停止处理。

Modifications / 改动点

  • astrbot/core/utils/t2i/network_strategy.py:
    新增 _prepare_template_sync() 方法承载 Shiki runtime 注入的同步逻辑,
    并在 render_custom_template() 中通过 loop.run_in_executor() 在线程池中异步执行,
    避免阻塞事件循环。代码高亮功能保留。
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image 正常显示

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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.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:

  • Prevent AstrBot from freezing during text-to-image rendering when Shiki runtime injection processes large templates (e.g., on Windows with self-hosted astrbot-t2i-service).

Enhancements:

  • Introduce a synchronous template preprocessing helper used via run_in_executor to keep syntax highlighting support without blocking the event loop.

@auto-assign auto-assign Bot requested review from Soulter and advent259141 May 5, 2026 08:18
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels May 5, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • 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).
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/utils/t2i/network_strategy.py
Comment thread astrbot/core/utils/t2i/network_strategy.py
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread astrbot/core/utils/t2i/network_strategy.py Outdated
Comment thread astrbot/core/utils/t2i/network_strategy.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 自部署 t2i-service 图片空白

1 participant