Skip to content

mixin_logger: free the log file on rotation and lock SetFileLeading - #539

Merged
boyan01 merged 2 commits into
mainfrom
mixin_logger/delete-rotated-file
Nov 9, 2026
Merged

boyan01 merged 2 commits into
mainfrom
mixin_logger/delete-rotated-file

Conversation

@YeungKC

@YeungKC YeungKC commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

WriteLog closed the std::ofstream when the file reached max_file_size but did not delete it. Each rotation leaked the stream object and its buffer.

SetFileLeading changed file_leading_ without mutex_. WriteLog holds mutex_ and reads file_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):

  • 20,000 rotations, then leaks <pid>:
    • Before: 40,000 leaks for 94,720,000 bytes
      After: 0 leaks
  • 2,000 rotations while a second thread calls SetFileLeading with 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):
    • Before: 206, 200, 236 of 2,000 files
      After: 0, 0, 0

The existing GoogleTest suite in src/test.cpp passes (5 tests).

Merge Danger

Door: two-way

Blast Radius: small

No API change. SetFileLeading now waits for a WriteLog call that is in progress. That call is a short file write.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 SetFileLeading to 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

@boyan01
boyan01 merged commit 253c709 into main Nov 9, 2026
7 checks passed
@boyan01
boyan01 deleted the mixin_logger/delete-rotated-file branch November 9, 2026 07:03
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.

3 participants