Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Forking within AnyEvent::Socket::tcp_server? [SOLVED]

by Riales (Hermit)
on Sep 23, 2013 at 21:36 UTC ( #1055388=perlquestion: print w/replies, xml ) Need Help??
Riales has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I'm trying to write a forking server using AnyEvent::Socket::tcp_server, but am running into an issue with the forking. Here is what I am trying to do right now:

use AnyEvent::Socket; use AnyEvent::Handle; use Parallel::ForkManager; # Some omitted initialization stuffs my $forker = Parallel::ForkManager->new(10); AnyEvent::Socket::tcp_server(undef, $port, sub { my ($fh, $host, $port) = @_; $forker->start and return; my $handle = AnyEvent::Handle->new( fh => $fh, keepalive => 1, on_connect => $on_connect_sub, on_read => $on_read_sub, on_error => $on_error_sub, ); }, ); AnyEvent->condvar->wait();

This is the error I get sometimes (but not always) while connecting:

Cannot start another process while you are in the child process at /us +r/local/share/perl/5.14.2/Parallel/ line 476.

If I comment out the line that does the forking, the server seems to run fine (it just doesn't fork). Does anybody know what I am doing wrong with the forking?


EDIT: Figured it out after some thought. I'm still a bit hazy on the details so I'm not sure I can vocalize it all properly, BUT here's what I think was happening:

When the code accepted a connection, I would fork and the child would create a handler. While it was great that the handler was in the child thread, I think the child could also still accept connections. I'm not sure how this part was arbitrated, but sometimes, an incoming connection would be accepted by the child, but because the child could not fork, both the original connection and the incoming connection would fail due to the failed fork.

Hope that makes sense. Hopefully somebody more knowledgable will come along and shed more light on the situation.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055388]
Approved by marinersk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-03-24 14:13 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (298 votes). Check out past polls.