Repository navigation
mixin_logger: free the log file on rotation and lock SetFileLeading - #539
Merged
Merged
Conversation
WriteLog closed the std::ofstream when the file reached the size limit but did not delete it, so each rotation leaked the stream and its buffer. Delete the stream instead. SetFileLeading changed the header string without the mutex that WriteLog holds while it writes the header to a new file, so a header could be written from a partly changed string. Hold the same mutex in SetFileLeading.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The narrow fixes match the existing ownership and locking model, with no unresolved findings.
No open issues 🎉
What changed in this PR
Fixes stream ownership and header synchronization in the native mixin_logger implementation without changing its API.
Changes:
- Deletes the output stream during rotation and destruction, preventing leaks.
- Locks
SetFileLeadingto synchronize header updates with log writes.
| File | Description |
|---|---|
packages/mixin_logger/src/mixin_logger.cpp |
Frees stream resources and synchronizes header updates. |
Review effort: Balanced
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
WriteLogclosed thestd::ofstreamwhen the file reachedmax_file_sizebut did not delete it. Each rotation leaked the stream object and its buffer.SetFileLeadingchangedfile_leading_withoutmutex_.WriteLogholdsmutex_and readsfile_leading_when it writes the header of a new file. When the two calls ran at the same time, the header of a new file could be a mix of the old and new strings.void SetFileLeading(const std::string &file_leading) { + std::lock_guard<std::mutex> lock(mutex_); file_leading_ = file_leading; } ... if (file_size_ >= max_file_size_) { - file_->close(); + delete file_; file_ = nullptr;The destructor also deletes the stream instead of only closing it.
Evidence
A native stress test that includes
mixin_logger.cpp(macOS, clang):leaks <pid>:After: 0 leaks
SetFileLeadingwith a 4096-byte string and a 64-byte string in turn. Count the files whose first line is not one of the two strings (three runs):After: 0, 0, 0
The existing GoogleTest suite in
src/test.cpppasses (5 tests).Merge Danger
Door: two-way
Blast Radius: small
No API change.
SetFileLeadingnow waits for aWriteLogcall that is in progress. That call is a short file write.