X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FProcessPipe.pm;h=2ce7eb8f46d71e5c3b2cd84d8147d32ca253232c;hb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;hp=eade524caada14ae99d4815822ec9738bef1f7ce;hpb=617f35dacbd4e5972bf2d82411b45009bbc79a42;p=public-inbox.git
diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm
index eade524c..2ce7eb8f 100644
--- a/lib/PublicInbox/ProcessPipe.pm
+++ b/lib/PublicInbox/ProcessPipe.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors
+# Copyright (C) 2016-2020 all contributors
# License: AGPL-3.0+
# a tied handle for auto reaping of children tied to a pipe, see perltie(1)
@@ -11,18 +11,31 @@ sub TIEHANDLE {
bless { pid => $pid, fh => $fh }, $class;
}
-sub READ { sysread($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
+sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
sub READLINE { readline($_[0]->{fh}) }
-sub CLOSE { close($_[0]->{fh}) }
+sub CLOSE {
+ my $fh = delete($_[0]->{fh});
+ my $ret = defined $fh ? close($fh) : '';
+ my $pid = delete $_[0]->{pid};
+ if (defined $pid) {
+ # PublicInbox::DS may not be loaded
+ eval { PublicInbox::DS::dwaitpid($pid, undef, undef) };
+
+ if ($@) { # ok, not in the event loop, work synchronously
+ waitpid($pid, 0);
+ $ret = '' if $?;
+ }
+ }
+ $ret;
+}
sub FILENO { fileno($_[0]->{fh}) }
sub DESTROY {
- my $fh = delete($_[0]->{fh});
- close $fh if $fh;
- waitpid($_[0]->{pid}, 0);
+ CLOSE(@_);
+ undef;
}
sub pid { $_[0]->{pid} }