Skip to content

fix: t2i shiki issue - #8013

Merged
Soulter merged 3 commits into
AstrBotDevs:masterfrom
lingyun14beta:fix/t2i-shiki-inject-blocks-event-loop
May 22, 2026
Merged

Soulter merged 3 commits into
AstrBotDevs:masterfrom
lingyun14beta:fix/t2i-shiki-inject-blocks-event-loop

Conversation

@lingyun14beta

@lingyun14beta lingyun14beta commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Fix #8011 fix #8055 在 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.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Fix a Windows freeze when using self-hosted astrbot-t2i-service by making template preprocessing non-blocking.

@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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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

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

Copy link
Copy Markdown
Contributor

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
@lingyun14beta lingyun14beta changed the title fix(t2i): run Shiki runtime injection in executor to avoid blocking e… fix:t2i May 6, 2026
@lingyun14beta
lingyun14beta marked this pull request as draft May 6, 2026 17:03
@lingyun14beta
lingyun14beta marked this pull request as ready for review May 7, 2026 16:26
@auto-assign
auto-assign Bot requested a review from Fridemn May 7, 2026 16:26

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Since _prepare_template_sync mutates tmpl_data, consider explicitly copying the dict before merging (tmpl_data = {**tmpl_data}) to avoid surprising side effects if the same tmpl_data is reused elsewhere or shared across calls.
  • Given that _prepare_template_sync is a @staticmethod, you can call it via type(self)._prepare_template_sync(...) or move it to a module-level helper to make the lack of self dependence clearer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since `_prepare_template_sync` mutates `tmpl_data`, consider explicitly copying the dict before merging (`tmpl_data = {**tmpl_data}`) to avoid surprising side effects if the same `tmpl_data` is reused elsewhere or shared across calls.
- Given that `_prepare_template_sync` is a `@staticmethod`, you can call it via `type(self)._prepare_template_sync(...)` or move it to a module-level helper to make the lack of `self` dependence clearer.

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.

@lingyun14beta

Copy link
Copy Markdown
Contributor Author

我的问题:(

@zzz6839

zzz6839 commented May 21, 2026

Copy link
Copy Markdown
meme-nikke-goddess-of-victory

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 22, 2026
@Soulter Soulter changed the title fix:t2i fix: t2i shiki issue May 22, 2026
@Soulter
Soulter merged commit 3f20bbd into AstrBotDevs:master May 22, 2026
21 checks passed
@lingyun14beta
lingyun14beta deleted the fix/t2i-shiki-inject-blocks-event-loop branch May 22, 2026 15:55
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 lgtm This PR has been approved by a maintainer 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] 4.24.2版本文本转图像显示空白 [Bug] 自部署 t2i-service 图片空白

3 participants