X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FProcessPipe.pm;h=97e9c268f8d1e91aa0f8c1225112dc42cd623768;hb=23af251dd607c4e75ab1e68063f2c885c48cc035;hp=c9234f4214d6f1f3a586370678301cd0c4107ff5;hpb=da089825419835c37afbdf7b7c5f0448766bdb27;p=public-inbox.git diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm index c9234f42..97e9c268 100644 --- a/lib/PublicInbox/ProcessPipe.pm +++ b/lib/PublicInbox/ProcessPipe.pm @@ -1,16 +1,20 @@ -# Copyright (C) 2016-2020 all contributors +# Copyright (C) 2016-2021 all contributors # License: AGPL-3.0+ # a tied handle for auto reaping of children tied to a pipe, see perltie(1) package PublicInbox::ProcessPipe; use strict; use v5.10.1; +use Carp qw(carp); sub TIEHANDLE { my ($class, $pid, $fh, $cb, $arg) = @_; - bless { pid => $pid, fh => $fh, cb => $cb, arg => $arg }, $class; + bless { pid => $pid, fh => $fh, ppid => $$, cb => $cb, arg => $arg }, + $class; } +sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip + sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) } sub READLINE { readline($_[0]->{fh}) } @@ -25,30 +29,42 @@ sub PRINT { print { $self->{fh} } @_; } -sub CLOSE { - my $fh = delete($_[0]->{fh}); - my $ret = defined $fh ? close($fh) : ''; - my ($pid, $cb, $arg) = delete @{$_[0]}{qw(pid cb arg)}; - if (defined $pid) { - # PublicInbox::DS may not be loaded - eval { PublicInbox::DS::dwaitpid($pid, $cb, $arg) }; +sub FILENO { fileno($_[0]->{fh}) } - if ($@) { # ok, not in the event loop, work synchronously - waitpid($pid, 0); +sub _close ($;$) { + my ($self, $wait) = @_; + my $fh = delete $self->{fh}; + 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 $?; - $cb->($arg, $pid) if $cb; + 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); } $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;