Skip to content

fix: retry health probe to survive serve startup race - #33

Open
wiyr0 wants to merge 1 commit into
UNLINEARITY:mainfrom
wiyr0:fix/opencode-checkhealth-startup-race
Open

fix: retry health probe to survive serve startup race#33
wiyr0 wants to merge 1 commit into
UNLINEARITY:mainfrom
wiyr0:fix/opencode-checkhealth-startup-race

Conversation

@wiyr0

@wiyr0 wiyr0 commented Jul 11, 2026

Copy link
Copy Markdown

opencode serve binds its TCP port before its HTTP layer is ready, so the single-shot checkHealth fetch could be accepted into the listen backlog and hang until undici's ~300s headersTimeout, surfacing as "Failed to start OpenCode: fetch failed" after ~5min. Retry within a 15s budget with a 3s per-attempt timeout so the startup race self-heals once the server finishes booting.

Summary · 概述

EN:

中文:

wechat-opencode-start 会间歇性卡在 Starting bridge in background...,约 5 分钟后后台 bridge 报 Failed to start OpenCode: fetch failed 退出,前台因 endpoint 永不就绪而无限挂起。

Root Cause

opencode serve 先绑 TCP 端口、后初始化 HTTP 层,存在「端口已通、HTTP 不应答」的启动空窗。而 OpenCodeServerAdapter.checkHealth() 是单次 fetch('/session/status')、无超时无重试:

this.serverPort = await reserveLocalPort();
await this.startServerProcess();            // opencode serve --port P --hostname 127.0.0.1
await waitForTcpPort(HOST, port, 10_000);   // 只检 TCP,端口一绑就返回
await this.checkHealth();                   // 单次 fetch,踩进空窗即挂死

waitForTcpPort 在端口 listen 的瞬间就返回,此时 fetch 的连接被 listen backlog 接走但永远没有响应,一直挂到 undici 默认 headersTimeout(~300s)才以 fetch failed 抛出

复现(spawn opencode serve、TCP 一通就 fetch):

[serve:out] opencode server listening on http://127.0.0.1:40631
fetch FAIL after 15 s : The operation was aborted due to timeout   # 踩进空窗:挂住

Fix

checkHealth()短超时 + 重试,让空窗自动自愈:

  • 每次 fetch 带 AbortSignal.timeout(3_000),踩进空窗最多 3s 中止(而非 5 分钟);
  • 失败后 500ms 重试,总预算 15s,覆盖 opencode serve 的 HTTP 预热;
  • 兼顾原语义:HTTP 非 2xx 仍按失败重试(避免误判 5xx 为就绪)。

新增三个常量在 bridge-adapters.shared.tsOPENCODE_HTTP_READY_TIMEOUT_MS / _PROBE_TIMEOUT_MS / _PROBE_INTERVAL_MS),就近放在既有 OPENCODE_* 常量旁。

Notes

  • 这是间歇性竞态,非必现,但实测命中概率不低。
  • 仓库内同类单次 fetch(如 SSE 连接前的探测)可能也有此问题,建议后续一并核对;本 PR 只修 checkHealth,保持改动聚焦。
  • 改动仅限 2 个源码文件,无运行时状态/依赖变更。

Related issue · 关联 issue(可选): #

Checklist · 自查

  • 本地跑过测试(如 bun test testnpm run lint)

  • npm run typecheck:src

  • npm run lint(全量 eslint bin src test)✅

  • bun test test(全量,451 pass / 0 fail / 1179 expect,22 文件,8.5s)✅

  • 实测打补丁到全局安装目录后,重跑 wechat-opencode-start,companion 正常进入 idle,微信↔本地双向同步恢复。

  • commit 首行是 Conventional Commit(feat: / fix: / docs: / test: / refactor: / build: / chore:) 是

opencode serve binds its TCP port before its HTTP layer is ready, so the single-shot checkHealth fetch could be accepted into the listen backlog and hang until undici's ~300s headersTimeout, surfacing as "Failed to start OpenCode: fetch failed" after ~5min. Retry within a 15s budget with a 3s per-attempt timeout so the startup race self-heals once the server finishes booting.
@wiyr0 wiyr0 changed the title fix(opencode): retry health probe to survive serve startup race fix: retry health probe to survive serve startup race Jul 11, 2026
@wiyr0

wiyr0 commented Jul 15, 2026

Copy link
Copy Markdown
Author

可以合入嘛

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.

1 participant