Repository navigation
fix(cluster,transport,http): harden stream/socket/timer lifecycle and remove dead exports - #2464
Conversation
… remove dead exports
## 变更摘要
- 改前:
- `src/lib/http/respond.mjs`:`collectBody` 未监听 `aborted` 与 `req.complete === false` 的 `close`,客户端中途断连时 Promise 挂起且监听器未清理。
- `src/lib/transport/api-kernel-client.mjs`:`apiKernelHealth` 未消费响应体(缺 `res.resume()`);`forwardApi` 重试定时器不响应 `AbortSignal` 且超时后仍继续重试。
- `src/lib/transport/proto-front-client.mjs`:`ensureProtoFront` 异常拒绝时未清理 `readyPromises` 导致缓存永久毒化;`postProtoFront` 缺 `res` `error`/`close` 兜底;存在零引用死导出 `protoFrontSnapshot`。
- `src/lib/transport/codex-kernel-client.mjs`:`codexKernelDescribe` 缺响应大小上限及 `res` `error`/`close` 处理,响应体截断时 Promise 永久挂起。
- `src/lib/transport/rust-kernel-client.mjs`:`dispatchRustAgentSession` 响应流提前关闭且未触发 `end`/`error` 时可挂起至 600s 超时,非流式分支收包未续期超时定时器。
- `src/lib/transport/slot-oauth.mjs`:`spawnCapturedProcess` 在超时/超限强杀后未关闭 `readline` 接口。
- `src/lib/transport/rust-kernel-supervisor.mjs`:存在零引用死导出 `CONTAINER_CLAUDE_BIN`。
- `src/lib/cluster/cluster-routes.mjs`:容器 ID 非法 `%` 编码抛 `URIError` 返回 500;`handleUpgrade` 对非法 URL 未捕获异常;`attachShell` 未监听 `ws`/`stream` `error` 事件存在未捕获崩溃风险。
- `src/lib/cluster/cluster-manager.mjs`:`ensureSlotRelays` 仅一半中继启动失败时未停止已启动的对端中继导致监听套接字泄漏;`startDockerInstall` 缺 `stream.on('error')` 与幂等收尾;`remove` 未清理节点遗留 `prewarmed` 与 shell `tickets`;`ensureLinkReady` 定时器未 `unref`。
- `src/lib/cluster/ssh-link.mjs`:`SshLink._connect` 在 `resolveTarget()` 返回后若代际已变更会泄漏跳板 `target.sock`;`execUnixStream` 250ms 就绪定时器在提前失败时未清理且未 `unref`;`execCollect` 定时器未 `unref`。
- `src/lib/cluster/docker-remote.mjs`:`dockerRequest` 与 `hijackExecStart` 缺幂等收敛、提前 `close` 处理及 `res` 异常时销毁底层流,定时器未 `unref`。
- `src/lib/cluster/direct-link.mjs` & `src/lib/cluster/socket-relay.mjs`:`dialGate` 定时器未 `unref`;`_pipeDirect` 在等待 `ready` 期间本地连接关闭不会立即销毁远端 TLS 套接字。
- `src/lib/cluster/tar.mjs`:存在零引用死导出 `readFirstTarEntry`。
- 改后:
- 补齐上述 HTTP/UDS/Docker/SSH/WebSocket 响应与流的异常/提前关闭幂等收敛、资源释放与定时器 `unref`,阻断 Promise 永久挂起、套接字泄漏与未捕获 `error` 崩溃。
- 删除 3 处确认全仓零生产/零测试引用的死导出(`protoFrontSnapshot`、`CONTAINER_CLAUDE_BIN`、`readFirstTarEntry`)。
- 在 5 个已有单测文件(`respond-sse`、`cluster-edit-routes`、`cluster-bridge-lifecycle`、`docker-memory-api`、`codex-kernel-client`)补充回归用例(0 新建测试文件)。
- 未改 / 延后:
- 开放 PR 碰撞文件按规跳过:`src/lib/transport/scheduler-client.mjs`(PR #2461)、`src/lib/transport/codex-kernel-supervisor.mjs`(PR #2455 / #2447)。
- `src/lib/cluster/remote-slot.mjs` 的 `migrateRemoteServiceContainers` 虽无生产引用,但同组 `migrateRemoteContainer` 仍有 `test/unit/remote-slot-migration.test.mjs` 覆盖,留待确认旧容器迁移窗口结束后一并下线。
- 未做跨协议/RPC 架构调整。
## 验收记录(Run)
- 环境与基线:WSL2 Linux (Node v24.13.0) + Windows git.exe,工作树 `X:\vm2api-wt-r4-cluster`,分支 `fix/r4-cluster-transport`,基线 `origin/dev` (`566e0c1db`,与最新 `origin/dev` `23b1c5423` 零文件碰撞)
- 已跑命令与结果:
- `npx biome check <19 changed files>`: PASS (19/19 files clean)
- `for f in $(find src scripts -name "*.mjs"); do node --check "$f"; done`: PASS (all `.mjs` syntax OK)
- `KIN_SCHEDULER_BIN=/mnt/x/vm2api-wt-gemini-format-parity/bin/kin-scheduler VM2API_TEST_CONCURRENCY=4 npm run test:ci`: PASS (13/13 isolated test files passed)
- `node --test test/unit/respond-sse.test.mjs test/unit/cluster-edit-routes.test.mjs test/unit/cluster-bridge-lifecycle.test.mjs test/unit/docker-memory-api.test.mjs test/unit/codex-kernel-client.test.mjs`: PASS (64/64 tests passed, 0 fail, 0 skip)
- 全库 `git grep -l` 命中全部 77 个关联单测文件(`--test-concurrency=4`):1511 PASS / 2 SKIP / 1 pre-existing baseline failure(`test/unit/kernel-switch-recovery.test.mjs:408` 在 `src/lib/vm/crag-license.mjs:465` 报 `license_unconfigured`,属 darwin 槽加密包基线环境问题,非本次改动范围)
## 回退与手破验证(Broken by hand)
- 临时移除 `src/lib/cluster/cluster-manager.mjs` `ensureSlotRelays` catch 块中的 `entry.kernel.stop()/entry.worker.stop()` -> `ensureSlotRelays stops partially started relay when sibling start fails` FAIL(`AssertionError: Expected values to be strictly equal: 0 !== 2`)-> 恢复后 PASS
- 临时移除 `src/lib/transport/codex-kernel-client.mjs` `codexKernelDescribe` 的 `res.on('error')` 与 `res.once('close')` -> `codexKernelDescribe fails promptly on truncated response body instead of hanging` FAIL(`timeout 3s` 触发 `exit=124`,报错 `Promise resolution is still pending but the event loop has already resolved`)-> 恢复后 PASS(3.7ms)
- 临时移除 `src/lib/cluster/cluster-routes.mjs` 中 `decodeURIComponent(cm[1])` 的 `try/catch` -> `malformed container encoding returns 400 and malformed upgrade URL is ignored safely` FAIL(`AssertionError: Expected values to be strictly equal: 500 !== 400`)-> 恢复后 PASS
## 真实使用(Real use)
- 未执行:本次为集群/传输/HTTP 层资源生命周期与死代码 Clean Code 收敛,按隔离红线未连生产机 `ph24`;已通过真实本地 Unix socket / SSH2 loopback / Docker Engine HTTP mock 端到端覆盖。
## 未运行项(Not run)
- 未跑全量 `npm run test:unit`(556 文件)、E2E 套件、前端 `web/` 构建与线上 `ph24` 验证:本次改动严格限定于 `src/lib/{cluster,transport,http}/` 与 5 个已有 Node 关联单测,已跑完整 `test:ci`(13 文件)及 77 个全仓关联单测文件(1511 pass)。
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 8e73c84d-f90f-4904-97ab-245f415b1de1) |
… Node 22
WHY:
On Node 22, calling .unref() on request/operation-scoped Promise timers
(execUnixStream readyTimer, withDockerDeadline/dockerRequest/hijackExecStart
timers, execCollect timer, dialGate timer, ensureLinkReady timer, forwardApi
retryTimer) allows the event loop to drain before the timer fires when no
other ref'd handle exists, causing CI failures ('Promise resolution is still
pending but the event loop has already resolved') in
cluster-bridge-lifecycle.test.mjs and docker-memory-api.test.mjs.
CHANGES:
- Remove .unref?.() on those 8 operation-scoped timers; every timer is already
cleared via clearTimeout() on settle/abort/close.
VERIFIED:
- node:22-bookworm-slim: cluster-bridge-lifecycle.test.mjs 14/14 PASS (was 11/14, 3 cancelled)
- node:22-bookworm-slim: docker-memory-api.test.mjs 18/18 PASS (was 12/18, 6 cancelled)
- node:22-bookworm-slim: cluster-edit-routes.test.mjs + respond-sse.test.mjs PASS
- WSL native: codex-kernel-client + cluster-edit-routes + respond-sse 32/32 PASS
- npm run test:ci: 13/13 PASS; npx biome check (5 files): 0 fixes
评审结果(head
|
1. 本次改动与原因(Head 2d9c169)修复 #2464 在 GitHub CI(Node 22)下的真实失败(cluster-bridge-lifecycle.test.mjs 与 docker-memory-api.test.mjs 报 Promise resolution is still pending but the event loop has already resolved):
2. 已执行验证与结果
ode --test test/unit/cluster-bridge-lifecycle.test.mjs — 14/14 PASS(修复前 11/14,3 cancelled)ode --test test/unit/docker-memory-api.test.mjs — 18/18 PASS(修复前 12/18,6 cancelled)ode --test test/unit/cluster-edit-routes.test.mjs — 6/6 PASSode --test test/unit/respond-sse.test.mjs — 7/7 PASS
px biome check(5 个改动文件)— 0 fixes 3. 破坏性验证(Broken-by-hand)
4. 未运行项
5. 风险与回滚
|
|
更正上一条里“除这两个文件外没有新增失败”的说法:我随后在同一个 Node 22.23.5 容器里把相关失败文件对 |
|
已在同一 |
验收评论(head
|
变更摘要
src/lib/http/respond.mjs:collectBody未监听aborted与req.complete === false的close,客户端中途断连时 Promise 挂起且监听器未清理。src/lib/transport/api-kernel-client.mjs:apiKernelHealth未消费响应体(缺res.resume());forwardApi重试定时器不响应AbortSignal且超时后仍继续重试。src/lib/transport/proto-front-client.mjs:ensureProtoFront异常拒绝时未清理readyPromises导致缓存永久毒化;postProtoFront缺reserror/close兜底;存在零引用死导出protoFrontSnapshot。src/lib/transport/codex-kernel-client.mjs:codexKernelDescribe缺响应大小上限及reserror/close处理,响应体截断时 Promise 永久挂起。src/lib/transport/rust-kernel-client.mjs:dispatchRustAgentSession响应流提前关闭且未触发end/error时可挂起至 600s 超时,非流式分支收包未续期超时定时器。src/lib/transport/slot-oauth.mjs:spawnCapturedProcess在超时/超限强杀后未关闭readline接口。src/lib/transport/rust-kernel-supervisor.mjs:存在零引用死导出CONTAINER_CLAUDE_BIN。src/lib/cluster/cluster-routes.mjs:容器 ID 非法%编码抛URIError返回 500;handleUpgrade对非法 URL 未捕获异常;attachShell未监听ws/streamerror事件存在未捕获崩溃风险。src/lib/cluster/cluster-manager.mjs:ensureSlotRelays仅一半中继启动失败时未停止已启动的对端中继导致监听套接字泄漏;startDockerInstall缺stream.on('error')与幂等收尾;remove未清理节点遗留prewarmed与 shelltickets;ensureLinkReady定时器未unref。src/lib/cluster/ssh-link.mjs:SshLink._connect在resolveTarget()返回后若代际已变更会泄漏跳板target.sock;execUnixStream250ms 就绪定时器在提前失败时未清理且未unref;execCollect定时器未unref。src/lib/cluster/docker-remote.mjs:dockerRequest与hijackExecStart缺幂等收敛、提前close处理及res异常时销毁底层流,定时器未unref。src/lib/cluster/direct-link.mjs&src/lib/cluster/socket-relay.mjs:dialGate定时器未unref;_pipeDirect在等待ready期间本地连接关闭不会立即销毁远端 TLS 套接字。src/lib/cluster/tar.mjs:存在零引用死导出readFirstTarEntry。unref,阻断 Promise 永久挂起、套接字泄漏与未捕获error崩溃。protoFrontSnapshot、CONTAINER_CLAUDE_BIN、readFirstTarEntry)。respond-sse、cluster-edit-routes、cluster-bridge-lifecycle、docker-memory-api、codex-kernel-client)补充回归用例(0 新建测试文件)。src/lib/transport/scheduler-client.mjs(PR fix(scheduler,gemini): add live snapshot, admission parity gates, and unified Gemini circuit/race switch #2461)、src/lib/transport/codex-kernel-supervisor.mjs(PR fix(scheduler): converge sticky, codex holds, and Claude circuit state onto Rust SSOT #2455 / chore(scheduler): prune pre-M3 dead scheduler modules and Claude circuit panel controls #2447)。src/lib/cluster/remote-slot.mjs的migrateRemoteServiceContainers虽无生产引用,但同组migrateRemoteContainer仍有test/unit/remote-slot-migration.test.mjs覆盖,留待确认旧容器迁移窗口结束后一并下线。验收记录(Run)
X:\vm2api-wt-r4-cluster,分支fix/r4-cluster-transport,基线origin/dev(566e0c1db,与最新origin/dev23b1c5423零文件碰撞)npx biome check <19 changed files>: PASS (19/19 files clean)for f in $(find src scripts -name "*.mjs"); do node --check "$f"; done: PASS (all.mjssyntax OK)KIN_SCHEDULER_BIN=/mnt/x/vm2api-wt-gemini-format-parity/bin/kin-scheduler VM2API_TEST_CONCURRENCY=4 npm run test:ci: PASS (13/13 isolated test files passed)node --test test/unit/respond-sse.test.mjs test/unit/cluster-edit-routes.test.mjs test/unit/cluster-bridge-lifecycle.test.mjs test/unit/docker-memory-api.test.mjs test/unit/codex-kernel-client.test.mjs: PASS (64/64 tests passed, 0 fail, 0 skip)git grep -l命中全部 77 个关联单测文件(--test-concurrency=4):1511 PASS / 2 SKIP / 1 pre-existing baseline failure(test/unit/kernel-switch-recovery.test.mjs:408在src/lib/vm/crag-license.mjs:465报license_unconfigured,属 darwin 槽加密包基线环境问题,非本次改动范围)回退与手破验证(Broken by hand)
src/lib/cluster/cluster-manager.mjsensureSlotRelayscatch 块中的entry.kernel.stop()/entry.worker.stop()->ensureSlotRelays stops partially started relay when sibling start failsFAIL(AssertionError: Expected values to be strictly equal: 0 !== 2)-> 恢复后 PASSsrc/lib/transport/codex-kernel-client.mjscodexKernelDescribe的res.on('error')与res.once('close')->codexKernelDescribe fails promptly on truncated response body instead of hangingFAIL(timeout 3s触发exit=124,报错Promise resolution is still pending but the event loop has already resolved)-> 恢复后 PASS(3.7ms)src/lib/cluster/cluster-routes.mjs中decodeURIComponent(cm[1])的try/catch->malformed container encoding returns 400 and malformed upgrade URL is ignored safelyFAIL(AssertionError: Expected values to be strictly equal: 500 !== 400)-> 恢复后 PASS真实使用(Real use)
ph24;已通过真实本地 Unix socket / SSH2 loopback / Docker Engine HTTP mock 端到端覆盖。未运行项(Not run)
npm run test:unit(556 文件)、E2E 套件、前端web/构建与线上ph24验证:本次改动严格限定于src/lib/{cluster,transport,http}/与 5 个已有 Node 关联单测,已跑完整test:ci(13 文件)及 77 个全仓关联单测文件(1511 pass)。Note
Medium Risk
Touches cluster connectivity, Docker remote API, and kernel forwarding—mostly defensive lifecycle fixes, but regressions could affect node shells, relay startup, or long-running agent sessions.
Overview
Hardens HTTP, Unix-socket, Docker, SSH, and WebSocket paths so abandoned or truncated streams settle once, tear down backing sockets, and stop leaking listeners or half-started resources.
Cluster: Node removal now stops prewarmed links and clears shell tickets; partial slot relay startup rolls back the sibling relay; Docker install jobs finish idempotently and handle stream errors; SSH jump sockets are destroyed when connect is superseded; direct relay pipes destroy the remote socket if the local side closes while waiting on
ready. Panel routes return 400 for bad container ID encoding, ignore malformed upgrade URLs, and route shell ws/stream errors to the client instead of crashing.Transport: Shared settled/finish patterns on
dockerRequest/ exec hijack,readBody(aborted/close), api-kernel forward (abort-aware retries, health body drain), codex describe (size cap + early close), proto-front (promise cache on boot failure, response stream errors), rust agent session (premature close + non-stream timer refresh), and slot-oauth readline cleanup. Removes unused exportsreadFirstTarEntry,protoFrontSnapshot, andCONTAINER_CLAUDE_BIN. Unit tests cover truncated Docker/describe bodies, relay rollback, SSH jump leak, malformed cluster URLs, and aborted request bodies.Reviewed by Cursor Bugbot for commit 2d9c169. Configure here.