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.


In reply to Forking within AnyEvent::Socket::tcp_server? [SOLVED] by Riales

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.