Runs fail with `setImmediate is not defined` in browsers and Convex's runtime once a program runs longer than ~1 ms
维护者通常 1 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- javascript, typescript
调研方向
Start with src/runtime/driver.ts around the yielding path and src/vm.ts where the slice timing is defined, then inspect how Runtime.make accepts host configuration. Review the existing implementation in linked PR #102 and locate the runtime or browser tests covering long-running programs. Done means long executions yield successfully in browser and bare V8-like hosts, with an overridable host yield, while existing interrupt and timeout behavior remains covered.
由索引模型根据 Issue 内容生成。
描述
When testing with v8 runtimes, Claude and I encountered the following. The below is written by Claude.
When a program runs long enough to hit a VM slice, Driver.run() yields with setImmediate
(src/runtime/driver.ts:63).
SLICE_MS is 1 (src/vm.ts:154), so any
program that runs for more than about 1 ms of VM time reaches that call. Browsers don't define setImmediate, and
neither do runtimes like Convex's default V8 runtime.
Short programs, host calls, and suspend → toBytes → fromBytes → resume all work fine in these environments. The
codebase also seems to anticipate non-Node hosts already (Buffer is only installed when the host has one,
globals.ts:184). This looks like the one place where it still assumes Node.
Repro
Tested on dev @ bf492e1, bundled with bun build src/index.ts --target=browser, running in headless Chromium 158:
import { Runtime } from "./psychoscript.browser.js"
const runtime = Runtime.make({ globals: () => ({}) })
console.log(await runtime.execute("return 1 + 1"))
// { ok: true, value: 2 }
console.log(await runtime.execute("let s = 0; for (let i = 0; i < 300000; i++) s += i; return s"))
// { ok: false, error: { kind: "ExecutionFailure", message: "setImmediate is not defined" } }
I got the same failure in Node 24 after delete globalThis.setImmediate (which mimics Convex's runtime, since its
timer setup only
defines setTimeout/setInterval).
Workaround
Shimming it before import fixes it. With this shim, every probe I ran passed in Chromium:
globalThis.setImmediate ??= (fn) => {
const c = new MessageChannel()
c.port1.onmessage = () => fn()
c.port2.postMessage(0)
}
Suggested fix
Use a portable default yield, and let the embedder override it:
const defaultYield = (): Promise<void> =>
new Promise((resolve) => {
if (typeof setImmediate === "function") setImmediate(resolve)
else if (typeof MessageChannel === "function") {
const { port1, port2 } = new MessageChannel()
port1.onmessage = () => {
port1.close()
resolve()
}
port2.postMessage(null)
} else Promise.resolve().then(() => resolve()) // bare V8 hosts: no queueMicrotask either
})
// e.g. Runtime.make({ yieldToHost: () => new Promise((r) => setTimeout(r, 0)) })
Why both:
- Bare V8 hosts like Convex's runtime have no
setImmediate,MessageChannelorqueueMicrotask. The
microtask fallback is the only yield that works in Convex queries/mutations. Those hosts definesetTimeoutbut
refuse to run the timer, so the callback never fires and the function fails. - A microtask yield can't be interrupted: timers and abort listeners never run while the guest is CPU-bound, so
timeoutMsstops working. In a Convex-like action, a 200 ms timeout let a loop run to completion at 793 ms; with a
setTimeout(r, 0)yield it stopped withTimeoutExceededat 206 ms. Hosts where timers do work (e.g. Convex
actions) should be able to pass that yield in.
- 主要语言
- Rust
- 星标
- 470
- 派生
- 11
- 平均合并
- 4 小时 10 分钟
- 30 天内合并 PR
- 89
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
anomalyco/psychoscript 的其他 Issue
-
fix(rust): large BigInts bypass Limits::max_heap_bytes可能已有人在做 @brxs 于 2 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 25/100
anomalyco/psychoscript#113 ·
维护者通常 1 天内回复
-
fix(rust): Returned::Block deadlocks single-threaded async executors under execute_async可能已有人在做 @brxs 于 2 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
anomalyco/psychoscript#110 ·
维护者通常 1 天内回复
-
fix(rust): duplicate fresh strings bypass Limits::max_heap_bytes可能已有人在做 @brxs 于 2 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
anomalyco/psychoscript#109 ·
维护者通常 1 天内回复
-
Importing psychoscript throws RangeError in Safari (WebKit/JavaScriptCore)可能已有人在做 @vibdev 于 2 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
anomalyco/psychoscript#100 ·
维护者通常 1 天内回复
查看 anomalyco/psychoscript 的全部 Issue
相似的 Issue
-
bug good first issue web
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
triage: not actionable
难度 2/5 1-3 小时 新手友好度 66/100
oxc-project/oxc#30518 · 1 条评论 ·
维护者通常 1 天内回复
-
area: panel
难度 2/5 1-3 小时 新手友好度 68/100
frandev94/shattered-islands#98 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复