* [RFC] lei: address lifetime problems from Linux::Inotify2
@ 2021-07-28 12:34 Eric Wong
2021-07-29 10:01 ` [RFC v2] lei: close inotify FD in forked child Eric Wong
0 siblings, 1 reply; 3+ messages in thread
From: Eric Wong @ 2021-07-28 12:34 UTC (permalink / raw)
To: meta
Linux::Inotify2 doesn't supply an explicit close() wrapper,
and Linux::Inotify2::Event objects maintain a reference to the
parent Linux::Inotify2 object, so we were leaking inotify FDs
into the "lei note-event" worker processes.
This delays the fork()-ing code paths into the next tick of the
event loop to ensure the inotify FD is closed when $dir_idle
is undef'ed in ->_lei_atfork_child.
Note: I'm not sure if I want to keep this, since
The correct fix would be to have a way to close inotify FDs,
but the current Linux::Inotify2 API doesn't make it easy.
And leaking an inotify FD isn't the worst thing in the world,
since it won't hang other processes. So maybe the cure
is worse than the disease, in this case.
---
lib/PublicInbox/LEI.pm | 31 ++++++++++++++++++++++---------
1 file changed, 22 insertions(+), 9 deletions(-)
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index d9fd40fd..b81f95c9 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -28,8 +28,8 @@ use Time::HiRes qw(stat); # ctime comparisons for config cache
use File::Path qw(mkpath);
use File::Spec;
our $quit = \&CORE::exit;
-our ($current_lei, $errors_log, $listener, $oldset, $dir_idle,
- $recv_cmd, $send_cmd);
+our ($current_lei, $errors_log, $listener, $oldset,
+ $dir_idle, $lne_nxt, $lne_q, $recv_cmd, $send_cmd);
my $GLP = Getopt::Long::Parser->new;
$GLP->configure(qw(gnu_getopt no_ignore_case auto_abbrev));
my $GLP_PASS = Getopt::Long::Parser->new;
@@ -557,6 +557,8 @@ sub _lei_atfork_child {
close $listener if $listener;
undef $listener;
undef $dir_idle;
+ undef $lne_nxt;
+ undef $lne_q;
%PATH2CFG = ();
$MDIR2CFGPATH = {};
eval 'no warnings; undef $PublicInbox::LeiNoteEvent::to_flush';
@@ -1153,6 +1155,21 @@ sub cfg2lei ($) {
$lei;
}
+sub lne_task { # requeue, runs without dir_idle stuff on stack
+ undef $lne_nxt;
+ my $q = $lne_q // return;
+ $lne_q = undef;
+
+ while (my ($loc, $nc, $bn, $fn, $cfg) = splice(@$q, 0, 5)) {
+ eval {
+ local %ENV = %{$cfg->{-env}};
+ my $lei = cfg2lei($cfg);
+ $lei->dispatch('note-event', $loc, $nc, $bn, $fn);
+ };
+ warn "E note-event $cfg->{-f}: $@\n" if $@;
+ }
+}
+
sub dir_idle_handler ($) { # PublicInbox::DirIdle callback
my ($ev) = @_; # Linux::Inotify2::Event or duck type
my $fn = $ev->fullname;
@@ -1161,14 +1178,9 @@ sub dir_idle_handler ($) { # PublicInbox::DirIdle callback
$nc = '' if $ev->IN_DELETE;
for my $f (keys %{$MDIR2CFGPATH->{$mdir} // {}}) {
my $cfg = $PATH2CFG{$f} // next;
- eval {
- local %ENV = %{$cfg->{-env}};
- my $lei = cfg2lei($cfg);
- $lei->dispatch('note-event',
- "maildir:$mdir", $nc, $bn, $fn);
- };
- warn "E note-event $f: $@\n" if $@;
+ push @$lne_q, "maildir:$mdir", $nc, $bn, $fn, $cfg;
}
+ $lne_nxt //= PublicInbox::DS::requeue(\&lne_task) if $lne_q;
}
if ($ev->can('cancel') && ($ev->IN_IGNORE || $ev->IN_UNMOUNT)) {
$ev->cancel;
@@ -1261,6 +1273,7 @@ sub lazy_start {
undef $sig;
local $SIG{PIPE} = 'IGNORE';
require PublicInbox::DirIdle;
+ local ($lne_q, $lne_nxt);
local $dir_idle = PublicInbox::DirIdle->new([$sock_dir], sub {
# just rely on wakeup to hit PostLoopCallback set below
dir_idle_handler($_[0]) if $_[0]->fullname ne $path;
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [RFC v2] lei: close inotify FD in forked child
2021-07-28 12:34 [RFC] lei: address lifetime problems from Linux::Inotify2 Eric Wong
@ 2021-07-29 10:01 ` Eric Wong
2021-08-04 10:40 ` Eric Wong
0 siblings, 1 reply; 3+ messages in thread
From: Eric Wong @ 2021-07-29 10:01 UTC (permalink / raw)
To: meta
It looks like Linux::Inotify2 2.3+ will include 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.
http://lists.schmorp.de/pipermail/perl/2021q3/thread.html
---
MANIFEST | 1 +
lib/PublicInbox/DirIdle.pm | 11 +++++++++++
lib/PublicInbox/LEI.pm | 2 +-
lib/PublicInbox/LI2Wrap.pm | 20 ++++++++++++++++++++
4 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 lib/PublicInbox/LI2Wrap.pm
diff --git a/MANIFEST b/MANIFEST
index a3913501..fb9f16bf 100644
--- 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
diff --git a/lib/PublicInbox/DirIdle.pm b/lib/PublicInbox/DirIdle.pm
index 65896f95..d572c274 100644
--- a/lib/PublicInbox/DirIdle.pm
+++ b/lib/PublicInbox/DirIdle.pm
@@ -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;
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index d9fd40fd..e6f763e1 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -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
index 00000000..b0f4f8b8
--- /dev/null
+++ b/lib/PublicInbox/LI2Wrap.pm
@@ -0,0 +1,20 @@
+# 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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [RFC v2] lei: close inotify FD in forked child
2021-07-29 10:01 ` [RFC v2] lei: close inotify FD in forked child Eric Wong
@ 2021-08-04 10:40 ` Eric Wong
0 siblings, 0 replies; 3+ messages in thread
From: Eric Wong @ 2021-08-04 10:40 UTC (permalink / raw)
To: meta
Pushed with present tense commit message now that L::I2 2.3+ is out:
https://public-inbox.org/meta/7fc6e30aeab9925bece4bb00f88bb91af5646aa2/s/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-08-04 10:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-28 12:34 [RFC] lei: address lifetime problems from Linux::Inotify2 Eric Wong
2021-07-29 10:01 ` [RFC v2] lei: close inotify FD in forked child Eric Wong
2021-08-04 10:40 ` Eric Wong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).