http://www.perlmonks.org?node_id=1055388

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/ForkManager.pm 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.