Later writes fail with "closed stream" when renaming the log fails during rotation on Windows
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 68/100
Direzione di ricerca
Start by reading LogDevice#shift_log_file and the Windows-specific rename and inter-process locking paths described in the issue. Reproduce the failure with the supplied script on Windows, then check the logger tests for rotation behavior and add coverage for a failed rename while a rotated file is open. Done means failed rotation leaves the writer open so later writes are preserved and a subsequent write can retry rotation.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
On Windows, LogDevice#shift_log_file closes @dev before it renames the active log to <log>.0. If that rename fails, the entry that triggered the rotation is lost. Every later write fails with closed stream until the log is reopened.
On Windows, a rename can't replace a destination file that is open, even when the handle that has it open uses File::SHARE_DELETE. With shift_age set to 2 or less, the active log is renamed over <log>.0 directly. So if another program still has <log>.0 open, the rename fails after @dev is closed. With shift_age set to 3 or more, rotation first moves <log>.0 to <log>.1, before it closes @dev. If that rename fails, the writer is still open. If it succeeds, <log>.0 is free for the active log.
Reproduction
The script sets shift_age to 2, so the second rotation renames the log over the app.log.0 that the first rotation created. Opening app.log.0 simulates a program that tails the log. For example, Fluentd's in_tail keeps the rotated file open for a few seconds (rotate_wait) to finish reading it.
require "logger"
require "tmpdir"
Dir.mktmpdir do |dir|
path = File.join(dir, "app.log")
logger = Logger.new(path, 2, 100)
logger.formatter = proc { |_severity, _time, _progname, msg| "#{msg}\n" }
logger.info("a" * 100)
logger.info("rotated once")
logger.info("b" * 100)
# A program that tails the log still has the rotated file open.
reader = File.open(path + ".0", File::RDONLY | File::BINARY | File::SHARE_DELETE)
logger.info("rotate again")
logger.info("next line")
puts "logger #{Logger::VERSION}: dev closed? #{logger.instance_variable_get(:@logdev).dev.closed?}"
puts "app.log: #{File.readlines(path).grep_v(/\A#/).map { |l| l.chomp[0, 12] }.inspect}"
reader.close
logger.close
end
Output on Windows 10 (ruby 3.4.13 x64-mingw-ucrt):
log rotation inter-process lock failed. Permission denied @ rb_file_s_rename - (.../app.log, .../app.log.0)
log writing failed. closed stream
log shifting failed. closed stream
log writing failed. closed stream
logger 1.7.0: dev closed? true
app.log: ["rotated once", "bbbbbbbbbbbb"]
The first two warnings come from logger.info("rotate again"). The first one reports that Windows denied the rename from app.log to app.log.0. It says "inter-process lock failed" because lock_shift_log rescues the error. The second one reports that the write found @dev closed. The last two warnings come from logger.info("next line"). Both the rotation check and the write hit the closed stream.
The same two entries are lost on Windows with logger 1.6.2 and 1.6.3. On 1.6.2, the first warning begins with log shifting failed instead. On Linux with logger 1.7.0, @dev stays open, and app.log has both entries.
Possible fix
Could shift_log_file close @dev only after the rename succeeds? Since logger 1.6.3, logger opens @dev with File::SHARE_DELETE on Windows. So the active log can be renamed while it is open. If the rename fails, the writer can still append to the current file and retry the rotation on the next write.
I tried this on Windows with logger 1.7.0. I prepended a version of shift_log_file that renames the active log first, then closes @dev and creates the new file. The script then printed dev closed? false. Both "rotate again" and "next line" were in app.log.
Related: #188 is another Windows problem in log rotation.
- Lingua principale
- Ruby
- Stelle
- 161
- Fork
- 76
- Merge medio
- 2g 5h
- PR unite (30g)
- 3
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ruby/logger
-
Programs using Logger sometimes resist SIGTERM / SIGINTForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
-
`datetime_format=` does not apply to custom formattersForse già presa @Rubyist007 l’ha presa 229 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
open-feature/ruby-sdk-contrib#340 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
zold-io/wts.zold.io#651 ·
I maintainer di solito rispondono entro 1 giorno
-
good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
erkamyaman/rust-anything#788 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
TheOdinProject/curriculum#31579 ·
I maintainer di solito rispondono entro 1 giorno