From d59a9cb667f106b29363795539b55116077bcd80 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 31 Dec 2020 13:51:49 +0000 Subject: use PublicInbox::DS for dwaitpid This simplifies our code and provides a more consistent API for error handling. PublicInbox::DS can be loaded nowadays on all *BSDs and Linux distros easily without extra packages to install. The downside is possibly increased startup time, but it's probably not as a big problem with lei being a daemon (and -mda possibly following suite). --- lib/PublicInbox/ProcessPipe.pm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'lib/PublicInbox/ProcessPipe.pm') diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm index c9234f42..afbb048d 100644 --- a/lib/PublicInbox/ProcessPipe.pm +++ b/lib/PublicInbox/ProcessPipe.pm @@ -5,6 +5,7 @@ package PublicInbox::ProcessPipe; use strict; use v5.10.1; +use PublicInbox::DS qw(dwaitpid); sub TIEHANDLE { my ($class, $pid, $fh, $cb, $arg) = @_; @@ -25,19 +26,21 @@ sub PRINT { print { $self->{fh} } @_; } +sub adjust_ret { # dwaitpid callback + my ($retref, $pid) = @_; + $$retref = '' if $? +} + 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) }; - - if ($@) { # ok, not in the event loop, work synchronously - waitpid($pid, 0); - $ret = '' if $?; - $cb->($arg, $pid) if $cb; + unless ($cb) { + $cb = \&adjust_ret; + $arg = \$ret; } + dwaitpid $pid, $cb, $arg; } $ret; } -- cgit v1.2.3-24-ge0c7