use PublicInbox::Spawn qw(popen_rd);
use IO::Handle ();
use PublicInbox::Syscall qw(EPOLLONESHOT);
+use PublicInbox::DS qw(dwaitpid);
# fields:
# async_cat => GitAsyncCat ref (read-only pipe)
# sock => writable pipe to Gcf2::loop
-
-
+# in => pipe we read from
+# pid => PID of Gcf2::loop process
+# owner_pid => process which spawned {pid}
sub new {
my ($rdr) = @_;
my $self = bless {}, __PACKAGE__;
$rdr //= {};
$rdr->{0} = $out_r;
my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
+ $self->{owner_pid} = $$;
@$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
$out_w->autoflush(1);
no warnings 'once';
+sub DESTROY {
+ my ($self) = @_;
+ delete $self->{in};
+ # GitAsyncCat::event_step may reap us with WNOHANG, too
+ my $pid = delete $self->{pid} or return;
+ if ($$ == $self->{owner_pid}) {
+ PublicInbox::DS->in_loop ? $self->close : delete($self->{sock});
+ dwaitpid $pid;
+ }
+}
+
# used by GitAsyncCat
*cat_async_step = \&PublicInbox::Git::cat_async_step;