Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Fork in Bot::BasicBot fails to send reply

by neilwatson (Priest)
on Jun 25, 2015 at 20:47 UTC ( [id://1132024]=perlquestion: print w/replies, xml ) Need Help??

neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

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 }

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Fork in Bot::BasicBot fails to send reply
by codiac (Beadle) on Jun 26, 2015 at 10:43 UTC

    If you look at the forkit method in Bot::BasicBot you will see it doesn't use fork, it uses POE::Wheel::Run, so it looks like you might run in to issues there.

    There is nothing in your question to indicate why you want to override forkit, maybe just use the Super's method? If not then maybe just copy the Super's method, then customize it.

Re: Fork in Bot::BasicBot fails to send reply
by RonW (Parson) on Jun 26, 2015 at 00:30 UTC

    If you're running this under MS Windows, then I think the child process does not inherit the socket.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1132024]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-24 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found