]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
inotify: wrap with informative error message
authorEric Wong <e@80x24.org>
Tue, 28 Mar 2023 11:12:36 +0000 (11:12 +0000)
committerEric Wong <e@80x24.org>
Wed, 29 Mar 2023 04:36:42 +0000 (04:36 +0000)
As encountered by Louis DeLosSantos, Linux inotify is capped by
a lesser-known limit than the standard RLIMIT_NOFILE (`ulimit -n`)
value.  Give the user a hint about the fs.inotify.max_user_instances
sysctl knob on EMFILE, since EMFILE alone may mislead users into
thinking they've hit the (typically higher) RLIMIT_NOFILE limit.

I can test this on my system using:

  perl -I lib -MPublicInbox::Inotify -E \
   'my @x = map { PublicInbox::Inotify->new } (1..128)'

But I hesitate to include it in the test suite since triggering
the limit can cause unrelated processes to fail.

Link: https://public-inbox.org/meta/CAE6jdTo8iQfNM9Yuk0Dwi-ARMxmQxX-onL8buXcQ9Ze3r0hKrg@mail.gmail.com/
Reported-by: Louis DeLosSantos <louis.delos@gmail.com>
MANIFEST
lib/PublicInbox/DirIdle.pm
lib/PublicInbox/InboxIdle.pm
lib/PublicInbox/Inotify.pm [new file with mode: 0644]

index 4053523307189ecd643dc98937a292f399f61fe1..3c421645864897802f980ab52769c21ddb8fc510 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -217,6 +217,7 @@ lib/PublicInbox/In2Tie.pm
 lib/PublicInbox/Inbox.pm
 lib/PublicInbox/InboxIdle.pm
 lib/PublicInbox/InboxWritable.pm
+lib/PublicInbox/Inotify.pm
 lib/PublicInbox/InputPipe.pm
 lib/PublicInbox/Isearch.pm
 lib/PublicInbox/KQNotify.pm
index 55c3982f707c0f9328f4adde1e9cb90eed526126..af99811c7da539bbdf6278d6748c9560e98759e0 100644 (file)
@@ -9,14 +9,14 @@ use PublicInbox::Syscall qw(EPOLLIN);
 use PublicInbox::In2Tie;
 
 my ($MAIL_IN, $MAIL_GONE, $ino_cls);
-if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
+if ($^O eq 'linux' && eval { require PublicInbox::Inotify; 1 }) {
        $MAIL_IN = Linux::Inotify2::IN_MOVED_TO() |
                Linux::Inotify2::IN_CREATE();
        $MAIL_GONE = Linux::Inotify2::IN_DELETE() |
                        Linux::Inotify2::IN_DELETE_SELF() |
                        Linux::Inotify2::IN_MOVE_SELF() |
                        Linux::Inotify2::IN_MOVED_FROM();
-       $ino_cls = 'Linux::Inotify2';
+       $ino_cls = 'PublicInbox::Inotify';
 # Perl 5.22+ is needed for fileno(DIRHANDLE) support:
 } elsif ($^V ge v5.22 && eval { require PublicInbox::KQNotify }) {
        $MAIL_IN = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
index f0d8a972ed03226074bba2317180a8e9dcf6ffa5..4231c0a0cf73d037e0a0963554df19bcb541e935 100644 (file)
@@ -10,9 +10,9 @@ use parent qw(PublicInbox::DS);
 use PublicInbox::Syscall qw(EPOLLIN);
 my $IN_MODIFY = 0x02; # match Linux inotify
 my $ino_cls;
-if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
+if ($^O eq 'linux' && eval { require PublicInbox::Inotify }) {
        $IN_MODIFY = Linux::Inotify2::IN_MODIFY();
-       $ino_cls = 'Linux::Inotify2';
+       $ino_cls = 'PublicInbox::Inotify';
 } elsif (eval { require PublicInbox::KQNotify }) {
        $IN_MODIFY = PublicInbox::KQNotify::NOTE_WRITE();
        $ino_cls = 'PublicInbox::KQNotify';
diff --git a/lib/PublicInbox/Inotify.pm b/lib/PublicInbox/Inotify.pm
new file mode 100644 (file)
index 0000000..3ef271c
--- /dev/null
@@ -0,0 +1,30 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# wrap Linux::Inotify2 XS module, support pure Perl via `syscall' someday
+package PublicInbox::Inotify;
+use v5.12;
+our @ISA;
+BEGIN {
+       eval { require Linux::Inotify2 };
+       if ($@) { # TODO: get rid of XS dependency
+               die "W: Linux::Inotify2 missing: $@\n";
+       } else {
+               push @ISA, 'Linux::Inotify2';
+       }
+};
+
+sub new {
+       $_[0]->SUPER::new // do {
+               my $msg = $!{EMFILE} ? <<EOM : "$_[0]->new: $!\n";
+inotify_init/inotify_init1: $!
+You may need to raise the `fs.inotify.max_user_instances' sysctl limit.
+Consult your OS documentation and/or sysctl(8) + sysctl.conf(5) manpages.
+EOM
+               $msg =~ s/^/E: /smg;
+               require Carp;
+               Carp::croak($msg);
+       }
+}
+
+1;