Skip to content

[PR] fix(#5167): poll the stale-rotation wait on fresh connections so a draining resident reads as released - #5169

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-30_drain-aware-rotation
Oct 30, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-30_drain-aware-rotation

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #5167

什么坏了(一句话)

自动升级换代时,旋转(rotation)等待用连接池里的 keep-alive 连接去问「旧实例还活着吗」,而排水中的旧实例「listener 已关、存量连接照常应答」——于是被误判成「没退出」,12 秒后假失败:dsh 宿主 attach-anyway 骑上将被强退的旧实例、launcher 走 spawn 阶梯,新旧两个进程同时写一份 machine-global 会话存储。

根因(机制链)

  1. 旧实例接受 POST /__bili/shutdown 后执行 server.close():listener 毫秒级释放(口已可被新 child 绑定),但存量 keep-alive 连接继续被服务——这是排水的本意([Bug] Windows: Proxy falsely detects parent-gone and self-terminates on every forwarded request (same live PID triggered | [Bug] Windows:代理每转发一次请求就误判 parent-gone 自杀(同一存活 PID 在 2.5h 内触发 12 次) #3890 后最坏 (1+10)×10s 才退进程)。
  2. 两条旋转等待随即轮询存活:
    • launcher:src/launcher.ts rotateStalePredecessor,每 200ms fetchHealthInfo;
    • dsh pin:src/agent/dsh-native.ts rotatePinnedStaleProxy,每 500ms fetchProxyVersion;
      两者都用 Node 全局 fetch(undici 按 origin 池化),轮询节拍远小于服务端 keepAliveTimeout = 5s,探测永远落在预热过的池化 socket 上 → 排水中的实例「应答如常」。
  3. 12s 预算耗尽 → 假阴性:dsh src/agent/dsh-native.ts:809 attach-anyway;launcher falling back to the normal spawn ladder。实测拓扑(本地探针日志,见 issue):预热探测 + shutdown POST 共用一个池化 socket,该 socket 在 close() 时刻有 in-flight 请求 → 存活并持续应答后续轮询。
  4. 附带修正:STALE_ROTATION_WAIT_MS 的注释(> the responder's 10s force-exit backstop)在 [Bug] Windows: Proxy falsely detects parent-gone and self-terminates on every forwarded request (same live PID triggered | [Bug] Windows:代理每转发一次请求就误判 parent-gone 自杀(同一存活 PID 在 2.5h 内触发 12 次) #3890 后已过时;且预算语义本身错位——口释放信号是 listener 关闭,不是进程退出。

修法

旋转等待涉及的全部请求(含 shutdown POST 自身)改带 Connection: close,每轮全新连接,「应答」重新等于「listener 还在收新连接」:

  • requestInstanceShutdown(launcher.ts):加 connection: close;
  • launcher 旋转等待轮询:新增可注入 dep fetchHealthInfoNoReuse(默认 = fetchHealthInfoDefault 带 noKeepAlive;发现路径 probeLiveInstances 的池化探测不变,零性能影响);
  • fetchProxyVersion(shared.ts):加可选 init 透传(纯 additive,其他调用方不变);rotatePinnedStaleProxy 轮询带 connection: close;
  • 两处 12s 预算注释改为 listener-close 语义;超时日志措辞同步(still accepts fresh connections (listener open))。

范围外(owner 已有待裁决策,本 PR 不碰):新实例绑定后旧实例排水+exit-flush 期间的存储重叠窗(#5068 选项 B / #5018);attach-anyway 决策本身(#4705 相关)。

测试

tests/issue5167-drain-aware-rotation.test.ts,4 例,全部走真实 http 排水服务器(无 fetch mock):

  1. launcher 旋转:排水中 resident(202 + srv.close(),存量连接继续服务)必须被读作「已释放」→ 正常 spawn 接管,drain-wait 有界(≤2 ticks),无「timed out」误报;
  2. launcher 负例:接受 shutdown 但 listener 真不退(wedged holder)→ 仍走满 deadline → 超时日志 + spawn-ladder 回退原样保留;
  3. dsh pinned 旋转:排水中 resident → 返回 true 且毫秒级完成(不再是 12s 假超时);用 undici.Agent({ connections: 1 }) 全局 dispatcher 钉住单连接池化拓扑——修复前该用例确定性红(12.3s,每轮池化轮询都被应答),修复后绿;
  4. dsh 负例:真不退口 → false(用 _setPinnedRotationWaitForTest 缩短 deadline,负例不拖慢 CI)。

既有回归全绿:#3544 旋转套件 20/20、launcher 343/343、dsh 相关 17 个套件 0 失败、npm run typecheck 干净。

…aining resident reads as released

During an auto-update turnover the old resident accepts /__bili/shutdown
and frees its LISTENER within milliseconds, but keeps answering pooled
keep-alive sockets while it drains (worst case (1+10)x10s post-#3890).
Both rotation waits polled liveness with Node's global fetch, whose undici
pool reuses exactly those sockets (poll cadence 200/500ms << the server's
5s keepAliveTimeout), so a draining resident read as alive for the whole
12s budget. The false negative sent dsh hosts into attach-anyway (riding
the doomed old instance while others used the new spawn) and the launcher
into the spawn ladder while the port was actually free — two live
processes writing one machine-global session store.

Every rotation request (shutdown POST included) now sends Connection:
close, so "answers" means "the listener still accepts" — the real
port-release signal; the stale 12s-budget comment is corrected to match
(listener close, not process exit). tests/issue5167-drain-aware-rotation
.test.ts pins both paths against REAL draining http servers (no fetch
mocks) and a single-connection dispatcher that reproduces the pooled
topology: pre-fix it consumes the full 12s deadline, post-fix it returns
in milliseconds; a wedged holder that truly keeps its listener still
times out into the fallback.
@ranxianglei ranxianglei changed the title fix(#5167): poll the stale-rotation wait on fresh connections so a draining resident reads as released [PR] fix(#5167): poll the stale-rotation wait on fresh connections so a draining resident reads as released Oct 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-30_drain-aware-rotation (4246a37)

Latest build — install this exact version

npm install -g billion-context@0.0.6847-pr.5169.09f3af7

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
4246a37 billion-context@0.0.6847-pr.5169.09f3af7 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr5169.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei merged commit a8c907d into master Oct 30, 2026
46 of 49 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.921-master.a8c907d (rolling master tag).

npm install -g billion-context@0.0.921-master.a8c907d  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Oct 30, 2026
…iagnostic text

`d47c6dcf` (ranxianglei#5103, "make startup-failure paths self-contained and actionable")
extended the squatter diagnostic with an actionable tail:

  ... waiting for the spawned instance to report its real origin
  ; if that never happens, kill pid N or set native.attachExternal=true /
  BILI_NATIVE_ATTACH_EXTERNAL=1 to attach to the holder instead

The message moved, the assertion did not: `tests/launcher.test.ts:8191`
still pinned the pre-ranxianglei#5103 string, so the two ranxianglei#3575 tests have failed on
every commit carrying both — master HEAD `6db59068` included.

Verified by reproduction, not by reading the diff:
  * master HEAD 6db5906, tests/launcher.test.ts -> 341 pass / 2 fail
  * pristine upstream/master 27b3155          -> 341 pass / 2 fail
  * both failing with the one-string diff above
  * after this one-line fix                     -> 344 pass / 0 fail

This is one of the fixed failure set ranxianglei#5195 attributes to `ranxianglei#5169`, but the
bisect there has no clean green baseline: `4bb2bc1e`, cited as "fully green",
actually has exactly ONE `test (` check and it is `cancelled` (verified via
`commits/4bb2bc1ed/check-runs`), so its shards never ran. Recording that
separately on ranxianglei#5195.
Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Oct 30, 2026
…iagnostic text

`d47c6dcf` (ranxianglei#5103, "make startup-failure paths self-contained and actionable")
extended the squatter diagnostic with an actionable tail:

  ... waiting for the spawned instance to report its real origin
  ; if that never happens, kill pid N or set native.attachExternal=true /
  BILI_NATIVE_ATTACH_EXTERNAL=1 to attach to the holder instead

The message moved, the assertion did not: `tests/launcher.test.ts:8191`
still pinned the pre-ranxianglei#5103 string, so the two ranxianglei#3575 tests have failed on
every commit carrying both — master HEAD `6db59068` included.

Verified by reproduction, not by reading the diff:
  * master HEAD 6db5906, tests/launcher.test.ts -> 341 pass / 2 fail
  * pristine upstream/master 27b3155          -> 341 pass / 2 fail
  * both failing with the one-string diff above
  * after this one-line fix                     -> 344 pass / 0 fail

This is one of the fixed failure set ranxianglei#5195 attributes to `ranxianglei#5169`, but the
bisect there has no clean green baseline: `4bb2bc1e`, cited as "fully green",
actually has exactly ONE `test (` check and it is `cancelled` (verified via
`commits/4bb2bc1ed/check-runs`), so its shards never ran. Recording that
separately on ranxianglei#5195.
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.

Auto-update on dsh leaves two proxy processes writing one session store: rotation waits mistake a draining old proxy for one that never exited

1 participant