Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/Net/Daemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ sub ChildFunc {
}
else {
my $pid = fork();
die "Cannot fork: $!" unless defined $pid;
if ( !defined $pid ) {
$self->Error("Cannot fork: %s", $!);
return;
}
return if $pid; # Parent
$self->$method(@args); # Child
exit(0);
Expand Down
21 changes: 16 additions & 5 deletions t/forkm.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if ( !$ok ) {
plan skip_all => 'This test requires a system with working forks.';
}

plan tests => 10;
my $NUM_CHILDREN = 10;

my ( $handle, $port );
if (@ARGV) {
Expand Down Expand Up @@ -82,18 +82,23 @@ sub MyChild {
return 1;
}

# Spawn 10 children, each running a series of exchanges.
# Spawn children, each running a series of exchanges.
# Children write results to a shared log file; parent collects them.
# If fork() fails (e.g., RLIMIT_NPROC on constrained smokers), stop
# spawning and test only the children we managed to create.
unlink "log";
my %childs;
for ( my $i = 0; $i < 10; $i++ ) {
my $spawned = 0;
for ( my $i = 0; $i < $NUM_CHILDREN; $i++ ) {
my $pid = fork();
if ( !defined($pid) ) {
die "Failed to create new child: $!";
diag("fork() failed at child $i: $! — testing with $spawned children");
last;
}
if ($pid) {
# Parent
$childs{$pid} = $i;
$spawned++;
}
else {
# Child
Expand All @@ -114,6 +119,12 @@ for ( my $i = 0; $i < 10; $i++ ) {
}
}

if ( !$spawned ) {
plan skip_all => "Cannot fork test children: $!";
}

plan tests => $spawned;

# Wait for all children to finish
while ( keys(%childs) > 0 ) {
my $pid = waitpid( -1, 0 );
Expand All @@ -132,7 +143,7 @@ if ( open( my $log_fh, '<', 'log' ) ) {
close($log_fh);
}

for ( my $i = 0; $i < 10; $i++ ) {
for ( my $i = 0; $i < $spawned; $i++ ) {
ok( $results[$i], "forked child " . ( $i + 1 ) . " exchange (1000 rounds)" );
}

Expand Down
Loading