fix(db): 路由跨 loop 的 DB 访问回引擎主 loop,消除 asyncmy "attached to a different loop"
会商/定时任务/沙箱工具等在 worker 线程的临时 loop 上用 asyncio.run 跑全局引擎连接池的 DB 操作,连接被绑定到随即关闭的临时 loop;池子回收/释放该连接时 asyncmy _terminate_graceful_close 在错误的 loop 上触发 future,持续刷 "got Future attached to a different loop"。 - engine.py: 新增 run_db_blocking(fire_and_forget_db_write 的带返回值版本)。离开主 loop 时用 run_coroutine_threadsafe 把协程送回主 loop 执行、阻塞调用线程拿结果;仅在无主 loop 或正同步处于主 loop 内(送回会死锁)时退回临时 loop。 - thread_paths / prompt / skill_tools: 三处同步→异步阻塞读桥(任务owner解析/常驻技能/ 技能可见性)改走 run_db_blocking,删除各自重复的 _run_coro_blocking。 - llm/tool metrics 中间件: fire-and-forget 写改走 fire_and_forget_db_write。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5ecf28e60c
commit
6aff165d09
6 files changed
+147
-96
No files matched your search
+2
-23
@@ -86,27 +86,6 @@ def _drop_archived_skills(skills: list[Skill]) -> list[Skill]:
|
||||
return skills
|
||||
|
||||
|
||||
def _run_coro_blocking(coro):
|
||||
"""Run an async coroutine to completion from sync code.
|
||||
|
||||
Mirrors ``skill_tools._run_coro_blocking``: when already inside a running
|
||||
event loop (e.g. prompt built during an async agent run), hand the coroutine
|
||||
to a fresh loop in a worker thread; otherwise run it directly.
|
||||
"""
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
in_loop = True
|
||||
except RuntimeError:
|
||||
in_loop = False
|
||||
|
||||
if in_loop:
|
||||
import concurrent.futures
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
|
||||
return pool.submit(asyncio.run, coro).result()
|
||||
return asyncio.run(coro)
|
||||
|
||||
|
||||
def _load_always_on_names() -> set[str]:
|
||||
"""Names marked 常驻(免压缩), read from the **DB** (skills table).
|
||||
|
||||
@@ -114,11 +93,11 @@ def _load_always_on_names() -> set[str]:
|
||||
simply treats every skill as compressible (safe default).
|
||||
"""
|
||||
try:
|
||||
from deerflow.persistence.engine import get_session_factory
|
||||
from deerflow.persistence.engine import get_session_factory, run_db_blocking
|
||||
from deerflow.persistence.skills import make_skill_store
|
||||
|
||||
store = make_skill_store(get_session_factory())
|
||||
return set(_run_coro_blocking(store.list_always_on_names()))
|
||||
return set(run_db_blocking(store.list_always_on_names()))
|
||||
except Exception:
|
||||
logger.debug("Failed to load always_on skill names from DB", exc_info=True)
|
||||
return set()
|
||||
|
||||
Reference in new issue
Block a user