Skip to content

log: fix in_tail failing to read the log during rotation on Windows - #5690

Merged
Watson1978 merged 1 commit into
fluent:masterfrom
Watson1978:log-windows-rotation-lock
Nov 27, 2026
Merged

Watson1978 merged 1 commit into
fluent:masterfrom
Watson1978:log-windows-rotation-lock

Conversation

@Watson1978

@Watson1978 Watson1978 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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_getpartial when 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_log holds flock(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::LogDeviceIO now runs the same steps as logger 1.7.0 but locks only one byte far beyond EOF with LockFileEx, 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/win32api alone no longer loads fluent/env. Fluent.windows? and the other platform predicates are still available from fluent/platform.

Docs Changes:
N/A

Release Note:
log: fix in_tail failing to read the log during rotation on Windows

🤖 Generated with Claude Code

@Watson1978 Watson1978 added bug Something isn't working backport to v1.19 We will backport this fix to the LTS branch labels Nov 27, 2026
@Watson1978 Watson1978 added this to the v1.21.0 milestone Nov 27, 2026

@ashie ashie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@Watson1978

Copy link
Copy Markdown
Contributor Author

Thanks, you're right. Replacing lock_shift_log with yield also dropped logger's File.identical? check and its ENOENT retry, not only the lock.

In cf7e6ea, LogDeviceIO on Windows runs the same steps as logger 1.7.0, but locks one byte far beyond EOF with LockFileEx instead of flock on the whole file. Reads of the log data don't overlap that byte, so in_tail can read the log during rotation. Other processes still wait for the lock, including ones that use flock like older Fluentd.

On Windows 10 with logger 1.7.0, two LogDeviceIO on the same path and --dry-run with the same config now rotate once, as on master. I also added tests for both. One checks that the lock is held during rotation, and the other checks that a log rotated by another LogDeviceIO is reopened instead of rotated again.

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>
@Watson1978
Watson1978 force-pushed the log-windows-rotation-lock branch from b773ec9 to 39f99fb Compare November 27, 2026 04:57
@Watson1978
Watson1978 requested review from ashie and kenhys and a balanced review from Copilot November 27, 2026 05:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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 ashie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@Watson1978

Copy link
Copy Markdown
Contributor Author

A note on why Fluent.windows?, linux? and macos? moved to fluent/platform. The problem is not Oj itself. Loading fluent/env runs Fluent::OjOptions.load_env, which sets Oj.default_options for the whole process.

The first version of this PR added require 'fluent/env' to log.rb only to call Fluent.windows?. Then a process that loads only fluent/log got a different default Oj mode (:object became :compat). Fluentd itself loads fluent/env early, so it is not affected. Code that loads fluent/log without fluent/env, such as fluent/log-ext in fluent-plugin-s3, could be affected when it runs outside Fluentd, for example in a test.

fluent/platform has only these checks, without that side effect. fluent/env still loads it, so code that gets Fluent.windows? through fluent/env keeps working.

@Watson1978
Watson1978 merged commit ff61ae2 into fluent:master Nov 27, 2026
20 checks passed
@Watson1978
Watson1978 deleted the log-windows-rotation-lock branch November 27, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport to v1.19 We will backport this fix to the LTS branch bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants