fix: retry health probe to survive serve startup race - #33
Open
wiyr0 wants to merge 1 commit into
Open
Conversation
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.
Author
|
可以合入嘛 |
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.
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')、无超时无重试:waitForTcpPort在端口 listen 的瞬间就返回,此时 fetch 的连接被 listen backlog 接走但永远没有响应,一直挂到 undici 默认headersTimeout(~300s)才以fetch failed抛出复现(spawn
opencode serve、TCP 一通就 fetch):Fix
给
checkHealth()加短超时 + 重试,让空窗自动自愈:AbortSignal.timeout(3_000),踩进空窗最多 3s 中止(而非 5 分钟);opencode serve的 HTTP 预热;新增三个常量在
bridge-adapters.shared.ts(OPENCODE_HTTP_READY_TIMEOUT_MS/_PROBE_TIMEOUT_MS/_PROBE_INTERVAL_MS),就近放在既有OPENCODE_*常量旁。Notes
fetch(如 SSE 连接前的探测)可能也有此问题,建议后续一并核对;本 PR 只修checkHealth,保持改动聚焦。Related issue · 关联 issue(可选): #
Checklist · 自查
本地跑过测试(如
bun test test、npm 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:) 是