fix(logger): stop console writes after EPIPE to prevent crash loop - #209
Open
iagorobo24-hub wants to merge 1 commit into
Open
fix(logger): stop console writes after EPIPE to prevent crash loop#209iagorobo24-hub wants to merge 1 commit into
iagorobo24-hub wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of changes
When the console pipe is closed (e.g.
npm start | head, terminal closed,--daemondetached), everyconsole.writeraisesEPIPE. Without anerrorlistener onstdout/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:
errorlisteners toprocess.stdout/process.stderrthat mark console output as broken onEPIPE(installed once, guarded by a global flag).loggerconsole writes throughwriteToConsole(), which stops console output once broken.__resetLoggerForTests()clears the broken flag between tests.Closes issue (optional)
How it was tested
tests/utils/logger.test.ts: emits a syntheticEPIPEonprocess.stderr, then verifies file logging continues (INFO,WARN,ERRORall present in the log file) whileconsole.warn/console.errorare never called again.1561 tests,npm run lint,npm run typecheck,npm run buildall green.Checklist
fix(logger): stop console writes after EPIPE to prevent crash loopmainnpm run lint,npm run build, andnpm testOS note: EPIPE handling is standard Node.js stream behavior and behaves identically on Linux, macOS, and Windows. On Windows,
process.stdout/process.stderrmay be virtual pipes (conhost/Windows Terminal); the guard covers that too since the error event path is identical. I tested on Linux only.