]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
ipc: simplify partial sendmsg fallback
authorEric Wong <e@80x24.org>
Thu, 9 Nov 2023 10:09:42 +0000 (10:09 +0000)
committerEric Wong <e@80x24.org>
Thu, 9 Nov 2023 21:53:52 +0000 (21:53 +0000)
In the rare case sendmsg(2) isn't able to send the full amount
(due to buffers >=2GB on Linux), use print + (autodie)close
to send the remainder and retry on EINTR.  `substr' should be
able to avoid a large malloc via offsets and CoW on modern Perl.

lib/PublicInbox/IPC.pm
t/ipc.t

index 3292d960409cae7677018928fd40e544a295c048..a5cae6f2e8b0113c67d394f3fee7d1afc6052b99 100644 (file)
@@ -10,7 +10,7 @@
 package PublicInbox::IPC;
 use v5.12;
 use parent qw(Exporter);
-use autodie qw(fork pipe read socketpair sysread);
+use autodie qw(close fork pipe read socketpair sysread);
 use Carp qw(croak);
 use PublicInbox::DS qw(awaitpid);
 use PublicInbox::Spawn;
@@ -266,15 +266,8 @@ sub stream_in_full ($$$) {
                        0) // croak "sendmsg: $!";
        undef $r;
        $n = $send_cmd->($w, $fds, $buf, 0) // croak "sendmsg: $!";
-       while ($n < length($buf)) {
-               my $x = syswrite($w, $buf, length($buf) - $n, $n);
-               if (!defined($n)) {
-                       next if $!{EINTR};
-                       croak "syswrite: $!";
-               }
-               $x or croak "syswrite wrote 0 bytes";
-               $n += $x;
-       }
+       print $w substr($buf, $n) if $n < length($buf); # need > 2G on Linux
+       close $w; # autodies
 }
 
 sub wq_io_do { # always async
diff --git a/t/ipc.t b/t/ipc.t
index 519ef089bfbec57edfbe9cd5d48738f839e50836..23ae2e7b2455dbb0b7febdfd25702408b36722b0 100644 (file)
--- a/t/ipc.t
+++ b/t/ipc.t
@@ -132,6 +132,13 @@ for my $t ('worker', 'worker again') {
                $exp = sha1_hex($bigger)."\n";
                is(readline($rb), $exp, "SHA WQWorker limit ($t)");
        }
+       SKIP: {
+               $ENV{TEST_EXPENSIVE} or skip 'TEST_EXPENSIVE not set', 1;
+               my $bigger = $big x 75000; # over 2G to trigger partial sendmsg
+               $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
+               my $exp = sha1_hex($bigger)."\n";
+               is(readline($rb), $exp, "SHA WQWorker sendmsg limit ($t)");
+       }
 }
 
 # wq_io_do works across fork (siblings can feed)