]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
daemon: reopen log files atomically
authorEric Wong <e@80x24.org>
Mon, 16 Jun 2025 20:05:13 +0000 (20:05 +0000)
committerEric Wong <e@80x24.org>
Mon, 16 Jun 2025 20:10:53 +0000 (20:10 +0000)
Ensuring there is no window for a Perl IO (file handle) to be
invalid is useful in case some code ever gets used in a
multithreaded environment.  The FD stability would also be less
confusing for users using `lsof(8)' or similar to track FDs.

lib/PublicInbox/Daemon.pm

index c5af836776e06112dc0a996f866771f60d04ca7a..c52ac0a1137a0de10ded5e9512ceb256681b7eb1 100644 (file)
@@ -110,7 +110,12 @@ sub do_chown ($) {
 }
 
 sub open_log_path ($$) { # my ($fh, $path) = @_; # $_[0] is modified
-       open $_[0], '>>', $_[1];
+       if (defined $_[0]) {
+               open my $tmp_fh, '>>', $_[1];
+               POSIX::dup2(fileno($tmp_fh), fileno($_[0])) or die "dup2: $!";
+       } else {
+               open $_[0], '>>', $_[1];
+       }
        $_[0]->autoflush(1);
        do_chown($_[1]);
        $_[0];