Log reads can fail with Errno::EACCES during LogDevice rotation on Windows
還沒有人認領這個 Issue。
評估
研究方向
Start with LogDevice#lock_shift_log and the Windows locking change in commit 7b6146f; the reproduction in the issue shows how a separate reader fails during rotation. Compare the locking behavior with create_logfile and the referenced draft PR fluent/fluentd#5690. Done means choosing and implementing a locking approach that allows reads during rotation while keeping rotators mutually exclusive, then verifying the reproduction on Windows.
由索引模型根據 Issue 內容生成。
描述
Since logger 1.6.3, a read through a separate handle can fail with Errno::EACCES during log rotation on Windows. Commit 7b6146f enabled flock(File::LOCK_EX) in LogDevice#lock_shift_log on Windows. Ruby implements flock there with LockFileEx. The lock is mandatory and covers the whole file.
For example, when Fluentd tails its own log, a read during rotation sometimes fails with Permission denied @ io_getpartial. Fluentd then loses the lines it has not yet read.
Reproduction
The hook makes the read happen at the same point in rotation on every run. In the affected versions, lock_shift_log already holds the lock at that point. A polling reader can hit the same window by chance.
require "logger"
require "tmpdir"
Dir.mktmpdir do |dir|
path = File.join(dir, "app.log")
logger = Logger.new(path, 3, 1024)
logger.info("x" * 1024)
# Open a separate handle to simulate a program that tails the log.
reader = File.open(path, File::RDONLY | File::BINARY | File::SHARE_DELETE)
# Read at the start of shift_log_age, then let rotation continue.
result = nil
hook = Module.new do
private
define_method(:shift_log_age) do
begin
result = "read #{reader.read.bytesize} bytes"
rescue Errno::EACCES => e
result = "#{e.class}: #{e.message}"
end
super()
end
end
logger.instance_variable_get(:@logdev).singleton_class.prepend(hook)
logger.info("rotate")
puts "logger #{Logger::VERSION}: #{result}"
reader.close
logger.close
end
Output on Windows 10 (ruby 3.4.13 x64-mingw-ucrt):
logger 1.6.2: read 1143 bytes
logger 1.6.3: Errno::EACCES: Permission denied @ io_fread - .../app.log
logger 1.7.0: Errno::EACCES: Permission denied @ io_fread - .../app.log
Possible fix
One option on Windows is to use LockFileEx to lock a single byte far beyond EOF. Windows allows this, and reads of the log data do not overlap that byte. Rotators that lock the same byte would still wait for each other. A whole-file flock from logger 1.6.3 to 1.7.0 also covers that byte, so mixed versions would still exclude each other. Raymond Chen describes this technique in You can use a file as a synchronization object, too.
The draft PR fluent/fluentd#5690 tries this approach. It overrides lock_shift_log and calls LockFileEx through Fiddle. If you prefer not to call a Win32 API from logger, could the locking step be made overridable?
Related behavior
create_logfile also holds flock(File::LOCK_EX) while writing a new log header. It holds the lock for much less time. Older versions also lock the file here.
#165 is another Windows problem around lock_shift_log after logger 1.6.2.
- 主要語言
- Ruby
- 星號
- 161
- 分支
- 76
- 平均合併
- 2 天 5 小時
- 30 天內合併 PR
- 3
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
ruby/logger 的其他 Issue
-
Programs using Logger sometimes resist SIGTERM / SIGINT可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
-
難度 3/5 1-2 天 新手友好度 68/100
-
難度 3/5 1-2 天 新手友好度 52/100
-
`datetime_format=` does not apply to custom formatters可能已有人在做 @Rubyist007 於 229 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 72/100
-
難度 4/5 3-5 天 新手友好度 35/100
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100
open-feature/ruby-sdk-contrib#340 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
zold-io/wts.zold.io#651 ·
維護者通常 1 天內回覆
-
good first issue help wanted
難度 2/5 1-3 小時 新手友好度 87/100
erkamyaman/rust-anything#788 ·
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 74/100
TheOdinProject/curriculum#31579 ·
維護者通常 1 天內回覆