prevent panic and resource leak in file rotation#48
Open
larspehrsson wants to merge 4 commits intomasterfrom
Open
prevent panic and resource leak in file rotation#48larspehrsson wants to merge 4 commits intomasterfrom
larspehrsson wants to merge 4 commits intomasterfrom
Conversation
The log rotation logic could cause a panic by attempting to access a closed file handle when renaming log files. This is resolved by: - Caching the filename in the roll() function before the file is closed. - Setting the file handle to nil after closing it. - Adding a nil check in CloseAllOpenFileLoggers() to handle already-rolled files. Additionally, a resource leak is fixed where rolling files were repeatedly added to the global open files slice. A new 'registered' flag ensures each file is added only once. As a minor improvement, the log writer now combines the timestamp and message into a single buffer to perform one atomic write preventing splitting up timestamp and message into different logs
Capture and return roll() error in rollingFile.Write to avoid proceeding with a write when log rotation fails
Ensures writes can resume after rotation or startup by calling open() if rf.file is nil. If open fails, return the error instead of attempting to write.
If a file rotation (`roll`) fails after closing the file but before reopening a new one, the file handle would be left as `nil`. Subsequent calls to `Write` would trigger another roll, leading to a panic when `roll` tried to access the `nil` file handle. This change moves the `nil` file check from `Write` into `roll` to ensure the file is open before proceeding with rotation logic, thus preventing the panic.
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.
The log rotation logic could cause a panic by attempting to access a closed file handle when renaming log files.
This is resolved by:
Additionally, a resource leak is fixed where rolling files were repeatedly added to the global open files slice. A new 'registered' flag ensures each file is added only once.
As a minor improvement, the log writer now combines the timestamp and message into a single buffer to perform one atomic write preventing splitting up timestamp and message into different logs