]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: close inotify FD in forked child
authorEric Wong <e@80x24.org>
Thu, 29 Jul 2021 10:01:31 +0000 (10:01 +0000)
committerEric Wong <e@80x24.org>
Wed, 4 Aug 2021 10:04:23 +0000 (10:04 +0000)
Linux::Inotify2 2.3+ includes an ->fh method to give us the
ability to safely close an FD without hitting EBADF (and
automatically use FD_CLOEXEC).

We'll still need a new wrapper class (LI2Wrap) to handle it for
users of old versions, though.

Link: http://lists.schmorp.de/pipermail/perl/2021q3/thread.html
MANIFEST
lib/PublicInbox/DirIdle.pm
lib/PublicInbox/LEI.pm
lib/PublicInbox/LI2Wrap.pm [new file with mode: 0644]

index a39135018f39275606a87b8e40887d17497dd1f0..fb9f16bfb37ebb3d8f20a80570aa77754ceed306 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -197,6 +197,7 @@ lib/PublicInbox/InputPipe.pm
 lib/PublicInbox/Isearch.pm
 lib/PublicInbox/KQNotify.pm
 lib/PublicInbox/LEI.pm
+lib/PublicInbox/LI2Wrap.pm
 lib/PublicInbox/LeiALE.pm
 lib/PublicInbox/LeiAddWatch.pm
 lib/PublicInbox/LeiAuth.pm
index 65896f950f51f756998899bd44a8c44a4442c116..d572c2749f7fc750e0a3ae3cbcf8748b1263ef10 100644 (file)
@@ -84,4 +84,15 @@ sub event_step {
        warn "$self->{inot}->read err: $@\n" if $@;
 }
 
+sub force_close {
+       my ($self) = @_;
+       my $inot = delete $self->{inot} // return;
+       if ($inot->can('fh')) { # Linux::Inotify2 2.3+
+               close($inot->fh) or warn "CLOSE ERROR: $!";
+       } elsif ($inot->isa('Linux::Inotify2')) {
+               require PublicInbox::LI2Wrap;
+               PublicInbox::LI2Wrap::wrapclose($inot);
+       }
+}
+
 1;
index d9fd40fd90e8244399e89a252f7f55a943cc2ea5..e6f763e1067869e0a7c6d3bb8a90e2b544f45e0d 100644 (file)
@@ -556,7 +556,7 @@ sub _lei_atfork_child {
        }
        close $listener if $listener;
        undef $listener;
-       undef $dir_idle;
+       $dir_idle->force_close if $dir_idle;
        %PATH2CFG = ();
        $MDIR2CFGPATH = {};
        eval 'no warnings; undef $PublicInbox::LeiNoteEvent::to_flush';
diff --git a/lib/PublicInbox/LI2Wrap.pm b/lib/PublicInbox/LI2Wrap.pm
new file mode 100644 (file)
index 0000000..61cf4be
--- /dev/null
@@ -0,0 +1,19 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Wrapper for Linux::Inotify2 < 2.3 which lacked ->fh and auto-close
+# Remove this when supported LTS/enterprise distros are all
+# Linux::Inotify2 >= 2.3
+package PublicInbox::LI2Wrap;
+use v5.10.1;
+our @ISA = qw(Linux::Inotify2);
+
+sub wrapclose {
+       my ($inot) = @_;
+       my $fd = $inot->fileno;
+       open my $fh, '<&=', $fd or die "open <&= $fd $!";
+}
+
+sub DESTROY {} # no-op
+
+1