Commit 4d4457d
chore: 引入 async def 同步阻塞静态检查 + 修复存量违规 (Project-N-E-K-O#842)
* chore: 引入 async def 同步阻塞静态检查 + 修复存量违规
动机:主进程运行 FastAPI + asyncio 事件循环,心跳和 WebSocket 对
事件循环 stall 极其敏感(曾出现 end_session 清理期间 ~1s 阻塞)。
仅靠人工 grep 无法阻止同步阻塞调用反复潜入,需要静态守卫。
实现:
- 启用 ruff 的 ASYNC210/220/221/222/251 规则,覆盖 time.sleep /
requests / urllib.request / subprocess.run / Popen.wait 等。
- 新增 scripts/check_async_blocking.py,用 AST 补齐 ruff 不覆盖的
部分:Thread/Process.join(timeout=) / queue.Queue.get() /
原生 socket.recv/accept/connect。
接收者匹配用 tail-name("tts_thread" / "request_queue" / "sock"…)
以避免 httpx / websockets / zmq socket 的误报。
嵌套 sync def 不会被检查——匹配 ruff 的行为,让放进线程/executor
的 worker 天然免于误报。
- 新增 .github/workflows/analyze.yml,CI 并行跑 ruff 与自定义脚本。
- 支持 `# noqa: ASYNC_BLOCK — <原因>` 行级抑制。
修复存量违规:
- main_server.py:546/596 initialize_character_data 两处
sync_process[k].join(timeout=...) 包裹进 asyncio.to_thread。
- main_logic/core.py:1453 tts_thread.join(timeout=1.0) 同上。
- main_logic/cross_server.py:174 maintain_connection 内
message_queue.get() 改为 get_nowait(),和外层 empty() 检查匹配,
消除竞态。
验证:ruff check . 与 scripts/check_async_blocking.py 均通过;
合成违规(time.sleep / Thread.join / Queue.get / socket.recv /
requests.get / subprocess.run)均被其中一个工具拦截。
https://claude.ai/code/session_01TC7MP5nYYtXZXMmTMWNQuN
* chore(ci): analyze.yml 显式收敛 GITHUB_TOKEN 至 contents:read
静态检查 job 不需要写入 repo 或任何其他资源,按最小权限原则显式
声明 permissions,避免默认宽权限。
https://claude.ai/code/session_01TC7MP5nYYtXZXMmTMWNQuN
* refactor(async-check): 精确捕获 Empty + 澄清 plugin/ 为何不进默认扫描
- cross_server.py: `except Exception` 收紧为 `except Empty`(CodeRabbit
建议喵)。语义更清晰,`queue.Queue.get_nowait()` 空队列就是 Empty,
其他异常不该在这里被吞。
- check_async_blocking.py: 在 DEFAULT_PATHS 旁加注释说明为什么不把
`plugin/` 纳入默认扫描——pyzmq Socket 和 asyncio.Queue 的 tail-name
与本检查器的启发式冲撞,在 plugin/ 下会产生 15+ 条误报(zmq
`sock.recv`/`connect`、`await asyncio.wait_for(q.get(), ...)` 等)。
ruff 的 ASYNC* 规则已经覆盖 plugin/,后续可以单独用类型推断的方式
再把 plugin/ 纳入这个脚本。
https://claude.ai/code/session_01TC7MP5nYYtXZXMmTMWNQuN
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent b904931 commit 4d4457d
6 files changed
Lines changed: 363 additions & 14 deletions
File tree
- .github/workflows
- main_logic
- plugin/plugins/mijia
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1450 | 1450 | | |
1451 | 1451 | | |
1452 | 1452 | | |
1453 | | - | |
| 1453 | + | |
1454 | 1454 | | |
1455 | 1455 | | |
1456 | 1456 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
171 | 172 | | |
172 | 173 | | |
173 | 174 | | |
174 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
175 | 179 | | |
176 | 180 | | |
177 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
163 | 168 | | |
164 | | - | |
165 | | - | |
| 169 | + | |
| 170 | + | |
166 | 171 | | |
167 | 172 | | |
168 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
0 commit comments