Greetings avout,
I have some bot (running on Linux) code using Bot::BasicBot. When I fork a search process it returns the data but there is no reply to the channel. Without forking the reply works. Why? I'm guessing that the child doesn't know something that the parent does, but I don't know what. I've cut down the code the relevant bits:
# Start the bot
my $bot = Cfbot->new( %{ $c->{irc} } )->run;
package Cfbot;
use base 'Bot::BasicBot';
use Data::Dumper;
my @kids;
#
# Subs that override Bot::BasicBot's own subs
#
sub said
{
# ... do stuff then call a forkit sup.
$self->forkit(
run => \&main::lookup_topics,
arg => $keyword,
msg => $msg
);
}
sub forkit
{
my $self = shift;
my %args = @_;
warn '%args '.Dumper( \%args );
my $msg = $args{msg};
warn 'self->{msg}'. Dumper( $self->{msg});
my $childpid = fork;
if ( 0 == 0 ) # <<< fork disabled
#if ( ! defined $childpid ) # <<< fork enabled
{
warn "Failed to fork, processing in foreground instead.";
my $replies = $args{run}->( $args{arg} );
$self->reply( $msg, $_ ) foreach ( @{ $replies } ); # <<< WORKS
}
elsif ( 1 == 0 ) # <<< fork disabled
#elsif ( $childpid == 0 ) # <<< fork enabled
{
# This is the child process
my $replies = $args{run}->( $args{arg} );
warn 'Child $replies '. Dumper( \$replies ) if $args->{debug};
#####
# TODO why does this reply fail?
#####
$self->reply( $msg, $_ ) foreach ( @{ $replies } ); # <<< FAILS
exit 0;
}
else
{
# This is the parent process.
# Keep record of kids to kill later and prevent zombies.
push @kids, $childpid;
waitpid $childpid, 0;
}
}
END { kill 15, @kids }