From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 0B2BA1FA19 for ; Sat, 16 Oct 2021 01:01:04 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 07/12] lei: more eval guards for die on failure Date: Sat, 16 Oct 2021 01:00:58 +0000 Message-Id: <20211016010103.30825-8-e@80x24.org> In-Reply-To: <20211016010103.30825-1-e@80x24.org> References: <20211016010103.30825-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Relying on $lei->fail is unsustainable since there'll always be parts of our code and dependencies which can trigger die() and break the event loop. --- lib/PublicInbox/LEI.pm | 6 +++--- lib/PublicInbox/LeiLcat.pm | 21 +++++++++------------ lib/PublicInbox/LeiQuery.pm | 24 +++++++++++------------- lib/PublicInbox/LeiXSearch.pm | 9 +++++---- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 0cdcf4492885..511b2c1d03a7 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -556,7 +556,7 @@ sub _lei_atfork_child { # we need to explicitly close things which are on stack if ($persist) { open $self->{3}, '<', '/' or die "open(/) $!"; - fchdir($self) or die; + fchdir($self); close($_) for (grep(defined, delete @$self{qw(0 1 2 sock)})); if (my $cfg = $self->{cfg}) { delete @$cfg{qw(-lei_store -watches -lei_note_event)}; @@ -779,7 +779,7 @@ sub lazy_cb ($$$) { sub dispatch { my ($self, $cmd, @argv) = @_; - fchdir($self) or return; + fchdir($self); local %ENV = %{$self->{env}}; local $current_lei = $self; # for __WARN__ $self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY @@ -1381,7 +1381,7 @@ sub wq_done_wait { # dwaitpid callback sub fchdir { my ($lei) = @_; my $dh = $lei->{3} // die 'BUG: lei->{3} (CWD) gone'; - chdir($dh) || $lei->fail("fchdir: $!"); + chdir($dh) || die "fchdir: $!"; } sub wq_eof { # EOF callback for main daemon diff --git a/lib/PublicInbox/LeiLcat.pm b/lib/PublicInbox/LeiLcat.pm index d553b18733da..191f6f244857 100644 --- a/lib/PublicInbox/LeiLcat.pm +++ b/lib/PublicInbox/LeiLcat.pm @@ -124,18 +124,15 @@ could not extract Message-ID from $x sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin my ($lei) = @_; # $_[1] = $rbuf - if (defined($_[1])) { - $_[1] eq '' and return eval { - $lei->fchdir or return; - my @argv = split(/\s+/, $lei->{mset_opt}->{qstr}); - $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) - or return; - $lei->_start_query; - }; - $lei->{mset_opt}->{qstr} .= $_[1]; - } else { - $lei->fail("error reading stdin: $!"); - } + $_[1] // return $lei->fail("error reading stdin: $!"); + return $lei->{mset_opt}->{qstr} .= $_[1] if $_[1] ne ''; + eval { + $lei->fchdir; + my @argv = split(/\s+/, $lei->{mset_opt}->{qstr}); + $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return; + $lei->_start_query; + }; + $lei->fail($@) if $@; } sub lei_lcat { diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm index c65b00ca0986..ec2ece051492 100644 --- a/lib/PublicInbox/LeiQuery.pm +++ b/lib/PublicInbox/LeiQuery.pm @@ -55,19 +55,17 @@ sub _start_query { # used by "lei q" and "lei up" } sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin - my ($self) = @_; # $_[1] = $rbuf - if (defined($_[1])) { - $_[1] eq '' and return eval { - $self->fchdir or return; - $self->{mset_opt}->{q_raw} = $self->{mset_opt}->{qstr}; - $self->{lse}->query_approxidate($self->{lse}->git, - $self->{mset_opt}->{qstr}); - _start_query($self); - }; - $self->{mset_opt}->{qstr} .= $_[1]; - } else { - $self->fail("error reading stdin: $!"); - } + my ($lei) = @_; # $_[1] = $rbuf + $_[1] // $lei->fail("error reading stdin: $!"); + return $lei->{mset_opt}->{qstr} .= $_[1] if $_[1] ne ''; + eval { + $lei->fchdir; + $lei->{mset_opt}->{q_raw} = $lei->{mset_opt}->{qstr}; + $lei->{lse}->query_approxidate($lei->{lse}->git, + $lei->{mset_opt}->{qstr}); + _start_query($lei); + }; + $lei->fail($@) if $@; } sub lxs_prepare { diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm index 4aa2a81c0025..8ab84b15c00b 100644 --- a/lib/PublicInbox/LeiXSearch.pm +++ b/lib/PublicInbox/LeiXSearch.pm @@ -460,10 +460,11 @@ sub do_post_augment { my ($lei) = @_; local $PublicInbox::LEI::current_lei = $lei; my $l2m = $lei->{l2m} or return; # client disconnected - $lei->fchdir or return; - my $err; - eval { $l2m->post_augment($lei) }; - $err = $@; + eval { + $lei->fchdir; + $l2m->post_augment($lei); + }; + my $err = $@; if ($err) { if (my $lxs = delete $lei->{lxs}) { $lxs->wq_kill('-TERM');