Skip to content

fix(code_run): 用 reader join 替代固定 sleep(1) 轮询,省掉每次工具调用最多 1 秒白等 - #876

Closed
leonsong09 wants to merge 1 commit into
lsdefine:mainfrom
leonsong09:fix/code-run-poll-latency
Closed

leonsong09 wants to merge 1 commit into
lsdefine:mainfrom
leonsong09:fix/code-run-poll-latency

Conversation

@leonsong09

Copy link
Copy Markdown

Fixes #875

改动

ga.py code_run():

-            time.sleep(1)
+            t.join(0.05)  # 等 reader 退出, 不再固定 sleep(1)

         t.join(timeout=1)
         exit_code = process.poll()
+        if exit_code is None:  # reader 可能先于进程回收退出
+            try: exit_code = process.wait(timeout=5)
+            except subprocess.TimeoutExpired: exit_code = process.poll()

1 行改 3 行加,净 +3 行。

为什么要加 process.wait()

单改 t.join(0.05) 会引入回归:reader 线程退出不代表进程已被回收,poll() 此时仍返回 None,退出码会丢成 None(结果被判为 ⏳ 而不是 success/error)。自测第一版就是这么挂的,所以补 5s 兜底 wait();超时仍走 poll(),不会卡住。

验证(纯内存,old/new 同源对照)

场景 旧 新
print('hi') 1.010s 0.245s
3000 行输出 1.010s 0.400s
sys.exit(7) exit=7 exit=7(一致)
超时(10s 脚本 / timeout=2) 2.01s 2.03s(一致)
stop_signal 触发 [Stopped] [Stopped](一致)

3000 行输出的内容与顺序逐行比对一致。

收益

temp/model_responses/*.txt 的 5,187 个响应间隔中位恰为 1.000s,其中 2,012 次精确 1s,code_run 占 1,912 次。短工具调用每次约省 0.77s。

影响面

只动 code_run 的等待方式,stream_reader、smart_format、超时判定、返回结构均未变。不涉及 llmcore / 前端 / 渠道配置。

进程结束后最长仍要空等 1s 才发现。改为 t.join(0.05) 后一退出即返回。
本机 5.2k 次响应间隔统计中位恰为 1.000s(2012 次精确 1s, code_run 占 1912),
实测短任务 1.01s->0.24s, 3000 行输出 1.01s->0.37s, 退出码与超时行为不变。

补 process.wait() 兜底: reader 可能先于进程回收退出导致 poll() 为 None。
@leonsong09

Copy link
Copy Markdown
Author

superseded by #880 — see #880 / #881.

Folded into #880 as commit 5 of 6, unchanged (+4/-1 in ga.py).

Closing to keep one open diff per change.

@leonsong09 leonsong09 closed this Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(code_run): 固定 sleep(1) 轮询导致每次工具调用白等最多 1 秒

1 participant