Update: Problem solved -- maybe. On a hunch, I eliminated the detach from the threads->create. Now, no more crashes. I'm too sleepy to figure out why this worked or whether there are hidden consequences. Perhaps someone in a different time zone can explain it for me to read tomorrow with my morning coffee...
_______________________________________________

I'm writing a small server using HTTP::Daemon and threads in ActivePerl 5.8.4 under WinXP. Below are the routines used to accept and service connections:

sub RunServer { my $connectno = 0; while (1) { my $daemon; my $port = $Admin{port}; until ( $daemon = HTTP::Daemon->new( LocalAddr => "", LocalPort => $port, Listen => 5 ) ) {sleep 2} my $readable = IO::Select->new; $readable->add($daemon); print "Listening...\n"; while (1) { while (IO::Select->select($readable, undef, undef, 0)) { my $connex = $daemon->accept; threads->create(\&Handler, $connex, ++$connectno)->detach } last if $port != $Admin{port}; sleep .1 } } } sub Handler { my ($connex, $connectno) = @_; my $peeraddr = $connex->peeraddr; $peeraddr = join('.', unpack('C4', $peeraddr)); print "Connection $connectno at " . localtime(time) . " from $peerad +dr\n"; while (my $req = $connex->get_request) { my $req_uri = $req->uri; my $req_content = $req->content; my $req_method = $req->method; print " Connection: $connectno Request: $req_method $req_content +$req_uri\n"; if ($req_method eq 'POST') { DoPost($connex, $peeraddr, $req_content, $req_uri) } elsif ($req_method eq 'GET') { DoGet($connex, $peeraddr, $req_uri) } else { SendNotFound($connex) } } print "Connection $connectno finished.\n"; sleep 1 while ($connex->connected); $connex->close; print "Connection $connectno closed.\n\n"; }
My problem is that defunct connections never seem to close, leaving the threads that service them active. If I remove the line,
sleep 1 while ($connex->connected);
forcing the close and returning from the thread, perl.exe crashes after about five or six connections.

Perhaps someone more experienced with these modules can shed some light...

Thanks,
Phil


In reply to Closing Connections using HTTP::Daemon by Dr. Mu

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.