Skip to content

fix(logger): stop console writes after EPIPE to prevent crash loop - #209

Open
iagorobo24-hub wants to merge 1 commit into
grinev:mainfrom
iagorobo24-hub:fix/logger-epipe-guard
Open

fix(logger): stop console writes after EPIPE to prevent crash loop#209
iagorobo24-hub wants to merge 1 commit into
grinev:mainfrom
iagorobo24-hub:fix/logger-epipe-guard

Conversation

@iagorobo24-hub

Copy link
Copy Markdown

Description of changes

When the console pipe is closed (e.g. npm start | head, terminal closed, --daemon detached), every console.write raises EPIPE. Without an error listener on stdout/stderr, Node escalates it to an uncaught exception — whose handler logs through the same console, producing an unbounded crash loop (a log file once grew to 2 GB).

The fix:

  • Attaches error listeners to process.stdout/process.stderr that mark console output as broken on EPIPE (installed once, guarded by a global flag).
  • Routes all logger console writes through writeToConsole(), which stops console output once broken.
  • File logging keeps working normally — only the console side is disabled.
  • __resetLoggerForTests() clears the broken flag between tests.

Closes issue (optional)

  • Not linked to a GitHub issue. Root cause discovered locally after observing runaway log growth.

How it was tested

  • New unit test in tests/utils/logger.test.ts: emits a synthetic EPIPE on process.stderr, then verifies file logging continues (INFO, WARN, ERROR all present in the log file) while console.warn/console.error are never called again.
  • Full suite passes: 1561 tests, npm run lint, npm run typecheck, npm run build all green.

Checklist

  • PR title follows Conventional Commits: fix(logger): stop console writes after EPIPE to prevent crash loop
  • This PR contains one logically complete change
  • Branch is rebased on the latest main
  • I ran npm run lint, npm run build, and npm test
  • If this PR is OS-sensitive, behavior/limitations for Linux/macOS/Windows are described

OS note: EPIPE handling is standard Node.js stream behavior and behaves identically on Linux, macOS, and Windows. On Windows, process.stdout/process.stderr may be virtual pipes (conhost/Windows Terminal); the guard covers that too since the error event path is identical. I tested on Linux only.

A closed console pipe makes each console.write raise EPIPE. Without an error listener, Node escalates it to an uncaught exception whose handler logs through the same console - an unbounded loop that once grew a log file to 2 GB. Attach stdout/stderr error listeners, mark console output broken on EPIPE, and keep file logging active.
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.

1 participant