--- AnyEvent/lib/AnyEvent.pm 2007/11/16 09:13:11 1.36 +++ AnyEvent/lib/AnyEvent.pm 2007/11/25 14:08:12 1.38 @@ -111,7 +111,7 @@ }); # to cancel the timer: - undef $w + undef $w; =head2 CONDITION WATCHERS @@ -254,7 +254,7 @@ use Carp; -our $VERSION = '2.6'; +our $VERSION = '2.8'; our $MODEL; our $AUTOLOAD; @@ -375,11 +375,12 @@ our %PID_CB; our $CHLD_W; +our $CHLD_DELAY_W; our $PID_IDLE; our $WNOHANG; sub _child_wait { - while (0 <= (my $pid = waitpid -1, $WNOHANG)) { + while (0 < (my $pid = waitpid -1, $WNOHANG)) { $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), (values %{ $PID_CB{0} || {} }); } @@ -387,6 +388,14 @@ undef $PID_IDLE; } +sub _sigchld { + # make sure we deliver these changes "synchronous" with the event loop. + $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub { + undef $CHLD_DELAY_W; + &_child_wait; + }); +} + sub child { my (undef, %arg) = @_; @@ -400,9 +409,9 @@ } unless ($CHLD_W) { - $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); - # child could be a zombie already - $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); + $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); + # child could be a zombie already, so make at least one round + &_sigchld; } bless [$pid, $arg{cb}], "AnyEvent::Base::Child"