From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 3E2931F49B for ; Sat, 7 Oct 2023 21:24:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1696713851; bh=oeg2J9c3tcj8MFvmoqqEFIZ0z4oJ/z1sLfS1L8F42yY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VR4oeeFhyDwOQO2ah+ooTKH6CyBiV/7ZFX2QQI75nmazF9ZqiMhZcDPsw5Ds2OrlR sNXSAzCzI30EKF1mqDomd0mHrvnRMRRgYh/91NidiFtmAJRTk6jyN3cG4zqEqkkvv+ ogXL/r/EbwfvbI6tVT4SMUyEp73il1CpIY0LUQqM= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 5/9] ipc: use autodie for most syscalls Date: Sat, 7 Oct 2023 21:24:06 +0000 Message-ID: <20231007212410.297785-6-e@80x24.org> In-Reply-To: <20231007212410.297785-1-e@80x24.org> References: <20231007212410.297785-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: I'm not sure how/if we should bother recovering from these, so just croak and let some caller deal with it. `autodie' uses Carp internally, so setting `PERL5OPT=-MCarp=verbose' in the environment gives us full stacktraces. --- lib/PublicInbox/IPC.pm | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm index ba8b5739..5964645e 100644 --- a/lib/PublicInbox/IPC.pm +++ b/lib/PublicInbox/IPC.pm @@ -8,9 +8,9 @@ # use ipc_do when you need work done on a certain process # use wq_io_do when your work can be done on any idle worker package PublicInbox::IPC; -use strict; -use v5.10.1; +use v5.12; use parent qw(Exporter); +use autodie qw(fork pipe read socketpair sysread); use Carp qw(croak); use PublicInbox::DS qw(awaitpid); use PublicInbox::Spawn; @@ -54,9 +54,9 @@ our $send_cmd = PublicInbox::Spawn->can('send_cmd4') // do { sub _get_rec ($) { my ($r) = @_; - defined(my $len = <$r>) or return; + my $len = <$r> // return; chop($len) eq "\n" or croak "no LF byte in $len"; - defined(my $n = read($r, my $buf, $len)) or croak "read error: $!"; + my $n = read($r, my $buf, $len); $n == $len or croak "short read: $n != $len"; ipc_thaw($buf); } @@ -98,12 +98,12 @@ sub ipc_worker_spawn { my ($self, $ident, $oldset, $fields, @cb_args) = @_; return if ($self->{-ipc_ppid} // -1) == $$; # idempotent delete(@$self{qw(-ipc_req -ipc_res -ipc_ppid -ipc_pid)}); - pipe(my ($r_req, $w_req)) or die "pipe: $!"; - pipe(my ($r_res, $w_res)) or die "pipe: $!"; + pipe(my $r_req, my $w_req); + pipe(my $r_res, my $w_res); my $sigset = $oldset // PublicInbox::DS::block_signals(); $self->ipc_atfork_prepare; my $seed = rand(0xffffffff); - my $pid = fork // die "fork: $!"; + my $pid = fork; if ($pid == 0) { srand($seed); eval { Net::SSLeay::randomize() }; @@ -211,15 +211,12 @@ sub recv_and_run { my $n = length($buf) or return 0; my $nfd = 0; for my $fd (@fds) { - if (open(my $cmdfh, '+<&=', $fd)) { - $self->{$nfd++} = $cmdfh; - $cmdfh->autoflush(1); - } else { - die "$$ open(+<&=$fd) (FD:$nfd): $!"; - } + open(my $cmdfh, '+<&=', $fd); + $self->{$nfd++} = $cmdfh; + $cmdfh->autoflush(1); } while ($full_stream && $n < $len) { - my $r = sysread($s2, $buf, $len - $n, $n) // croak "read: $!"; + my $r = sysread($s2, $buf, $len - $n, $n); croak "read EOF after $n/$len bytes" if $r == 0; $n = length($buf); } @@ -267,8 +264,7 @@ sub wq_broadcast { sub stream_in_full ($$$) { my ($s1, $fds, $buf) = @_; - socketpair(my $r, my $w, AF_UNIX, SOCK_STREAM, 0) or - croak "socketpair: $!"; + socketpair(my $r, my $w, AF_UNIX, SOCK_STREAM, 0); my $n = $send_cmd->($s1, [ fileno($r) ], ipc_freeze(['do_sock_stream', length($buf)]), 0) // croak "sendmsg: $!"; @@ -315,7 +311,7 @@ sub wq_sync_run { sub wq_do { my ($self, $sub, @args) = @_; if (defined(wantarray)) { - pipe(my ($r, $w)) or die "pipe: $!"; + pipe(my $r, my $w); wq_io_do($self, 'wq_sync_run', [ $w ], wantarray, $sub, @args); undef $w; _wait_return($r, $sub); @@ -344,10 +340,9 @@ sub wq_nonblock_do { # always async sub _wq_worker_start { my ($self, $oldset, $fields, $one, @cb_args) = @_; my ($bcast1, $bcast2); - $one or socketpair($bcast1, $bcast2, AF_UNIX, SOCK_SEQPACKET, 0) or - die "socketpair: $!"; + $one or socketpair($bcast1, $bcast2, AF_UNIX, SOCK_SEQPACKET, 0); my $seed = rand(0xffffffff); - my $pid = fork // die "fork: $!"; + my $pid = fork; if ($pid == 0) { srand($seed); eval { Net::SSLeay::randomize() }; @@ -381,9 +376,7 @@ sub wq_workers_start { my ($self, $ident, $nr_workers, $oldset, $fields, @cb_args) = @_; ($send_cmd && $recv_cmd) or return; return if $self->{-wq_s1}; # idempotent - $self->{-wq_s1} = $self->{-wq_s2} = undef; - socketpair($self->{-wq_s1}, $self->{-wq_s2}, AF_UNIX, SOCK_SEQPACKET, 0) - or die "socketpair: $!"; + socketpair($self->{-wq_s1}, $self->{-wq_s2},AF_UNIX, SOCK_SEQPACKET, 0); $self->ipc_atfork_prepare; $nr_workers //= $self->{-wq_nr_workers}; # was set earlier my $sigset = $oldset // PublicInbox::DS::block_signals();