Repository navigation
fix: break logger EPIPE feedback loop — async stderr error re-enters uncaughtException (#1233) - #1240
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1240Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1240.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 收到。这是带 |
…uncaughtException (#1233) On Linux, stderr over a pipe is an async stream: EPIPE surfaces as an 'error' event, not a sync throw, so the try/catch around process.stderr.write() never saw it. Unhandled, each event rethrew as uncaughtException, the top-level handler logged it through the logger, and the loop spammed bili.log (77MB in minutes observed) until 10MB rotation wiped the forensic window. - logger.ts: module-init 'error' listener on process.stderr flips logging to file-only once (single [warn]); sync write failures (Windows) take the same path; log() skips stderr after the flip so the logger's error path never re-enters the writer that produced the error. - server.ts: uncaughtException handler logs the first stream-write error and suppresses repeats (belt-and-suspenders for non-logger writers). - tests/logger-epipe.test.ts: spawn-based regression (closed stderr pipe -> no storm, durable record intact, exactly one warn) + predicate unit test. Red on unpatched code, green with the fix.
728f4ac to
d57a087
Compare
[bot] 🏷 Review complete — verified, rebased to current master, mergeable. Per the agent-PR protocol I did not redo the work; I audited code + behavior against #1233 and re-ran everything on the live master. Summary by review gate: ① Direction (issue first). Root cause, not mask ✓. I reproduced the mechanism on unpatched code: piped Linux stderr is an async stream, so EPIPE arrives as an ② Regression review. Inventoried every existing behavior the diff touches:
③ Code. Listener is idempotent ( ④ Tests. Real teeth, independently verified:
⑤ Diff. Single commit, 3 files, 160+/14-, every line in scope; no version/lockfile/generated churn. One action taken: master moved several commits during review (v0.1.145 release, image-compression work landed), so I rebased the branch onto current master ( Minor observation (non-blocking, no change made): the Verdict: root cause correctly identified and fixed at the source, regression test has teeth, no unintended behavior drift beyond what's flagged, clean diff, green on current master. Mergeable. 中文摘要:修复了日志器 stderr EPIPE 反馈环(异步管道的 EPIPE 以 error 事件重抛为 uncaughtException,处理器再写日志形成死循环、数分钟刷爆 77MB 日志并冲掉取证窗口)——根因是缺少 stderr error 监听器,修复在模块初始化时挂监听并一次性降级为纯文件日志(仅一条 [warn]),另在崩溃处理器里对死流写错误做首记后抑制;已独立验证回归测试红绿差异(未修复代码 FIRED 35181 vs 修复后 0)、rebase 到当前 master 后全量测试通过(仅有的 3 个失败为沙箱预存环境问题,与 PR 无关),可以合并。 |
[bot] 🏷 Follow-up: fresh CI on the rebased head |
Fixes #1233
Root cause (verified with minimal repro)
On Linux
process.stderrover a pipe is an async stream: EPIPE surfaces as anerrorevent, not a sync throw, so thetry { process.stderr.write(line) } catch {}guard insrc/logger.tsnever saw it. Noerrorlistener was attached, so each event rethrew asuncaughtException→ the top-level handler (src/server.ts) logged it through the logger → another stderr write → another EPIPE event — a tight feedback loop.Repro A/B (child process emulating the proxy: real logger + same uncaughtException handler shape; parent destroys the stderr pipe mid-run):
Fix (root cause)
Invariant enforced: the logger's error path must never re-enter the writer that produced the error.
src/logger.ts: module-initprocess.stderr.on("error", …)listener flips logging to file-only once, with a single[warn] stderr unavailable (write EPIPE); continuing with file-only logging. Sync write failures (Windows sync stderr) take the same path via the existing catch. After the flip,log()skips stderr entirely — no re-entry, no repeated throws.src/server.ts: belt-and-suspenders — theuncaughtExceptionhandler classifies stream-write errors (isStreamWriteError: EPIPE/EIO/EBADF/ERR_STREAM_DESTROYED), logs the first one, suppresses repeats. Protects against any writer outside the logger hitting a dead stream.Behavior change (intended — flagged per policy)
When the stderr consumer closes (client crash, terminal closed, piped reader exit):
uncaughtException: Error: write EPIPEspam loop — every iteration wrote to the log file (77MB in minutes observed), churning 10MB rotation until the forensic window was wiped; CPU pegged per closed pipe.[warn]line in the log file, then silent file-only logging; the process keeps running normally. Non-logger stream-write uncaughtExceptions: first is logged, repeats suppressed (previously every one was logged).No wire/message-shape change; both compression modes unaffected (logger + crash-handler scope only).
Pre-flight (rebased head on current master)
npm run typecheck— greennpm test— 2397 pass / 0 fail / 2 skipped, except 4 failures intests/claude-native.test.tsthat are pre-existing environment issues (live-ps-walk / spawned-host e2e): verified the identical failure set on pristine master without this PR.npm run build— greentests/logger-epipe.test.ts) verified red on unpatched code, green with the fix.