Repository navigation
feat: terminal backstop for the clientError drain path (#1529) - #1530
Conversation
[bot] 🏷 收到,正在审这个 PR。已确认:#1529(原始 issue)→ 本 PR( |
…ault per docs (review of #1530) The parse mapped negative integers to 0 (disabled) via Math.max(0,x), while the PR body and CONFIGURATION.md (en+zh) all state non-numeric/negative -> default 30000. Align code with the documented contract: invalid input must not silently disable the protection, and it matches the sibling BILI_KEEP_ALIVE_TIMEOUT_MS shape (non-positive -> fallback). 0 remains the explicit opt-out. Adds a pinning test (startup-line echo of the resolved value); verified it fails against the pre-fix parse.
[bot] 🏷 Review 完成 — 可合并(已直接在分支上补了一个小修 commit ① 原 issue(#1529)方向核对#1529 给了三个选项,选项 1(建议项)=「bail 后 N 秒无 peer FIN → destroy + 独立 ledger reason + env knob,沿用 #1528 opt-in 模式」。本 PR 逐条落实: ② 回归审查(既有行为清点)
③ 代码审查主发现(已在分支上修复)负值解析与文档契约不符:代码 其余逻辑审查无异议:timer 均 unref(不延长进程生命周期); ④ 测试有效性两个新测试都是真测试:钉住 knob 接线(startup 行)、drain-FIN 先行顺序、新分类、warn marker、peer 无 RST、knob-off 时 socket 仍被持有( ⑤ diff 整洁度4 文件 +165/−11,每行都与本 PR 目的相关;无空行/重排噪音、无 lockfile/生成物 churn;顺带文档补漏是独立标注 commit。✅ 验证证据(head
|
|
Deep review complete (the #1529 leg; #1528 reviewed separately) — mergeable, stacked cleanly on #1528's head. Verified:
Non-blocking gap (noted, not fixing here): removing the Merge after #1528 — the stack order is load-bearing. |
[bot] 🏷 收到,结论一致 —— 你审的 head( 两点收尾对齐:
中文摘要:双方深审结论一致(可合并,#1528 先行),drainArmed 测试缺口双方均定性为非阻塞且难以确定性触发,待 #1528 合入后我可代做 rebase 与全套重跑。 |
#1452 item 1 replaced the clientError socket destroy with drain-then-close (resume() + end() on end + 300ms unref'd bail end()). Residual edge found in review: if the peer NEVER sends FIN after the bail (adversarial / slowloris-style: parse-garbage then silence), the socket stays half-open on our side indefinitely — the keep-alive reaper keys off completed responses and Node enables no SO_KEEPALIVE by default. Each such socket holds a connRecords ledger entry plus kernel buffers. Second-stage backstop (issue option 1): after the bail end(), arm an unref'd timer for BILI_CLIENT_ERROR_BACKSTOP_MS (default 30000; 0 restores the hold-until-peer-death status quo; non-numeric/negative fall back). On fire, if the socket is still alive and the peer FIN never arrived, destroy it. - Safe against the #1452 RST signature: resume() has drained the recv buffer for the whole window, so the destroy carries no unread residual bytes. - Distinct ledger reason clienterror-backstop (checked first so a racing error event cannot mask it) + its own warn marker line. - Idempotence: a second clientError on the same socket cannot stack listeners or timers (drainArmed guard on ConnRecord). - Behavior change: old = half-open clientError sockets held until peer death; new = destroyed ~300ms + 30s after clientError unless the peer FINs first. The common case is byte-for-byte unchanged (well-behaved peers FIN within ms of our drain-FIN and get the clean FIN as before). - Config surface: one new env var, documented in CONFIGURATION.md en+zh. Tests (tests/issue1452-socket-hardening.test.ts): adversarial silent peer (raw socket with allowHalfOpen:true — the only pure half-open holder) pinned to reason=clienterror-backstop with the drain-FIN-first ordering asserted; knob-off test proves status-quo hold (exposure liveConns=1 past the would-be backstop moment) then clean server-end close on peer FIN.
…ault per docs (review of #1530) The parse mapped negative integers to 0 (disabled) via Math.max(0,x), while the PR body and CONFIGURATION.md (en+zh) all state non-numeric/negative -> default 30000. Align code with the documented contract: invalid input must not silently disable the protection, and it matches the sibling BILI_KEEP_ALIVE_TIMEOUT_MS shape (non-positive -> fallback). 0 remains the explicit opt-out. Adds a pinning test (startup-line echo of the resolved value); verified it fails against the pre-fix parse.
ff3a687 to
8c7b636
Compare
|
Conflict resolved — rebased onto master (new head, 3 commits). Root cause of the conflict was benign: #1528 picked up the zh-CN doc rows as a review fix (
Post-rebase verification: env rows appear exactly once per language (3 #1452 rows + 1 backstop row, en+zh); hardening suite 11/11 (now includes the backstop tests); typecheck clean. PR base retargeted |
Closes #1529
Follow-up to #1452 / PR #1528 item 1 (stacked on
2026-09-27_mitm-rst-idle-hardening, verified current at push time).What
#1528 item 1 replaced the clientError socket destroy with drain-then-close (
resume()+end()onend+ 300ms unref'd bailend()). Residual edge found during review of that work: if the peer NEVER sends FIN after the bail (adversarial / slowloris-style: send parse-garbage, then go silent), the socket stays half-open on our side indefinitely — the HTTP keep-alive reaper keys off completed responses and Node does not enable SO_KEEPALIVE by default. Each such socket holds aconnRecordsledger entry plus kernel buffers.This adds the second-stage backstop (issue option 1):
end(), an unref'd timer arms forBILI_CLIENT_ERROR_BACKSTOP_MS(default30000;0restores hold-until-peer-death; non-numeric/negative → default). On fire, if the socket is still alive and no peer FIN arrived, it is destroyed.resume()-drained for the whole window, so the kernel recv buffer is empty — the destroy carries no unread residual bytes and cannot surface as the [0.1.158] MITM 链路上客户端侧疑似闲置连接拆除 RST+上游短周期停滞无看门狗——附一次客户端进程崩溃的完整取证,请评估是否需要加固 #1452 incident signature (destroy-with-residual → RST). From FIN_WAIT_2 with an empty queue the close is quiet (no RST to the peer); the backstop frees our fd + ledger entry, which is exactly the retention the issue bounds.clienterror-backstop, classified first so a racing error event cannot mask it; distinct warn marker[conn#N] clientError backstop: ….clientErroron the same socket early-returns (drainArmedguard on ConnRecord) — no stacked listeners/timers.Behavior change (flagged)
Config surface
One new env var:
BILI_CLIENT_ERROR_BACKSTOP_MS(default30000,0disables, garbage → default — same parsing shape as the sibling #1452 knobsBILI_KEEP_ALIVE_TIMEOUT_MS/BILI_EXPOSURE_LOG_INTERVAL_MS). No config-file field, no CLI flag: this is a process-level connection-lifecycle budget like its siblings, and the existing env-var convention is the established home for these (the providers table and the three-levelcompresshierarchy do not express socket-lifecycle budgets). Documented in CONFIGURATION.md en + zh.Regression review (existing behaviors touched)
drainArmed,backstopAt, null/false-initialized); classification prependsclienterror-backstopbeforeerrored— can only reclassify sockets whose backstop actually fired (backstopAtis set exclusively by the backstop timer), all other reasons unchanged.[conn] keepAliveTimeout=…msextended with aclientErrorBackstop=…mssuffix — additive; existing test greps the prefix substring and still passes.Tests
Two new regression tests in
tests/issue1452-socket-hardening.test.ts:allowHalfOpen:true— the only pure half-open holder; plain net sockets auto-end their write side upon receiving our FIN) → drain FIN observed first, then server ledgerreason=clienterror-backstop+ warn marker, no peer-side error (no residual bytes → no RST).0) → socket still held past the would-be backstop moment (exposureliveConns=1), no backstop line, then peer FIN → cleanreason=server-end(status quo proven end-to-end).In-passing fixes (separate commits, one root cause)
The zh env table had drifted behind the English table (rows added en-only). Backfilled into CONFIGURATION.zh-CN.md at the matching table positions, each as its own labeled commit:
847b1342— the three [0.1.158] MITM 链路上客户端侧疑似闲置连接拆除 RST+上游短周期停滞无看门狗——附一次客户端进程崩溃的完整取证,请评估是否需要加固 #1452 hardening rows (efaddeb landed them English-only).bdad624a— theACP_RENDER_NONErow (opencode 插件模式下 render tag 注入消息正文,模型回显漏网后原样渲染进 TUI(元数据与正文同层问题) #933), found while verifying full en/zh parity after the first backfill.Pre-flight (head of current base branch)
npm run typecheck✅npm test✅ 3011 pass / 0 fail / 12 skippednpm run build✅ (dist-import-annotations OK)