Subject: | async process sometimes kills other async processes (plus proposed solution) |
Date: | Tue, 15 Feb 2011 02:59:46 -0800 (PST) |
To: | bug-Async [...] rt.cpan.org |
From: | Sander De Dycker <sanderdedycker [...] yahoo.com> |
Hi,
Since async processes are forked, they also get a copy of any Async object already created. Since Async objects have a destructor that kills the corresponding process, it can happen that one async process kills (some of) the others when it ends.
For example : when two async processes are launched from the main process, the second will have a copy of the Async object for the first. If the second process ends before the first, the destructor of that object copy will ensure that the first process is killed (probably prematurely).
Making these changes to the code fixes the problem (also attached as patch file) :
--- Async.pm
+++ Async_patched.pm
@@ -22,6 +22,7 @@ sub new {
PIPE => $r,
FD => fileno($r),
DATA => '',
+ PARENT_PID => $$
};
bless $self => $pack;
} else { # child
@@ -81,9 +82,11 @@ sub result {
sub DESTROY {
my ($self) = @_;
- my $pid = $self->{PID};
- kill 9 => $pid; # I don't care.
- waitpid($pid, 0);
+ if ($$ == $self->{PARENT_PID}) { # if we created the process, we kill it, otherwise we leave it alone
+ my $pid = $self->{PID};
+ kill 9 => $pid; # I don't care.
+ waitpid($pid, 0);
+ }
}
package AsyncTimeout;
Thank you for sharing this Async module. It's very useful !
Sander De Dycker
Message body is not shown because sender requested not to inline it.