Later writes fail with "closed stream" when renaming the log fails during rotation on Windows

Ouverte
#189 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
68/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
ruby
Domaine
backend

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Ruby
Étoiles
161
Forks
76
Merge moyen
2 j 5 h
PR mergées (30 j)
3

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de ruby/logger

Toutes les issues de ruby/logger

Issues similaires

Plus d'issues Ruby

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.