cpan-authors / IPC-Run

https://metacpan.org/pod/IPC::Run
Other
21 stars 38 forks source link

Die inside coderef causes escape into the parent process. #97

Open simcop2387 opened 7 years ago

simcop2387 commented 7 years ago

Didn't see this reported yet, but if you die inside a coderef it escapes outside IPC::Run and starts running from where things were before the fork. You can see that the output gets duplicated weirdly in this example.

#!/usr/bin/env perl

use strict;
use warnings;
use IPC::Run qw/harness/;

use Data::Dumper;
use POSIX qw/_exit/;

sub worker {
  # TODO this could be done via IO::Async somehow now without IPC::Run I think.  But it'll take an IO::Async something that supports the namespace stuff
  my $in = '';
  my $out = '';
  my $origpid = $$;
  my $h = harness sub {$|++; print "HI\n"; die "Dying inside coderef";}, '<', \$in, '>&', \$out;
  my ($start_time, $end_time);
  eval {
    $h->start();
    $start_time = time();
    while($h->pumpable()) {
      $h->pump_nb();

      sleep(0.1); # yield some time
      die "Timeout" if (time() - $start_time) > 12;
    }
  };
  $end_time = time();
  my $err = $@;
  $h->kill_kill; # shouldn't be necessary but it's safe
  eval {$h->finish();} if $err;

  print STDERR Dumper({out => $out, pid => $$, opid => $origpid});

  return $out;  
}

worker();
toddr commented 6 years ago

Sounds like we're missing a fork capture somewhere.