Repository navigation
log: fix in_tail failing to read the log during rotation on Windows - #5690
Conversation
ashie
left a comment
There was a problem hiding this comment.
On Windows, I reproduced a double rotation with two processes writing to the same log file. Before this change, the file rotated once. With this change, the second process also rotated the newly created log, moving the first process’s new log entry into .0 and the original log into .1.
A normal Fluentd instance and a --dry-run invocation can open the same supervisor log when they use the same configuration, so they could hit this race. I reproduced the race with two LogDeviceIO processes, and the --dry-run CLI case remains unverified.
Could we preserve inter-process locking for rotation without blocking in_tail reads?
|
Thanks, you're right. Replacing In cf7e6ea, On Windows 10 with logger 1.7.0, two |
Since logger 1.6.3, Logger::LogDevice#lock_shift_log locks the whole log file with flock during rotation on Windows too. flock on Windows is a mandatory lock, so in_tail reading fluentd's own log failed with "Permission denied @ io_getpartial" and lost the unread lines. With logger 1.6.3 or later on Windows, Fluent::LogDeviceIO now runs the same steps as logger 1.7.0, but locks only one byte far beyond EOF with LockFileEx. Readers are not blocked. logger's whole-file flock also covers that byte, so processes rotating the same file still wait for each other. Fluent.windows? and friends move to fluent/platform, so that fluent/log doesn't load fluent/env, which sets Oj's options. See ruby/logger#188 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
b773ec9 to
39f99fb
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The native LockFileEx/Fiddle changes require Windows x64 and ARM runtime validation of error preservation, flock interoperability, and timeout behavior that this source-only review cannot provide.
No open issues 🎉
What changed in this PR
This PR addresses Windows log-rotation failures that can cause in_tail to lose unread lines from Fluentd’s own log.
Changes:
- Uses a one-byte lock beyond EOF during Windows rotation with logger 1.6.3 or later, preserving rotation coordination without blocking readers.
- Extracts shared platform detection into a separate module.
- Adds regression tests for reading during rotation, lock coordination, interrupted waits, and reopening rotated files.
| File | Description |
|---|---|
test/test_log.rb |
Adds rotation, locking, and reopening regression tests. |
lib/fluent/win32api.rb |
Adds nonblocking lock and unlock API support. |
lib/fluent/platform.rb |
Centralizes platform-detection helpers. |
lib/fluent/log.rb |
Implements Windows rotation locking beyond EOF. |
lib/fluent/file_wrapper.rb |
Explicitly loads platform-detection helpers. |
lib/fluent/env.rb |
Uses the extracted platform module. |
Review effort: Balanced
ashie
left a comment
There was a problem hiding this comment.
I reran the two-process reproduction on Windows x64 and confirmed that the log now rotates only once. Both processes’ new entries stayed in the active log. All 125 tests in test_log.rb passed.
One compatibility note worth recording: requiring fluent/win32api directly no longer loads fluent/env. The platform predicates are still available, and both normal Fluentd startup and require 'fluent/load' explicitly load fluent/env. This should only affect callers that rely on the internal Win32 helper to load fluent/env indirectly. I’m comfortable approving with that change.
|
A note on why The first version of this PR added
|
Which issue(s) this PR fixes:
Fixes #
What this PR does / why we need it:
On Windows, in_tail that reads fluentd's own log sometimes fails with
Permission denied @ io_getpartialwhen the log rotates, and the unread lines of the old file are lost. Since logger 1.6.3 (ruby/logger@7b6146f),Logger::LogDevice#lock_shift_logholdsflock(LOCK_EX)on the whole log file during rotation on Windows too, and flock on Windows is a mandatory lock (ruby/logger#188). With logger 1.6.3 or later on Windows,Fluent::LogDeviceIOnow runs the same steps as logger 1.7.0 but locks only one byte far beyond EOF withLockFileEx, so readers are not blocked. logger's whole-file flock also covers that byte, so processes rotating the same file still wait for each other. The short flock that logger takes while writing the header of a new log file is unchanged.Requiring
fluent/win32apialone no longer loadsfluent/env.Fluent.windows?and the other platform predicates are still available fromfluent/platform.Docs Changes:
N/A
Release Note:
log: fix in_tail failing to read the log during rotation on Windows
🤖 Generated with Claude Code