]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: more eval guards for die on failure
authorEric Wong <e@80x24.org>
Sat, 16 Oct 2021 01:00:58 +0000 (01:00 +0000)
committerEric Wong <e@80x24.org>
Sat, 16 Oct 2021 01:42:52 +0000 (01:42 +0000)
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
lib/PublicInbox/LeiLcat.pm
lib/PublicInbox/LeiQuery.pm
lib/PublicInbox/LeiXSearch.pm

index 0cdcf4492885ce4b3ca98d2d2586ce49e50a6cac..511b2c1d03a74277b3195c02489beb289084995f 100644 (file)
@@ -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
index d553b18733da5a267845e7b142b73b10aad07d75..191f6f244857221d0086db1b0548d64ed35b2c8a 100644 (file)
@@ -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 {
index c65b00ca0986f5857b6cfc53b0dcba4abe64849c..ec2ece0514929660cd9139a569cbfd8bc1c3875c 100644 (file)
@@ -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 {
index 4aa2a81c00256698b87241bfe7c19578210aad4e..8ab84b15c00ba1937f0e73c24140c7fb7e471254 100644 (file)
@@ -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');