perf(runtime): stop sandbox pump loop idle-spinning while awaiting a host call (#3233)#3242
Merged
Merged
Conversation
…host call (#3233) The hook/action runner's pump loop yielded via setImmediate every iteration and drained the VM job queue. While the body only *waits* on an in-flight host promise the queue is empty each pass, so the loop woke ~200k×/s doing nothing (~50k iterations for a 250ms wait — surfaced in the timeout message's pump count). Make the yield adaptive: stay on setImmediate while the script is progressing; once a pump executes zero VM jobs, ramp up to a small capped setTimeout (≤8ms). Any executed job (a settled host call, a resumed continuation) resets the fast path, so sequential host calls and multi-turn work keep their low latency — only a genuinely idle wait backs off, by at most the cap. Deadline enforcement and every existing pump-budget/timeout/transaction guarantee are unchanged. Tests: a never-settling host call now reports a bounded pump count (<1000 for a 300ms wait, vs ~50k before); a call that settles at ~120ms — squarely in the backoff regime — still resolves promptly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BZguyAaQbyUpwMZ2gMLaAP
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 16 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
os-zhuang
marked this pull request as ready for review
July 19, 2026 12:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3233.
问题
hook/action 沙箱的 pump 循环每轮
setImmediate让出后排空 VM 任务队列。当 body 只是在等待一个在途的宿主调用(慢的ctx.api读写,或一次要跨很多 event-loop turn 才 settle 的调用)时,队列每轮都是空的,于是循环以 ~20 万次/秒空转——250ms 的等待里约 5 万次(可从超时信息的 pump 计数看到)。改动
packages/runtime/src/sandbox/quickjs-runner.ts的 pump 循环让出改为自适应:setImmediate,近零延迟;setTimeout(1→2→4→8ms,封顶 8ms);deadline 兜底与既有的所有 pump-budget / 超时 / 事务语义不变。
测试(
quickjs-runner.test.ts新增 2 例)其余 pump-budget(>1000 顺序调用、>1000-turn 单次调用、永不 settle 超时)、超时解析、事务、嵌套重入等既有用例全部通过(34/34)。附 changeset(
patch)。备注
这是 #3233 里列的「退一步的最小改动:自适应退避」——相对「宿主 settle 时信号唤醒」的完整重写,退避对沙箱这个关键热路径风险更低、控制流不变,同样把空转从 ~20 万/秒降到 ~百余/秒。
🤖 Generated with Claude Code
Generated by Claude Code