Skip to content

Commit

Permalink
fix bug with unwishful kill to childs
Browse files Browse the repository at this point in the history
  • Loading branch information
sugar84 committed Oct 4, 2011
1 parent a1fdd1a commit aa92397
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/MojoX/Run.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ sub DESTROY {
my $proc = $self->{_data}->{$pid};

# kill process (HARD!)
kill(9, $pid);
kill(9, $pid) unless $self->_is_child;
$_log->debug("Killing subprocess $pid with SIGKILL") if (defined $_log);

my $loop = $self->ioloop();
Expand Down Expand Up @@ -845,7 +845,9 @@ sub _spawn {

# spawn command
my $pid = undef;
$self->_is_child(1);
eval { $pid = MojoX::_Open3::open3($stdin, $stdout, $stderr, $o->{cmd}) };
$self->_is_child(0);
if ($@) {
$self->{_error} = "Exception while starting command '$o->{cmd}': $@";
return 0;
Expand Down Expand Up @@ -1401,6 +1403,15 @@ sub _exit_status {
return ($exit_val, $signum, $core);
}

sub _is_child {
my $self = shift;
my $mod = shift || "";

$self->{_is_child} = 1 if $mod;

return $self->{_is_child};
}

=head1 BUGS/CAVEATS
There seem to be problems on some B<OpenBSD, DragonFly and Solaris> systems
Expand Down
29 changes: 29 additions & 0 deletions t/mojox-run-exit-signal.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env perl
use common::sense;
use lib qw(./lib);

use MojoX::Run;
use Test::More tests => 1;

my $run = MojoX::Run->new;

my $arr = [qw(aa bb cc dd ee ff gg)];

my ($ex_stats, $i);
for my $elem (@$arr) {
my $pid = $run->spawn(
cmd => sub {
print $elem;
exit 0;
},
exit_cb => sub {
my ($pid, $res) = @_;

$ex_stats .= $res->{exit_signal};
$run->ioloop->stop if ++$i == scalar @$arr;
},
);
}
$run->ioloop->start;

is( $ex_stats, "0000000", "exit childs with code_ref" );

0 comments on commit aa92397

Please sign in to comment.