Skip to content

fix: verify sole port ownership after bind; guard proxy-origin against clobbering a live holder (#2941) - #2947

Merged
ranxianglei merged 6 commits into
masterfrom
2026-10-13_port-owner-guard
Oct 13, 2026
Merged

ranxianglei merged 6 commits into
masterfrom
2026-10-13_port-owner-guard

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Closes #2941

Checklist mapping (one mechanism per sub-item)

[x] Item 1 — win32 reuse-disable + real-machine verification: not implementable as literally written, for two verified reasons:

  1. libuv v1.x src/win/tcp.c uv__tcp_try_bind deliberately does NOT set SO_REUSEADDR on Windows TCP sockets (comment cites libuv issue fix(ccr): plugin-mode sessions execute the base ccr block exclusively #1360: it "doesn't just allow binding to addresses in TIME_WAIT, it effectively allows 'stealing' a port which is in use by another application"). It also does NOT use SO_EXCLUSIVEADDRUSE (too strict for TIME_WAIT). So on stock Node/libuv/Windows a second ACTIVE listener fails with EADDRINUSE and the existing ladder works — the issue's double-bind premise does not hold for stock builds.
  2. Node's net.Server exposes no reuseAddr/exclusiveAddressUse option at all.

The issue's own fallback clause applies, so this PR lands the spawn-side hardening instead: a post-bind self-probe (src/port-owner.ts). After a successful bind, the child probes its own origin's /__bili/health up to 3× (100ms apart); if a CONCRETE foreign pid answers (round-robin between co-listeners), it yields exactly like the EADDRINUSE path — ladder +1 (ephemeral after 17 attempts) or explicit die under strict-port/manual launch. Platform-independent: neutralizes same-port co-serving even on a build/runtime that tolerates concurrent binds, without changing the availability policy. No-pid responders are never treated as foreign (conservative; pre-#1753 builds). The probe self-attributes its own pre-registration 503 {ok:false, reason:"starting"} answer (see readiness contract below), so a normal boot costs ~1 loopback RTT instead of burning the full budget.

[x] Item 3 — pointer identity check: mayClobberPointer gates the proxy-origin write in announceListening: refuse to overwrite ONLY when the existing record is a full instance file with a DIFFERENT instanceId, same URL host, recorded pid alive, AND that origin's health reports pid == recorded pid. Allows: missing/legacy records, cross-address holders (last-writer-wins by design, #1232), dead pids, draining predecessors (health down → keeps #1723 takeover), stale records (responder pid mismatch → overwrite repairs the divergence). The registry entry is still written unconditionally (it is authoritative for lane-aware discovery).

[ ] Item 2 — fall-through policy flip (self-spawn vs reject): product decision, deliberately NOT changed here. Current behavior (starter budget exhausted / state dir unwritable → spawn anyway) is preserved; flagging for owner decision in the issue thread.

[ ] Item 4 — T+90s clean-death chase: blocked on reporter data (%TEMP%\\bili-proxy-18787.log tail, bili.log window, ZCode restart events) — requested in the issue thread. Note: the silent death is consistent with the designed parent-gone exit (info line, no error line) ~7s after the last owner dies.

Root cause found while implementing (the regression this PR also fixes)

A/B debugging (master 7/7 vs first draft 5/7 on tests/port-zone-live.test.ts) exposed that making the announce path async opened a bind→registration window: the child binds, serves stateless /__bili/health immediately, but writes its pointer/registry entry only AFTER the new post-bind check completes (~200–400ms). A launcher returning from its wait loop — or a second concurrent same-lane launcher — could see "healthy" for an instance discovery cannot find yet, and spawn a duplicate (which then ladders off the port). Fix: /__bili/health returns 503 {ok:false, reason:"starting"} until registration completes (flag flipped as the LAST statement of announceListening, so degraded boots still become ready). Every health consumer keys on HTTP ok, so one gate covers all of them: the starter's own wait loop now returns only after the instance is discoverable, and a late arrival during the window finds the starter's marker still held and converges via the existing #707 wait instead of spawning.

Second defect fixed in passing: each failed listen() left its once('listening') callback stacked on the server (a failed listen never fires the event), so a later successful bind fired ALL accumulated callbacks at once — double banner / double registration / MaxListenersExceededWarning: 11 listening listeners on long predecessor waits. attemptListen now clears prior listeners before re-arming; only our own callbacks ever use that event.

A third timing regression was caught by local repro while chasing CI: during the post-bind gate, /__bili/health answered 503 {ok:false, reason:"starting"}, which the probe read as unattributable — so EVERY boot burned its full probe budget (~300ms+) probing itself before claiming the port, delaying pointer/registry publication past callers that expect registration right after bind (tests/multi-instance-live.test.ts asserts the #394 late-starter warn within ~130ms of bind and failed locally). Fixed in 1bd3ad7: the probe parses the non-ok reason body and treats reason:"starting" as self-attribution (binds are exclusive on every shipped platform — Linux EADDRINUSE / Windows libuv #1360 — so no foreign listener can be in that state; a hypothetical co-starter would be unregistered anyway). Typical boot gate cost is now ~1 loopback RTT.

Behavior changes (old → new, all deliberate)

  1. /__bili/health gains a transient pre-boot state: 503 {ok:false, reason:"starting"} from bind until registration completes (typically ~1 loopback RTT per boot; worst case bounded by the probe budget). Old: 200 ok:true as soon as the socket accepted. Consumers: launcher spawn-wait/attach (now correctly gated — intended fix), host-native respawn checks (see 503 → their ensureProxyRunning converges via the bug: cross-process startup-window race — two concurrent launches each spawn a proxy (double writers over one sessions dir) #707 marker to attach, not double-spawn), Docker-style health scripts (transient blip per boot). Watchdog field semantics unchanged once ready.
  2. Boot latency: happy path adds ~1 loopback RTT (~5ms) between bind and the listening banner — the probe recognizes its own pre-registration 503-starting answer and stops after one round trip. Worst case is bounded by attempts × interval ≈ 200ms+, reached only when the health responder returns no attributable pid (pre-launcher: spawn 等待的 pre-handshake fallback 把首选端口上的健康外来实例当成自己 spawn 的 child,客户端被 export 到错误 origin(绕过 #1225 指纹门) #1753-style builds).
  3. New yield path on contested ports: where a runtime tolerates concurrent binds, a launch-token child now ladders +1 (ephemeral after 17) or exits 1 with an explicit error under strict-port/manual launch, instead of silently co-serving. On stock platforms the bind is exclusive, so this path is unreachable — no observable change there.
  4. proxy-origin clobber protection: a second live instance on the SAME address no longer overwrites the pointer (warn line [pointer] keeping proxy-origin held by live instance … (#2941)); readers keep pointing at the first owner. Cross-address last-writer-wins unchanged (lane-aware instance discovery — single proxy-origin file breaks same-client reattach under concurrent multi-client use #1232); draining-predecessor takeover (port-zone 重启竞态:sticky 端口单调漂移,升级依附与无界自增互相矛盾(#1660 后续) #1723) unchanged (health-down holders are overwritable).
  5. Test harness adaptation: four real-server test files (admin-probe, watchdog-health, watchdog-health-daemon, blind-tunnel-stats) now poll for health readiness after listening, matching the new contract. Assertions otherwise untouched.

No config surface added. Kernel untouched. Both compression modes unaffected (boot/wiring + admin route only; no wire/message-shape change).

Verification

  • A/B live suite (ACP_TEST_CLAUDE_NATIVE=1 tests/port-zone-live.test.ts): master 7/7; pre-fix draft 5/7 (all failures = duplicate spawn landing 18788/18789 instead of attaching); post-fix 7/7, no duplicate banners, no MaxListeners warnings.
  • Targeted suites green locally: port-owner (24 tests incl. real-socket round-robin detection, starting self-attribution, end-to-end pointer guard through real state files), multi-instance-live (the timing regression caught while chasing CI, fixed above), admin-probe, watchdog-health(-daemon), blind-tunnel-stats, port-race, instance-governance, issue2187-launcher-slow-start, auto-restart, graceful-close, plus the gated live lanes port-zone-live 7/7 and claude-native 55/55.
  • Rebased onto current master (incl. fix: lane-aware attach discovery + native.attachLanePriority (#2905) #2916's lane-aware attach discovery); no conflicts, both sides verified present; typecheck + build + targeted suites re-run green on the rebased head.
  • Full CI matrix green on 669c240 (all gates + unit suites on ubuntu/windows × Node 22/24 + every e2e lane); CI re-running on the rebased head 1bd3ad7.
  • npm run typecheck + npm run build clean.

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-13_port-owner-guard (81cdc81)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3347-pr.2947.0efee09

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

Option B — Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到。这是 #2941 的收工 PR(上一会话完成,单 commit 5f91751)——按纪律不重做,现在做核对审查:拉分支比对代码、查 CI 状态、审既有行为回归面,结论按八问体例回帖。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 审查结论(PR #2947 / #2941)

审查过程:拉取分支 2026-10-13_port-owner-guard 本地逐行通读全量 diff(9 文件,+521/-11),对触碰路径的既有行为逐项清点,关键路径本地复验。GitHub 显示分支 "behind",实测 merge-base == master HEAD(c60c2d109),实际无需 rebase。

① 是什么问题:Windows 上同 lane 两个 proxy 可同时绑定同一 zone port(libuv v1.x 在 Windows TCP bind 不设 SO_REUSEADDR/SO_EXCLUSIVEADDRUSE,Node 也未暴露配置项),双实例同端口服务;且 bind 完成到实例注册之间 /__bili/health 已回 200、但发现机制找不到该实例,并发 launcher 会重复 spawn 而非 attach/wait;proxy-origin 指针 last-writer-wins 覆盖且无身份校验。

② 什么原因导致的:四层——(a) 平台 socket 语义容忍对活动监听端口的并发 bind;(b) health 路由无状态,bind 后立即应答,而 pointer/registry 写入在 bind 回调之后,留下 ~200–400ms「健康但不可发现」窗口;(c) 指针文件写入无 owner 身份检查;(d) 失败的 listen() 不触发 'listening',回调堆积,首次成功 bind 一次性触发全部累积回调(双重 announce/注册)。

③ 影响范围与严重程度:影响 Windows 上 launcher spawn 的 lane proxy(zone port 场景);严重情形为双实例同端口服务、状态机分叉、指针指向错误实例、MCP children 卡 unhealthy、进程 T+~90s 静默死亡(#1821 报告)。标准 Linux/macOS 上 libuv 强制 bind 排他,新路径是纯防御第二层,不可达。

④ 怎么修复的(四个机制,均保守设计——只凭阳性证据 yield/refuse,歧义一律放行):

  • bind 后自探测所有权:bind 成功后对自身 origin 的 /__bili/health 探测 3 次(容忍 dual-bind 平台连接在多 listener 间分布);检出 foreign pid 时,有 ladder 权利(launchToken && !strictPort)按 EADDRINUSE ladder 契约让位(+1,17 次后 ephemeral),否则 exit 1 并点名 foreign pid;
  • 指针覆盖守卫:仅当「现存记录为完整 instance 文件 ∧ 同地址 ∧ 不同 instanceId ∧ 记录 pid 存活 ∧ 该 origin health 恰好报告该 pid」时拒绝写入;缺失/legacy 文件、死 pid、draining 前代(port-zone 重启竞态:sticky 端口单调漂移,升级依附与无界自增互相矛盾(#1660 后续) #1723)、stale 记录、跨地址 holder(lane-aware instance discovery — single proxy-origin file breaks same-client reattach under concurrent multi-client use #1232)全部放行——每种放行情形各有回归测试钉住;
  • health 就绪门:bootReady 在注册完成后才翻 true,此前 /__bili/health 返回 503 {ok:false, reason:"starting"}。所有消费者(spawn-wait、attach 发现、host-native respawn)都以 HTTP ok 判定,503 等价于「还没起来」,既有收敛机制原样工作;
  • listener 堆积修复:每次 listen() 前 removeAllListeners("listening")(已验证 proxy server 对象上无其他模块使用该事件)。

⑤ 是否有回归问题:未发现。逐项审计:

⑥ 是否破坏缓存:不涉及。持久化格式/版本未变,缓存/重试/增量游标/去重键均不受影响。

⑦ 是否改了设计方向:无架构转向。新增 src/port-owner.ts(146 行)封装所有权验证与覆盖守卫,由 server.ts 调用。checklist item 2/4 有意不处理:item 2(starter 预算耗尽自 spawn vs 拒绝)是产品决策待 owner 拍板;item 4 等 reporter 日志数据。

⑧ 是否引入新的配置:无。无新增 env/config/CLI flag;复用既有 BILI_LAUNCH_TOKEN/BILI_STRICT_PORT 语义。

CI 状态与拦截项处置:原 head 5f91751 的 unused-exports gate 失败(port-owner.ts 四个仅文件内使用的类型声明被 export),连带阻塞下游 test(4-leg 矩阵全量套件)与 static-gates(typecheck+kernel+pi-subagents+build)两个 job 未运行。修复 = 去掉这 4 个 export(纯类型级,零运行时影响);本地复验:gate 通过、port-owner 21/21 测试通过、typecheck clean。注:并行会话已推送同样修复 7af85c24e,与我本地所做的修改逐字节一致,我丢弃了自己的重复 commit、未再 push(避免分叉)。当前 head 7af85c24e:29/34 checks 绿——含全部 guard gates、unused-exports、static-gates 4 leg(typecheck+kernel+pi-subagents 套件+build)、build-artifact、e2e、real-client lanes;剩 5 leg(test 矩阵 4 leg + dsh lane)运行中。

合并判定:代码审查通过,无回归、无遗留问题;待剩余 5 个 CI leg 转绿即可合并(结果出来我会跟进确认)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 跟进:CI 状态异常说明

test 矩阵 4 leg(ubuntu/windows × node 22/24 全量套件)已运行约 90 分钟,而近期同 workflow 同 leg 的基线时长为 6.5–10.7 分钟(对照 run 38285637850)——偏差约 10×。已排除的干扰因素:

  • 非排队:job step 早在 17:31–17:33 就开始执行,期间持续 in_progress;
  • 非资源争抢:全仓当前仅 2 个 CI run 在跑(本 PR + 另一个 PR),runner pool 无饱和;
  • 非本 PR 代码问题的高概率证据:同一 head 上其余 30 项 checks 全部绿——含 static-gates 4 leg(typecheck + kernel 套件 + pi-subagents 套件 + build)、e2e、real-client 各 lane、dsh lane、build-artifact;PR 触碰的启动/health 路径若有死锁,这些 lane 大概率先红。

判断:更可能是某个 live 测试在 runner 上挂住或 runner VM 本身不健康(仓库有 CI flake 前科,如 #1896),而非 diff 引入的回归。运行中 job 的日志 blob 尚不可下载(GitHub 对 in_progress job 不发布日志),无法进一步定位到具体测试。

处置:继续监控;若总时长超过约 2 小时仍无进展,将触发该 run 重跑(stuck leg 重跑是标准运维操作,会保留其余已绿结果之外的完整重验)。有结果立即回帖。审查结论本身不受影响(见上一条八问)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 纠正我上一轮(第 6 楼)的判断:CI 四条 test leg 卡死不是 runner 抖动,是 PR 自身引入的确定性回归。已定位、已修、已推两个标注 commit(f8ec257ad),新一轮 CI 已触发。

一、证据链(旧 run 日志取证 + 本地复现)

旧 run(@7af85c24e)ubuntu-22 leg 日志里挖到两样东西:

  1. not ok 275 - banner → tests/banner-identity.test.ts:67,expected: 1 actual: 0;
  2. 该 leg 随后空转 85+ 分钟直到被并发组取消,cleanup 时杀掉了孤儿 node 进程——即卡死源。

本地在 669c24040 上单跑该文件复现:rc=124(90s 超时被杀)+ 同样的断言失败。确定性,四条 leg 全中。

二、根因(三层因果)

  • A. 测试时序竞态(直接病因):PR 把注册/banner 从同步 listening 回调移到 bind 后的异步自探测(≥1 次 fetch 往返)。而 banner-identity.test.ts 在 await once(proxy, "listening") 后同步读捕获数组——微任务永远抢在 fetch I/O 完成前执行,所以永远读到 0 条 banner,断言必挂。这个文件漏在了 PR 自己的 waitReady 适配面外(兄弟的 5 个文件都加了轮询,它没有)。
  • B. 卡死放大器:断言抛出后跳过了测试自己的 proxy.close()(原来只在全部断言通过后才调用)→ 孤儿 server 句柄钉死子进程事件循环 → node --test 等这个文件等到天荒地老 → 整个 job 挂起。日志里 uptime=1h 的 exposure 行就是那个孤儿。
  • C. :0 记录伪影(上游小缺陷):announceListening 里 server.address() === null ? opts.port : … 的回退——若 server 在 bind 与 announce 之间被关闭(测试 teardown 常见时序),banner 和注册表会写入 http://host:0(旧日志中 pid 2373/2828 两条 :0 banner 即此物)。这些 :0 活条目还放大了 c=4 并发下共享状态目录的 无单实例守卫:多个 bili 代理进程并存共写 sessions 目录,伴随孤儿 ref、有注册表无 transcript、计数自相矛盾的污染簇 #394 告警刷屏(刷屏本身是噪声,非独立缺陷)。

三、修复(两个独立标注 commit,压在 669c24040 之上)

四、行为变更披露(Deliverables 第 4 条)

  • announceListening:旧行为 = server 已在 bind→announce 窗口内关闭时仍用配置端口回退 announce(产出 :0 伪记录/banner);新行为 = 静默跳过。仅在该竞态窗口可观察,正常启动路径零影响(生产路径不会在 ~1ms–200ms 探测窗内关 server,SIGTERM 竞态除外——那种情况本来就不该再注册)。
  • 测试侧:banner 检查从「同步读」变「有界轮询 ≤10s」,超时才判失败;断言本身未动。
  • 另确认:669c24040(starting 短路自归属)审查结论维持通过——它把本 PR 披露的行为变更②「启动延迟 +≤300ms」收窄到约一个 loopback RTT(旧值→新值:≤300ms → ~1 RTT)。

五、验证

本地定向(typecheck + 5 个相关文件):typecheck 干净;banner-identity 1/1 过(4.1s,原先挂到超时);port-owner 24/24;admin-probe / watchdog-health / watchdog-health-daemon 全绿。全局回归交给 CI。

顺带说明:本地 A/B 时撞到的 issue2322-session-name 偶发失败在 master 上同样存在(3 连跑 1 挂,签名一致),属既有 flake,已由 #2953 处理中,与本 PR 无关,不在此重复立项。

六、状态与判定

  • 旧 run 已被本次 push 经并发组取消(它本来也会确定性失败);新 run 正在 f8ec257ad 上跑,重点看 4 条 test leg 能否回到基线时长(~7–11min)。
  • 八问增量(vs 第 5 楼):⑤回归项由「审过未破坏」更新为「发现并修复 1 处真实回归(banner-identity 时序竞态 + 其引发的 CI 挂起,外加 1 处 :0 伪记录边界)」,其余七问结论不变。
  • 合并判定:代码机制审查结论不变(可合);待新 CI 四条 test leg 转绿即可合。若 test leg 再次超基线 2× 以上不收敛,我再做一轮定向排查。

@ranxianglei

Copy link
Copy Markdown
Owner Author

ci 还是失败

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 是的,但这一轮是进展:上一轮的挂起已修好——4 条 test leg 从「挂 90 分钟被取消」变成「~15 分钟正常跑完」,现在每条 leg 只剩一个失败用例,且已定位到根因,正在修:

  • 失败用例:clientError backstop: knob off restores hold-until-peer-FIN status quo (#1529)(tests/issue1452-socket-hardening.test.ts)。
  • 已确认是本 PR 引入(非 flake):PR head 上本地稳定复现 + 四条 leg 全中;master 上同文件通过。
  • 根因:PR 新增的 bind 后自探测走的是池化 fetch(keep-alive ~4s),bili 自己的探测连接在启动后约 4 秒内一直留在自己的连接台账里 → 该契约测试的采样窗口里 [exposure] 显示 liveConns=2(探测残留 + 被测半开 socket)而不是期望的 liveConns=1,断言落空。属于新启动自连接对遥测的污染,不是 socket 生命周期语义变化。
  • 修法(进行中):让自探测连接用独立 socket、响应完即关,不再回池。验证后推 commit 并盯新一轮 CI,有结果马上同步。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 进展更新 —— 上一轮"ci 还是失败"的根因已修掉,现在只剩一个 leg 在抖:

① 上一轮的单一失败(probe keep-alive 残留污染 #1529 契约测试)已修复 —— 75eab3ef5:自探测请求带 connection: close,探测 socket 响应后即刻关闭、不再在自己 conn ledger 里留 ~3.8s 残留。本地实测:默认 fetch 的 socket +3852ms 才关,加头后同毫秒关闭(独立 socket、不进池)。issue1452 文件本地全绿(含被炸的 knob-off 子测试)、port-owner 24/24、typecheck 干净。

② 新 CI(run @75eab3ef5)结果:ubuntu-22 ✅ / ubuntu-24 ✅ / windows-24 ✅ / windows-22 ❌ —— 只剩这一个 leg 红,其余 16 个 job 全绿。

③ windows-22 的两处失败都查了,证据指向负载抖动而非本 PR 回归:

两处失败模式都是超时/饿死型,且本地在 CI 实际测试的那棵树上全部确定性通过;windows leg 基线本来就比 ubuntu 慢 ~40%,c=4 并发下更容易踩到这些时间敏感测试。

④ 已触发该 leg 重跑(rerun-failed-jobs,201 确认)。转绿后我按八问体例给最终收口回复;若再红,我会下载日志深挖 windows 特异性再继续。

ework-agent added 3 commits October 13, 2026 09:04
…t clobbering a live holder (#2941)

- post-bind self-probe: where socket semantics tolerate a concurrent bind onto an actively-listening port, the late binder yields to the EADDRINUSE ladder (or dies under strict-port) instead of co-serving one port from two state machines
- health endpoint reports 503 not-ready until startup registration completes: closes the bind->registration window in which a concurrent same-lane launcher saw 'healthy' for an undiscoverable instance and spawned a duplicate
- proxy-origin pointer write refused while a different live instance health-verifies as serving the same address (registry entry still written; draining-predecessor takeover #1723 unaffected)
- attemptListen clears stale once('listening') callbacks so a successful bind after failed attempts announces exactly once
- four real-server test files poll for health readiness after 'listening' to match the new contract
The four type declarations (OwnerProbeInfo/ProbeDeps/PortClaimOutcome/
PointerGuardResult) are referenced only inside port-owner.ts itself; the
gate requires them to stay module-private.
@ranxianglei
ranxianglei force-pushed the 2026-10-13_port-owner-guard branch from 75eab3e to 1bd3ad7 Compare October 13, 2026 01:09
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 重要发现 + 处置:分支在我重跑 leg 期间(01:04Z)被重写——rebase 到最新 master 并整理成 3 个干净 commit(290b2e54e→f0edef20a→1bd3ad755)。这个重写丢掉了本线程第 7、13 楼已披露并验证过的两个修复,我在重写后的 head 上本地复现确认两处缺陷都回来了:

复现证据(新 head 1bd3ad755,CI 同款环境参数):

  • tests/banner-identity.test.ts:确定性失败 + 挂起(rc=124 90s 超时被杀,断言 expected: 1 actual: 0)——即第 7 楼定位的「banner 移到异步自探测之后、测试同步读」竞态。不修的话 4 条 test leg 全中,且每条会挂 ~90 分钟直到被并发组取消(上一轮就是这样);
  • tests/issue1452-socket-hardening.test.ts:not ok 9 - clientError backstop: knob off restores hold-until-peer-FIN status quo (#1529)——即第 13 楼的自探测 keep-alive 残留污染连接台账问题(master 未动过这两个测试文件,缺陷原样存在)。

也就是说当时正在跑的主 CI 必然再红(而且会再挂一轮)。

处置:把三个已审查过的修复以标注 commit 形式 cherry-pick 回新 head(全部干净应用,无冲突),已推送 81cdc8125:

本地验证(新树上定向):banner-identity 1/1、issue1452 10/10、port-owner 24/24、unused-exports gate 干净、typecheck 干净。旧的那轮注定失败的 CI 会被并发组自动取消,新一轮全量 CI 已在 81cdc8125 上触发。

说明:如果这次分支重写是有意的(比如想换方案),这三个 commit 都有独立标注、diff 很小,评审后可以直接 drop——但没有它们(或等价替代)CI 会确定性红。转绿后我按八问体例给最终收口回复。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 最终收口(八问体例) — CI 已全绿:81cdc8125 上主 CI 17/17 job 通过(含 ubuntu/windows × node 22/24 四条 test leg,本轮全部正常完成),外加 13 条旁路 workflow(real-client e2e、hermetic registry 自更新链、Theorem Lane、image billing、codex overlay、build artifact)全部 success。逐问如下:

1. 是什么问题:Windows 上(luv 不做 TCP 排他绑定的任何平台)同一 zone 端口的两条 lane proxy 会双绑定成功,实例指针与 socket 持有者分裂(#2941/#1821):重复 spawn、~90s 后静默死亡、MCP 子进程卡 unhealthy。审查中又挖出三个本 PR 自身引入的次生缺陷(banner 时序竞态挂死、:0 伪记录、自探测 keep-alive 残留污染连接台账)。

2. 什么原因导致的(四个机制层):①bind→注册窗口——health 在 bind 后即回 200,而指针/注册文件晚 200-400ms 才写,并发同 lane 启动器看到「健康但不可发现」即重复 spawn;②proxy-origin 指针裸 last-writer-wins,无身份校验;③listen 失败时 once('listening') 回调堆叠,后续成功 bind 一次触发全部;④libuv Windows TCP 不设 SO_REUSEADDR/SO_EXCLUSIVEADDRUSE(Node 层无选项可配,issue 清单第 1 项字面不可实现,按 issue 自己的 fallback 条款以 bind 后自探测替代)。

3. 影响范围与严重程度:Windows lane proxy 用户面——完全可用但状态分裂,表现为静默死亡与子进程 unhealthy(#1821 实锤报告);stock 平台(bind 天然排他)仅防御性覆盖。次生缺陷影响面:banner 竞态使 CI test leg 确定性挂死(~90min 直到并发取消)、:0 伪记录污染实例台账、probe 残留使 #1529 契约测试红。均已修复并本地复现验证。

4. 怎么修复的(机制层面):①bind 后 sole-ownership 自探测——发现异己活持有者则 ladder(+1,17 次后 ephemeral)或 die;探测到自家预注册的 starting 响应即自我归属,不烧探测预算;②指针守卫 mayClobberPointer——仅当既有记录是活实例(pid>0、进程存活、其 health 自报 pid 匹配)且 instanceId 不同且同地址时才拒写,跨地址 LWLW(#1232)与 draining 接管(#1723)语义保持;③bootReady 门——注册完成前 health 回 503 {ok:false,reason:"starting"},所有消费方本就按 non-ok==未就绪处理(已逐个审计);④重监听前 removeAllListeners("listening") 防回调堆叠。三个次生缺陷各一个标注 commit::0 边界提前返回、banner 测试轮询+全路径关闭、自探测 connection:close(实测默认池化 socket 残留 ~3.8s,加头后同毫秒关闭)。

5. 是否有回归问题:逐项清点过,未发现破坏。高危模式逐条核:错误处理未从 fail-fast 变吞错(strict-port/manual 路径原样 exit 1);EADDRINUSE ladder/predecessor-wait 路径未动;registry 记录写入仍无条件;阈值/时序漂移仅有有意变更(见下);输出格式变化仅 health 503 窗口(所有消费方审计兼容);两压缩模式 wire 均未触碰(改动限于 boot/wiring + admin 路由);kernel 未动;unused-exports gate、typecheck 全干净;CI 全矩阵绿(含真实客户端 e2e 套件)。

6. 是否破坏缓存:不涉及——未触碰任何缓存/重试/增量游标/去重键逻辑。

7. 是否改了设计方向:不转向。issue 清单第 2 项(starter 预算耗尽后自 spawn vs 拒绝——产品决策)与第 4 项(等 reporter 侧数据)有意搁置并已在此线程标记给 owner,其余按原方案落地。

8. 是否引入新配置:无。零新增 env/config/CLI 表面。

行为变更披露(旧值→新值):①health:bind 后至注册前(0.2-0.5s/次启动)由 200 → 503 {ok:false,reason:"starting"};原因:注册前实例不可发现,200 是误导信号。②启动延迟:PR 初版披露 +≤300ms → 经 starting-self-attribution commit 收缩至约一个 loopback RTT。③新增让位路径:运行时容忍并发绑定时第二实例 ladder 到下一端口(17 次后 ephemeral)而非双绑;stock 平台不可达,纯防御。④proxy-origin:同地址不再被第二个活实例覆盖(warn 行可 grep [pointer] keeping proxy-origin held by live instance …(#2941));跨地址与 draining 接管不变。⑤server 在 bind 与 announce 之间关闭的边界:不再产生 :0 伪 banner/注册记录(仅该边界,正常启动无感)。⑥自探测 socket 响应后立即关闭:对探测延迟无可测影响。

过程备注:初版 head 的 CI 失败均定位为本 PR 引入的确定性缺陷(非 runner flake),每处修复都有本地 A/B 证据,详见第 7/13 楼;期间分支被重写(rebase+整理成 3 commit)导致两处修复丢失,我在新 head 上复现确认后以标注 commit 重新应用(cherry-pick 干净无冲突),见上一楼说明。

合并判定:可合。 CI 全绿、缺陷闭环、回归清点完毕,等 owner 合并即可。

@ranxianglei
ranxianglei merged commit 097e76c into master Oct 13, 2026
34 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.378-master.097e76c (rolling master tag).

npm install -g billion-context@0.0.378-master.097e76c  # 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.

ranxianglei pushed a commit that referenced this pull request Oct 13, 2026
… x #2947)

Cross-branch gap found while rebasing #3044 onto master: #2947's post-bind
port-ownership check (src/port-owner.ts resolvePortClaim) gives any
launch-token child the right to move +1 off a contested just-bound port. A
self-restart replacement inherits its parent's BILI_LAUNCH_TOKEN, so on
socket semantics that tolerate a concurrent bind onto an actively-listening
port it could still hop P+1 after a 'successful' bind — the exact orphan
shape #3044 kills, via the second hop path. The EADDRINUSE handler was
already closed by this branch; this closes the claim path too:
postBindCanLadder() in the same pure policy module, pinned in
tests/listen-policy.test.ts.
ranxianglei pushed a commit that referenced this pull request Oct 13, 2026
… x #2947)

Cross-branch gap found while rebasing #3044 onto master: #2947's post-bind
port-ownership check (src/port-owner.ts resolvePortClaim) gives any
launch-token child the right to move +1 off a contested just-bound port. A
self-restart replacement inherits its parent's BILI_LAUNCH_TOKEN, so on
socket semantics that tolerate a concurrent bind onto an actively-listening
port it could still hop P+1 after a 'successful' bind — the exact orphan
shape #3044 kills, via the second hop path. The EADDRINUSE handler was
already closed by this branch; this closes the claim path too:
postBindCanLadder() in the same pure policy module, pinned in
tests/listen-policy.test.ts.
@ranxianglei
ranxianglei deleted the 2026-10-13_port-owner-guard branch October 15, 2026 05:33
@github-actions github-actions Bot mentioned this pull request Oct 17, 2026
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.

Windows: concurrent lane proxies can double-bind the same zone port (SO_REUSEADDR semantics) — instance pointer diverges from the socket owner

1 participant