about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2024-04-25 21:31:44 +0000
committerEric Wong <e@80x24.org>2024-04-28 17:05:28 +0000
commit25c5108f2bdb29009ac986b7bde29ffca24e6e7a (patch)
treecaafc84388fe38a282e5d0f5dc87c9612a1fa178
parentdba2370bd52ce5b550906c7fdb516af512d81bce (diff)
downloadpublic-inbox-25c5108f2bdb29009ac986b7bde29ffca24e6e7a.tar.gz
While existing callers are private (lei, *-index, -watch) are
private, we should not be blocking the event loop in
public-facing servers when we hit ETOOMANYREFS, ENOMEM, or
ENOBUFS.
-rw-r--r--lib/PublicInbox/CmdIPC4.pm12
-rw-r--r--lib/PublicInbox/Spawn.pm12
-rw-r--r--lib/PublicInbox/Syscall.pm8
3 files changed, 17 insertions, 15 deletions
diff --git a/lib/PublicInbox/CmdIPC4.pm b/lib/PublicInbox/CmdIPC4.pm
index 2f102ec6..fc77bd03 100644
--- a/lib/PublicInbox/CmdIPC4.pm
+++ b/lib/PublicInbox/CmdIPC4.pm
@@ -11,8 +11,8 @@ use Socket qw(SOL_SOCKET SCM_RIGHTS);
 sub sendmsg_retry ($) {
         return 1 if $!{EINTR};
         return unless ($!{ENOMEM} || $!{ENOBUFS} || $!{ETOOMANYREFS});
-        return if ++$_[0] >= 50;
-        warn "# sleeping on sendmsg: $! (#$_[0])\n";
+        return if --$_[0] < 0;
+        warn "# sleeping on sendmsg: $! ($_[0] tries left)\n";
         select(undef, undef, undef, 0.1);
         1;
 }
@@ -22,15 +22,15 @@ require Socket::MsgHdr; # XS
 no warnings 'once';
 
 # any number of FDs per-sendmsg(2) + buffer
-*send_cmd4 = sub ($$$$) { # (sock, fds, buf, flags) = @_;
-        my ($sock, $fds, undef, $flags) = @_;
+*send_cmd4 = sub ($$$$;$) { # (sock, fds, buf, flags) = @_;
+        my ($sock, $fds, undef, $flags, $tries) = @_;
+        $tries //= 50;
         my $mh = Socket::MsgHdr->new(buf => $_[2]);
         $mh->cmsghdr(SOL_SOCKET, SCM_RIGHTS, pack('i' x scalar(@$fds), @$fds));
         my $s;
-        my $try = 0;
         do {
                 $s = Socket::MsgHdr::sendmsg($sock, $mh, $flags);
-        } while (!defined($s) && sendmsg_retry($try));
+        } while (!defined($s) && sendmsg_retry($tries));
         $s;
 };
 
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index e36659ce..e9e81e88 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -176,15 +176,15 @@ out:
         return (int)pid;
 }
 
-static int sendmsg_retry(unsigned *tries)
+static int sendmsg_retry(int *tries)
 {
         const struct timespec req = { 0, 100000000 }; /* 100ms */
         int err = errno;
         switch (err) {
         case EINTR: PERL_ASYNC_CHECK(); return 1;
         case ENOBUFS: case ENOMEM: case ETOOMANYREFS:
-                if (++*tries >= 50) return 0;
-                fprintf(stderr, "# sleeping on sendmsg: %s (#%u)\n",
+                if (--*tries < 0) return 0;
+                fprintf(stderr, "# sleeping on sendmsg: %s (%d tries left)\n",
                         strerror(err), *tries);
                 nanosleep(&req, NULL);
                 PERL_ASYNC_CHECK();
@@ -201,7 +201,7 @@ union my_cmsg {
         char pad[sizeof(struct cmsghdr) + 16 + SEND_FD_SPACE];
 };
 
-SV *send_cmd4(PerlIO *s, SV *svfds, SV *data, int flags)
+SV *send_cmd4_(PerlIO *s, SV *svfds, SV *data, int flags, int tries)
 {
         struct msghdr msg = { 0 };
         union my_cmsg cmsg = { 0 };
@@ -211,7 +211,6 @@ SV *send_cmd4(PerlIO *s, SV *svfds, SV *data, int flags)
         AV *fds = (AV *)SvRV(svfds);
         I32 i, nfds = av_len(fds) + 1;
         int *fdp;
-        unsigned tries = 0;
 
         if (SvOK(data)) {
                 iov.iov_base = SvPV(data, dlen);
@@ -332,6 +331,9 @@ EOM
         if (defined $all_libc) { # set for Gcf2
                 $ENV{PERL_INLINE_DIRECTORY} = $inline_dir;
                 %RLIMITS = rlimit_map();
+                *send_cmd4 = sub ($$$$;$) {
+                        send_cmd4_($_[0], $_[1], $_[2], $_[3], 50);
+                }
         } else {
                 require PublicInbox::SpawnPP;
                 *pi_fork_exec = \&PublicInbox::SpawnPP::pi_fork_exec
diff --git a/lib/PublicInbox/Syscall.pm b/lib/PublicInbox/Syscall.pm
index 99af5bf5..4cbe9623 100644
--- a/lib/PublicInbox/Syscall.pm
+++ b/lib/PublicInbox/Syscall.pm
@@ -467,8 +467,8 @@ if (defined($SYS_sendmsg) && defined($SYS_recvmsg)) {
 no warnings 'once';
 require PublicInbox::CmdIPC4;
 
-*send_cmd4 = sub ($$$$) {
-        my ($sock, $fds, undef, $flags) = @_;
+*send_cmd4 = sub ($$$$;$) {
+        my ($sock, $fds, undef, $flags, $tries) = @_;
         my $iov = pack('P'.TMPL_size_t,
                         $_[2] // NUL, length($_[2] // NUL) || 1);
         my $fd_space = scalar(@$fds) * SIZEOF_int;
@@ -487,10 +487,10 @@ require PublicInbox::CmdIPC4;
                         $msg_controllen,
                         0); # msg_flags
         my $s;
-        my $try = 0;
+        $tries //= 50;
         do {
                 $s = syscall($SYS_sendmsg, fileno($sock), $mh, $flags);
-        } while ($s < 0 && PublicInbox::CmdIPC4::sendmsg_retry($try));
+        } while ($s < 0 && PublicInbox::CmdIPC4::sendmsg_retry($tries));
         $s >= 0 ? $s : undef;
 };