+SKIP: {
+ my $pid = spawn(['true'], undef, { pgid => 0 });
+ ok($pid, 'spawned process with new pgid');
+ is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
+ is($?, 0, 'true exited successfully');
+ pipe(my ($r, $w)) or BAIL_OUT;
+ $pid = eval { spawn(['true'], undef, { pgid => 1, 2 => $w }) };
+ close $w;
+ my $err = do { local $/; <$r> };
+ # diag "$err ($@)";
+ if (defined $pid) {
+ waitpid($pid, 0) if defined $pid;
+ isnt($?, 0, 'child error (pure-Perl)');
+ } else {
+ ok($@, 'exception raised');
+ }
+}
+
+{ # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
+ my $script = <<'EOF';
+$| = 1; # unbuffer stdout
+defined(my $pid = fork) or die "fork: $!";
+if ($pid == 0) { exit }
+elsif ($pid > 0) {
+ my $waited = waitpid(-1, 0);
+ $waited == $pid or die "mismatched child $pid != $waited";
+ $? == 0 or die "child err: $>";
+ $SIG{CHLD} = sub { print "HI\n"; exit };
+ print "RDY $$\n";
+ select(undef, undef, undef, 0.01) while 1;
+}
+EOF
+ my $oldset = PublicInbox::DS::block_signals();
+ my $rd = popen_rd([$^X, '-e', $script]);
+ diag 'waiting for child to reap grandchild...';
+ chomp(my $line = readline($rd));
+ my ($rdy, $pid) = split(' ', $line);
+ is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
+ ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
+ is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
+ ok(close $rd, 'popen_rd close works');
+ PublicInbox::DS::sig_setmask($oldset);
+}
+