Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rolling log file writer: avoid truncating file when rotating #144

Merged
merged 1 commit into from
Jan 10, 2017
Merged

Rolling log file writer: avoid truncating file when rotating #144

merged 1 commit into from
Jan 10, 2017

Conversation

lwithers
Copy link

@lwithers lwithers commented Jan 9, 2017

There are several conditions under which the rotating log file writer can actually truncate the file it is rotating.

Principally, this is caused by a lack of locking when writing to the log file. In rollingFileWriter.Write(), we test for whether the current file needs to be rotated, and if so then perform the rotation. This needs locking to ensure that we do not have two (or more) goroutines making the decision to rotate, then rotating. If this occurs then the second rotation of an empty or virtually-empty file will truncate the original file's contents. This commit resolves the problem by adding mutex locking to Write().

There is also a more convoluted path through the time-based file writer code which (previously) checked the modification time of the file on disk, meaning that a mutex in the Write() routine alone is not sufficient. This commit also changes the time-based tests to cache the time and filename, removing the need for a call to os.Stat() and removing the possibility of making an inconsistent decision to roll more than once.

Fixes #143. Supersedes #135.

There are several conditions under which the rotating log file writer
can actually truncate the file it is rotating.

Principally, this is caused by a lack of locking when writing to the log
file. In rollingFileWriter.Write(), we test for whether the current file
needs to be rotated, and if so then perform the rotation. This needs
locking to ensure that we do not have two (or more) goroutines making
the decision to rotate, then rotating. If this occurs then the second
rotation of an empty or virtually-empty file will truncate the original
file's contents. This commit resolves the problem by adding mutex
locking to Write().

There is also a more convoluted path through the time-based file writer
code which (previously) checked the modification time of the file on
disk, meaning that a mutex in the Write() routine alone is not
sufficient.  This commit also changes the time-based tests to cache the
time and filename, removing the need for a call to os.Stat() and
removing the possibility of making an inconsistent decision to roll more
than once.

Fixes #143.
@pkorotkov
Copy link
Collaborator

@lwithers Many thanks for your contribution! No lock in rolling writing is really a dangerous bug.
Looks good to me. Merging.

@pkorotkov pkorotkov merged commit 7bfb793 into cihub:master Jan 10, 2017
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.

rollingfile lost log
2 participants