]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ProcessPipe.pm
run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / ProcessPipe.pm
index e088c1053f0b08877a277f281f3192925bac791d..30509d022842fdb75d5404a9afbc2beca24022b2 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2019 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)
@@ -11,17 +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 { delete($_[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 {
-       delete($_[0]->{fh});
-       waitpid($_[0]->{pid}, 0);
+       CLOSE(@_);
+       undef;
 }
 
 sub pid { $_[0]->{pid} }