fix(utils): prevent ERR_STREAM_WRITE_AFTER_END crash during log rotation#150
Merged
fix(utils): prevent ERR_STREAM_WRITE_AFTER_END crash during log rotation#150
Conversation
Fixes race condition in date-based log rotation that caused crashes when Claude sessions crossed midnight. The async rotation fired but writeToLogFile continued to write to the stream being closed, causing stream write-after-end errors. Changes: - Add defensive checks in writeToLogFile() to skip writes during rotation - Check isRotating flag and stream.writable property before writing - Enhance close() method to wait for in-progress rotation (max 5 seconds) - Add detailed comments explaining the race condition scenario Impact: Prevents application crashes in long-running sessions that span midnight. Trade-off: May skip 1-2 log entries during rotation (acceptable vs crash). Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
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.
Summary
Fixes a critical race condition in the logger's date-based log rotation that caused
ERR_STREAM_WRITE_AFTER_ENDcrashes when Claude sessions crossed midnight. The issue occurred because log rotation was fired asynchronously (fire-and-forget pattern) while the write operation continued immediately, racing against stream closure.Changes
writeToLogFile(): CheckisRotatingflag andstream.writableproperty before writing to prevent writes during rotationclose()method: Made async and added wait logic (max 5 seconds with 100ms polling) for in-progress rotation to completeImpact
Before:
ERR_STREAM_WRITE_AFTER_ENDAfter:
Checklist