]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiXSearch.pm
lei: more consistent IPC exit and error handling
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
index daf42098a9bc4d066a98d99e1df31c63e1d406db..1024b0209da05a124b7cdd4a01b34d04fcc8b272 100644 (file)
@@ -197,13 +197,6 @@ sub each_eml { # callback for MboxReader->mboxrd
        $each_smsg->($smsg, undef, $eml);
 }
 
-# PublicInbox::OnDestroy callback
-sub kill_reap {
-       my ($pid) = @_;
-       kill('KILL', $pid); # spawn() blocks other signals
-       waitpid($pid, 0);
-}
-
 sub query_remote_mboxrd {
        my ($self, $uris) = @_;
        local $0 = "$0 query_remote_mboxrd";
@@ -212,60 +205,38 @@ sub query_remote_mboxrd {
        my ($opt, $env) = @$lei{qw(opt env)};
        my @qform = (q => $lei->{mset_opt}->{qstr}, x => 'm');
        push(@qform, t => 1) if $opt->{thread};
-       my @cmd = ($self->{curl}, qw(-sSf -d), '');
        my $verbose = $opt->{verbose};
-       my $reap;
+       my ($reap_tail, $reap_curl);
        my $cerr = File::Temp->new(TEMPLATE => 'curl.err-XXXX', TMPDIR => 1);
        fcntl($cerr, F_SETFL, O_APPEND|O_RDWR) or warn "set O_APPEND: $!";
-       my $rdr = { 2 => $cerr };
+       my $rdr = { 2 => $cerr, pgid => 0 };
        my $coff = 0;
+       my $sigint_reap = $lei->can('sigint_reap');
        if ($verbose) {
                # spawn a process to force line-buffering, otherwise curl
                # will write 1 character at-a-time and parallel outputs
                # mmmaaayyy llloookkk llliiikkkeee ttthhhiiisss
-               push @cmd, '-v';
-               my $o = { 1 => $lei->{2}, 2 => $lei->{2} };
+               my $o = { 1 => $lei->{2}, 2 => $lei->{2}, pgid => 0 };
                my $pid = spawn(['tail', '-f', $cerr->filename], undef, $o);
-               $reap = PublicInbox::OnDestroy->new(\&kill_reap, $pid);
-       }
-       for my $o ($lei->curl_opt) {
-               $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
-               if ($o =~ s/=[is]@\z//) {
-                       my $ary = $opt->{$o} or next;
-                       push @cmd, map { ("--$o", $_) } @$ary;
-               } elsif ($o =~ s/=[is]\z//) {
-                       my $val = $opt->{$o} // next;
-                       push @cmd, "--$o", $val;
-               } elsif ($opt->{$o}) {
-                       push @cmd, "--$o";
-               }
+               $reap_tail = PublicInbox::OnDestroy->new($sigint_reap, $pid);
        }
-       $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
-       my $tor = $opt->{torsocks} //= 'auto';
+       my $curl = PublicInbox::LeiCurl->new($lei, $self->{curl}) or return;
+       push @$curl, '-s', '-d', '';
        my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
        for my $uri (@$uris) {
                $lei->{-current_url} = $uri->as_string;
                $lei->{-nr_remote_eml} = 0;
                $uri->query_form(@qform);
-               my $cmd = [ @cmd, $uri->as_string ];
-               if ($tor eq 'auto' && substr($uri->host, -6) eq '.onion' &&
-                               (($env->{LD_PRELOAD}//'') !~ /torsocks/)) {
-                       unshift @$cmd, which('torsocks');
-               } elsif (PublicInbox::Config::git_bool($tor)) {
-                       unshift @$cmd, which('torsocks');
-               }
-
-               # continue anyways if torsocks is missing; a proxy may be
-               # specified via CLI, curlrc, environment variable, or even
-               # firewall rule
-               shift(@$cmd) if !$cmd->[0];
-
+               my $cmd = $curl->for_uri($lei, $uri);
                $lei->err("# @$cmd") if $verbose;
                my ($fh, $pid) = popen_rd($cmd, $env, $rdr);
+               $reap_curl = PublicInbox::OnDestroy->new($sigint_reap, $pid);
                $fh = IO::Uncompress::Gunzip->new($fh);
                PublicInbox::MboxReader->mboxrd($fh, \&each_eml, $self,
                                                $lei, $each_smsg);
-               waitpid($pid, 0) == $pid or die "BUG: waitpid (curl): $!";
+               my $err = waitpid($pid, 0) == $pid ? undef : "BUG: waitpid: $!";
+               @$reap_curl = (); # cancel OnDestroy
+               die $err if $err;
                if ($? == 0) {
                        my $nr = $lei->{-nr_remote_eml};
                        mset_progress($lei, $lei->{-current_url}, $nr, $nr);
@@ -308,12 +279,18 @@ sub git_tmp ($) {
        $git;
 }
 
+sub xsearch_done_wait { # dwaitpid callback
+       my ($arg, $pid) = @_;
+       my ($wq, $lei) = @$arg;
+       $lei->child_error($?, 'non-fatal error from '.ref($wq)) if $?;
+}
+
 sub query_done { # EOF callback for main daemon
        my ($lei) = @_;
        my $l2m = delete $lei->{l2m};
-       $l2m->wq_wait_old($lei) if $l2m;
+       $l2m->wq_wait_old(\&xsearch_done_wait, $lei) if $l2m;
        if (my $lxs = delete $lei->{lxs}) {
-               $lxs->wq_wait_old($lei);
+               $lxs->wq_wait_old(\&xsearch_done_wait, $lei);
        }
        $lei->{ovv}->ovv_end($lei);
        if ($l2m) { # close() calls LeiToMail reap_compress
@@ -338,7 +315,7 @@ sub do_post_augment {
        if (my $err = $@) {
                if (my $lxs = delete $lei->{lxs}) {
                        $lxs->wq_kill;
-                       $lxs->wq_close;
+                       $lxs->wq_close(0, undef, $lei);
                }
                $lei->fail("$err");
        }
@@ -362,10 +339,10 @@ sub start_query { # always runs in main (lei-daemon) process
        }
        if ($lei->{opt}->{thread}) {
                for my $ibxish (locals($self)) {
-                       $self->wq_do('query_thread_mset', [], $ibxish);
+                       $self->wq_io_do('query_thread_mset', [], $ibxish);
                }
        } elsif (locals($self)) {
-               $self->wq_do('query_mset', []);
+               $self->wq_io_do('query_mset', []);
        }
        my $i = 0;
        my $q = [];
@@ -373,7 +350,7 @@ sub start_query { # always runs in main (lei-daemon) process
                push @{$q->[$i++ % $MAX_PER_HOST]}, $uri;
        }
        for my $uris (@$q) {
-               $self->wq_do('query_remote_mboxrd', [], $uris);
+               $self->wq_io_do('query_remote_mboxrd', [], $uris);
        }
 }
 
@@ -383,7 +360,7 @@ sub ipc_atfork_child {
        $self->SUPER::ipc_atfork_child;
 }
 
-sub query_prepare { # called by wq_do
+sub query_prepare { # called by wq_io_do
        my ($self) = @_;
        local $0 = "$0 query_prepare";
        my $lei = $self->{lei};
@@ -392,25 +369,11 @@ sub query_prepare { # called by wq_do
        pkt_do($lei->{pkt_op_p}, '.') == 1 or die "do_post_augment trigger: $!"
 }
 
-sub fail_handler ($;$$) {
-       my ($lei, $code, $io) = @_;
-       for my $f (qw(lxs l2m)) {
-               my $wq = delete $lei->{$f} or next;
-               $wq->wq_wait_old($lei) if $wq->wq_kill_old; # lei-daemon
-       }
-       close($io) if $io; # needed to avoid warnings on SIGPIPE
-       $lei->x_it($code // (1 >> 8));
-}
-
-sub sigpipe_handler { # handles SIGPIPE from l2m/lxs workers
-       fail_handler($_[0], 13, delete $_[0]->{1});
-}
-
 sub do_query {
        my ($self, $lei) = @_;
        my $ops = {
-               '|' => [ \&sigpipe_handler, $lei ],
-               '!' => [ \&fail_handler, $lei ],
+               '|' => [ $lei->can('sigpipe_handler'), $lei ],
+               '!' => [ $lei->can('fail_handler'), $lei ],
                '.' => [ \&do_post_augment, $lei ],
                '' => [ \&query_done, $lei ],
                'mset_progress' => [ \&mset_progress, $lei ],
@@ -441,7 +404,7 @@ sub do_query {
        delete $lei->{pkt_op_p};
        $l2m->wq_close(1) if $l2m;
        $lei->event_step_init; # wait for shutdowns
-       $self->wq_do('query_prepare', []) if $l2m;
+       $self->wq_io_do('query_prepare', []) if $l2m;
        start_query($self, $lei);
        $self->wq_close(1); # lei_xsearch workers stop when done
        if ($lei->{oneshot}) {
@@ -454,6 +417,7 @@ sub add_uri {
        if (my $curl = $self->{curl} //= which('curl') // 0) {
                require PublicInbox::MboxReader;
                require IO::Uncompress::Gunzip;
+               require PublicInbox::LeiCurl;
                push @{$self->{remotes}}, $uri;
        } else {
                warn "curl missing, ignoring $uri\n";