1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # automatically kill + reap children when this goes out-of-scope
5 package PublicInbox::AutoReap;
10 my (undef, $pid, $cb) = @_;
11 bless { pid => $pid, cb => $cb, owner => $$ }, __PACKAGE__
15 my ($self, $sig) = @_;
16 CORE::kill($sig // 'TERM', $self->{pid});
20 my ($self, $sig) = @_;
21 my $pid = delete $self->{pid} or return;
22 $self->{cb}->() if defined $self->{cb};
23 CORE::kill($sig, $pid) if defined $sig;
24 my $ret = waitpid($pid, 0) // die "waitpid($pid): $!";
25 $ret == $pid or die "BUG: waitpid($pid) != $ret";
30 return if $self->{owner} != $$;