X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FProcessPipe.pm;h=1bc792c4bada77676916b10f330e87d1c0d9b98f;hb=refs%2Fheads%2Fmaster;hp=943405ff07bd56692d8919ad3f3537407b7c3643;hpb=ebf0acfefa318b8404f30bb69008183478640f0e;p=public-inbox.git
diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm
index 943405ff..1bc792c4 100644
--- a/lib/PublicInbox/ProcessPipe.pm
+++ b/lib/PublicInbox/ProcessPipe.pm
@@ -1,38 +1,72 @@
-# Copyright (C) 2016 all contributors
+# Copyright (C) all contributors
# License: AGPL-3.0+
-# 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 warnings;
+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) = @_;
- bless { pid => $pid, fh => $fh }, $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 READ { sysread($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
+sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip
+
+sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
sub READLINE { readline($_[0]->{fh}) }
-sub CLOSE {
- my $fh = delete($_[0]->{fh});
- my $ret = defined $fh ? close($fh) : '';
- my $pid = delete $_[0]->{pid};
- if (defined $pid) {
- waitpid($pid, 0);
- $ret = '' if $?;
+sub WRITE {
+ use bytes qw(length);
+ syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0);
+}
+
+sub PRINT {
+ my $self = shift;
+ print { $self->{fh} } @_;
+}
+
+sub FILENO { fileno($_[0]->{fh}) }
+
+sub _close ($;$) {
+ my ($self, $wait) = @_;
+ my ($fh, $pid) = delete(@$self{qw(fh pid)});
+ my $ret = defined($fh) ? close($fh) : '';
+ return $ret unless defined($pid) && $self->{ppid} == $$;
+ if ($wait) { # caller cares about the exit status:
+ # 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;
}
-sub FILENO { fileno($_[0]->{fh}) }
+# if caller uses close(), assume they want to check $? immediately so
+# we'll waitpid() synchronously. n.b. wantarray doesn't seem to
+# propagate `undef' down to tied methods, otherwise I'd rely on that.
+sub CLOSE { _close($_[0], 1) }
+# if relying on DESTROY, assume the caller doesn't care about $? and
+# we can let the event loop call waitpid() whenever it gets SIGCHLD
sub DESTROY {
- CLOSE(@_);
+ _close($_[0]);
undef;
}
-sub pid { $_[0]->{pid} }
-
1;