]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ProcessPipe.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / ProcessPipe.pm
index 97e9c268f8d1e91aa0f8c1225112dc42cd623768..1bc792c4bada77676916b10f330e87d1c0d9b98f 100644 (file)
@@ -1,16 +1,27 @@
-# Copyright (C) 2016-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>
 
-# a tied handle for auto reaping of children tied to a pipe, see perltie(1)
+# a tied handle for auto reaping of children tied to a read-only pipe, see perltie(1)
+# DO NOT use this as-is for bidirectional pipes/sockets (e.g. in PublicInbox::Git),
+# both ends of the pipe must be at the same level of the Perl object hierarchy
+# to ensure orderly destruction.
 package PublicInbox::ProcessPipe;
-use strict;
-use v5.10.1;
-use Carp qw(carp);
+use v5.12;
+use PublicInbox::DS qw(awaitpid);
+
+sub waitcb { # awaitpid callback
+       my ($pid, $err_ref, $cb, @args) = @_;
+       $$err_ref = $?; # sets >{pp_chld_err} for _close
+       $cb->($pid, @args) if $cb;
+}
 
 sub TIEHANDLE {
-       my ($class, $pid, $fh, $cb, $arg) = @_;
-       bless { pid => $pid, fh => $fh, ppid => $$, cb => $cb, arg => $arg },
-               $class;
+       my ($cls, $pid, $fh, @cb_arg) = @_;
+       my $self = bless { pid => $pid, fh => $fh, ppid => $$ }, $cls;
+       # we share $err (and not $self) with awaitpid to avoid a ref cycle
+       $self->{pp_chld_err} = \(my $err);
+       awaitpid($pid, \&waitcb, \$err, @cb_arg);
+       $self;
 }
 
 sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip
@@ -33,24 +44,15 @@ sub FILENO { fileno($_[0]->{fh}) }
 
 sub _close ($;$) {
        my ($self, $wait) = @_;
-       my $fh = delete $self->{fh};
+       my ($fh, $pid) = delete(@$self{qw(fh pid)});
        my $ret = defined($fh) ? close($fh) : '';
-       my ($pid, $cb, $arg) = delete @$self{qw(pid cb arg)};
        return $ret unless defined($pid) && $self->{ppid} == $$;
        if ($wait) { # caller cares about the exit status:
-               my $wp = waitpid($pid, 0);
-               if ($wp == $pid) {
-                       $ret = '' if $?;
-                       if ($cb) {
-                               eval { $cb->($arg, $pid) };
-                               carp "E: cb(arg, $pid): $@" if $@;
-                       }
-               } else {
-                       carp "waitpid($pid, 0) = $wp, \$!=$!, \$?=$?";
-               }
-       } else { # caller just undef-ed it, let event loop deal with it
-               require PublicInbox::DS;
-               PublicInbox::DS::dwaitpid($pid, $cb, $arg);
+               # synchronous wait via defined(wantarray) on awaitpid:
+               defined(${$self->{pp_chld_err}}) or $wait = awaitpid($pid);
+               ($? = ${$self->{pp_chld_err}}) and $ret = '';
+       } else {
+               awaitpid($pid); # depends on $in_loop or not
        }
        $ret;
 }