From 3f20bbdf236fd9f8b86687a292baca97231701fc Mon Sep 17 00:00:00 2001 From: lingyun14 Date: Fri, 22 May 2026 19:11:38 +0800 Subject: [PATCH] fix: t2i shiki issue (#8013) * fix(t2i): run Shiki runtime injection in executor to avoid blocking event loop * Update network_strategy.py * ruff --- astrbot/core/utils/t2i/network_strategy.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/astrbot/core/utils/t2i/network_strategy.py b/astrbot/core/utils/t2i/network_strategy.py index 4fec12195..1191e154a 100644 --- a/astrbot/core/utils/t2i/network_strategy.py +++ b/astrbot/core/utils/t2i/network_strategy.py @@ -156,9 +156,11 @@ class NetworkRenderStrategy(RenderStrategy): if options: default_options |= options - if SHIKI_RUNTIME_TEMPLATE_PATTERN.search(tmpl_str): - tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data - tmpl_str = inject_shiki_runtime(tmpl_str) + # 在线程池中执行 Shiki 注入,避免 1.2MB JS 处理阻塞事件循环 + loop = asyncio.get_running_loop() + tmpl_str, tmpl_data = await loop.run_in_executor( + None, self._prepare_template_sync, tmpl_str, tmpl_data + ) post_data = { "tmpl": tmpl_str, "json": return_url, @@ -219,3 +221,11 @@ class NetworkRenderStrategy(RenderStrategy): }, return_url, ) + + @staticmethod + def _prepare_template_sync(tmpl_str: str, tmpl_data: dict) -> tuple[str, dict]: + """在线程池中执行的同步模板预处理(避免阻塞事件循环)""" + if SHIKI_RUNTIME_TEMPLATE_PATTERN.search(tmpl_str): + tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data + tmpl_str = inject_shiki_runtime(tmpl_str) + return tmpl_str, tmpl_data