]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GitAsyncCat.pm
ds: awaitpid: do not clobber entries for reaped processes
[public-inbox.git] / lib / PublicInbox / GitAsyncCat.pm
index 2d601542964224de7c1d4fc630eda40929e50386..6dda7340ad72c9336a6643b650ec749fdd9fd0a8 100644 (file)
@@ -1,14 +1,14 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # internal class used by PublicInbox::Git + PublicInbox::DS
 # This parses the output pipe of "git cat-file --batch"
 package PublicInbox::GitAsyncCat;
-use strict;
+use v5.12;
 use parent qw(PublicInbox::DS Exporter);
 use POSIX qw(WNOHANG);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
-our @EXPORT = qw(ibx_async_cat ibx_async_prefetch);
+our @EXPORT = qw(ibx_async_cat ibx_async_prefetch async_check);
 use PublicInbox::Git ();
 
 our $GCF2C; # singleton PublicInbox::Gcf2Client
@@ -45,10 +45,14 @@ sub event_step {
        }
 }
 
-sub git_tmp_cleanup {
+sub watch_cat {
        my ($git) = @_;
-       $git->cleanup(1) and
-               PublicInbox::DS::add_timer(3, \&git_tmp_cleanup, $git);
+       $git->{async_cat} //= do {
+               my $self = bless { git => $git }, __PACKAGE__;
+               $git->{in}->blocking(0);
+               $self->SUPER::new($git->{in}, EPOLLIN|EPOLLET);
+               \undef; # this is a true ref()
+       };
 }
 
 sub ibx_async_cat ($$$$) {
@@ -66,17 +70,22 @@ sub ibx_async_cat ($$$$) {
                \undef;
        } else { # read-only end of git-cat-file pipe
                $git->cat_async($oid, $cb, $arg);
-               $git->{async_cat} //= do {
-                       my $self = bless { git => $git }, __PACKAGE__;
-                       $git->{in}->blocking(0);
-                       $git->{-tmp} and PublicInbox::DS::add_uniq_timer(
-                                               3, \&git_tmp_cleanup, $git);
-                       $self->SUPER::new($git->{in}, EPOLLIN|EPOLLET);
-                       \undef; # this is a true ref()
-               };
+               watch_cat($git);
        }
 }
 
+sub async_check ($$$$) {
+       my ($ibx, $oidish, $cb, $arg) = @_; # $ibx may be $ctx
+       my $git = $ibx->{git} // $ibx->git;
+       $git->check_async($oidish, $cb, $arg);
+       $git->{async_chk} //= do {
+               my $self = bless { git => $git }, 'PublicInbox::GitAsyncCheck';
+               $git->{in_c}->blocking(0);
+               $self->SUPER::new($git->{in_c}, EPOLLIN|EPOLLET);
+               \undef; # this is a true ref()
+       };
+}
+
 # this is safe to call inside $cb, but not guaranteed to enqueue
 # returns true if successful, undef if not.  For fairness, we only
 # prefetch if there's no in-flight requests.
@@ -99,3 +108,34 @@ sub ibx_async_prefetch {
 }
 
 1;
+package PublicInbox::GitAsyncCheck;
+use v5.12;
+our @ISA = qw(PublicInbox::GitAsyncCat);
+use POSIX qw(WNOHANG);
+use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
+
+sub event_step {
+       my ($self) = @_;
+       my $git = $self->{git} or return;
+       return $self->close if ($git->{in_c} // 0) != ($self->{sock} // 1);
+       my $inflight = $git->{inflight_c};
+       if ($inflight && @$inflight) {
+               $git->check_async_step($inflight);
+
+               # child death?
+               if (($git->{in_c} // 0) != ($self->{sock} // 1)) {
+                       $self->close;
+               } elsif (@$inflight || exists $git->{rbuf_c}) {
+                       # ok, more to do, requeue for fairness
+                       $self->requeue;
+               }
+       } elsif ((my $pid = waitpid($git->{pid_c}, WNOHANG)) > 0) {
+               # May happen if the child process is killed by a BOFH
+               # (or segfaults)
+               delete $git->{pid_c};
+               warn "E: git $pid exited with \$?=$?\n";
+               $self->close;
+       }
+}
+
+1;