xskill icon indicating copy to clipboard operation
xskill copied to clipboard

0.7.1a2 进程唯一一把门让 close 排队时挡住全进程,写事务里做文件读写把等待放大到 8-30 秒

Open Shimmering-Ruby opened this issue 2 months ago • 0 comments

这是什么

0.7.1a2(PR #425)修掉了 #424 的面板冻死,但引入了一个新的慢:16 个 xskill-team-sync 线程全部停在 _build_auto_canary_users → _sqlite_call,主线程的 HTTP 请求跟着阻塞 8-30 秒。配置 canary.enabled=false 也压不住。

名词沿用 #424:门 是 _sqlite_connect.py 里的 _SQLiteCallGate;共享侧 是普通 SQL 调用;独占侧 只有 Connection.close()。

根因

一、门是进程唯一一把,close 排队时把全进程都挡住

#425 给独占侧加了优先:close 在等的时候,新的共享调用也要排队。这条规则本身是对的,问题在门的作用域。进程里只有一个 _SQLITE_CALL_GATE,registry.db、team_clients.db、team_profile.db、embed store、atom 向量索引全共用。于是:

  • 任意一个连接的 close 要等全进程所有数据库上的在飞调用归零;
  • 它在等的期间,全进程所有新调用都排在它后面;
  • 它等的那次在飞调用可能正在 SQLite 的写锁上烧 busy_timeout(10 秒)。

线程停在 _build_auto_canary_users 只是因为它是最热的调用点(每次 /sync 每个带 staging 的 skill 查一次),不是它慢。生产库上那 3 条查询冷 44 ms、热 0.8 ms。

面板探针(16 线程读 + 每 0.1 秒一次 close + 一个长写事务):

panel p99 panel max
0.7.1a1 6-13 ms 15-70 ms
0.7.1a2 337-410 ms 1.7-1.9 s

二、写锁持有时间无界:写事务里在做文件读写

上面那 10 秒的来源是写锁。WAL 下读不等写,但写等写,上限是最长那笔写事务。审计出 6 处写事务在写锁开着的时候做非 SQL 的事:

位置 锁开着时干的事
registry.py reset_trajectories 逐条 trajectory glob + unlink atom 文件,逐条 DELETE … GLOB
registry.py reset_not_fit_for_interest_change 同上,config 热加载自动触发
registry.py discover_trajectories 第一条 INSERT 后继续 stat 每个文件、每个新文件读两个 json sidecar
registry.py unregister_dir 逐 trajectory 3 条语句,且挂在 async def 路由上
catalog_store.py reconcile_native_canary_catalog 每个 skill 读 git refs、SKILL.md、candidates.yml
projection.py project_generation BEGIN IMMEDIATE 之后逐行 json.dumps 再写 8 张表

实测 16 个短写线程加一笔长写事务:长写 300 ms 时短写最长等 1.1 秒,长写 2 秒时最长等 2.5 秒。等待跟着最长写事务走,busy_timeout 只是兜底。

三、canary.enabled 是死配置

config.yaml 一直有这个字段,CanaryConfig.from_dict 从不读。设为 false 后 /sync 照样逐 skill 查自动灰度用户。

四、还有 4 个 async def 路由在事件循环上跑同步 SQLite

api_trajectory_content、api_list_registry_dirs、api_register_dir、api_unregister_dir,经 list_watch_dirs / register_dir / unregister_dir 走 pooled_connection。#425 只改了两个 trajectory 路由。

五、canary 缓存过期时 16 个线程同时重算

auto_canary_users_by_skill 的 5 秒缓存在锁外重建,过期瞬间每个 team-sync 线程各跑一遍那 3 条查询。

修法

  1. 门改成每个连接一把。close 只等本连接自己的在飞调用;连接是线程亲和的(pooled_connection 线程本地、一次性连接同线程开关),实际几乎不等。放弃的唯一契约是"A 连接的 close 不与 B 连接的调用重叠",对应改 2 条测试。安全性:sqlite3_close 释放 GIL 后只拿本连接的 mutex 和 VFS 静态互斥量,后者临界区内不回调 Python,不会与其他连接互等。
  2. 上表 6 处写事务改成"锁开着时只跑 SQL":文件读写、json.dumps 移到事务前或 commit 后,逐行循环改成事务外算好行列表、事务内 executemany。
  3. canary.enabled 接成真开关,默认 true 不改现有行为。
  4. 4 个路由改 def。
  5. canary 缓存过期时在锁内重建,一个线程算、其余等结果。

Shimmering-Ruby avatar Sep 16 '26 03:09 Shimmering-Ruby