Skip to content

Fix pending output corruption when changing file buffers - #3673

Merged
gabime merged 2 commits into
gabime:v1.xfrom
fhgffy:fix/preserve-output-on-buffer-resize
Oct 12, 2026
Merged

gabime merged 2 commits into
gabime:v1.xfrom
fhgffy:fix/preserve-output-on-buffer-resize

Conversation

@fhgffy

@fhgffy fhgffy commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Changing a file sink from a 64 KiB buffer to 128 KiB while log output is pending can corrupt the previously written message. The original reproducer produced the expected 29 bytes, but their contents differed. file_helper::set_buffer_size() resizes or releases storage that the open FILE still uses, and calls setvbuf after I/O.

Flush the pending output and reuse the existing non-truncating reopen path so the new buffer is configured before I/O. Assigning the same size is a no-op. After a size change, the rotating sink flushes callback output and refreshes its size estimate.

A size change now reopens the current pathname in append mode and invokes the existing close/open handlers. It uses the existing flush/reopen exception paths; callers must handle reconfiguration failures. This also avoids relying on runtime-specific behavior for repeated setvbuf calls (C11 draft, section 7.21.5.6).

Validation on Windows, MinGW GCC 15.1.0, C++11 Debug, with warnings treated as errors:

  • The new pending-output regression fails on the unmodified base in both compiled and header-only builds (2 failed assertions out of 16 each).
  • The original reproducer passes after the fix; focused tests pass in both builds (42 assertions in 6 test cases each), covering default/custom buffers, growth, shrinkage, reset, unchanged sizes, file events, and rotating size tracking.
  • Full unit tests pass: compiled 725 assertions / 196 cases; header-only 746 assertions / 196 cases.
  • CTest passes all 3 targets, including CMake static/shared source and installed-consumer checks.

Linux/macOS, MSVC, wchar configurations, C++20 std::format, and sanitizers have not been run locally.

@fhgffy
fhgffy marked this pull request as ready for review October 3, 2026 23:51
@gabime
gabime merged commit 6c9e898 into gabime:v1.x Oct 12, 2026
14 checks passed
@gabime

gabime commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thanks @fhgffy

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.

2 participants