Ah yes, the perils of async processing

The place that "blocks" is $client_socket = $socket->accept();

That wont return until a new socket is ready. That is where i would focus my debug efforts. Put debug prints before and after that line. There are two other datums i would like to see, the absolute number of connections and the number of items in @clients. My guess is that you will see it happens after a certain number of inbound connections.

Why is that blocking point important when you think you are mostly using threads? Because if it cannot get a new connection it will not run your "join-cleanup" section. And if a number of threads are joinable and they are not reaped strange things can happen. If the place it hangs is on the 64th/65th connection suspect this.

The next thing i noticed was that nothing ever leaves @clients. If you dont remove the thread objects from that array they wont ever be DESTROYED and strange things will happen. Since the socket is localed to the thread the socket is never "closed" either. If this was the case again i would expect to see it happen on some number that is some power of 2. But this doesnt matter anyway.

Please explain to me how you expect to reach the line

print "Client exit from ".$user{peer_address}.":".$user{peer_port}."\n +";
Once you realize it cannot be reached you also realize nothing is ever joinable. so even if $client_socket is no longer a operating socket that loop never leaves. Every 5 min it tries to read/write to the socket and just fails. Id expect the prints to throw a warning at least but it is possible they instead block because the socket is not writable.

Even fixing those problems you are still stuck with the accept blocking and preventing any cleanup. fixing that is not so easy. You will find that to solve this problem most use IO::Select;

Ive been playing with this since before IO:Select. some untested cut&paste code for you to look at.

my $server = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => "0.0.0.0", LocalPort => $main::PORT, Listen => SOMAXCONN, Reuse => 1); my $sel_read = IO::Select->new(); my $sel_write = IO::Select->new(); my $sel_error = IO::Select->new(); $sel_read-> add($server); my $timeout=50; my %clients; superloop:while(1) { my ($rd_ptr,$wr_ptr,$er_ptr)=IO::Select->select($sel_read,undef,$sel +_error,$timeout) ; # or die "Error in select: $! "; my $outtime=time; eachfh: for my $fh (@$rd_ptr) { if ($fh == $server) { my $client_socket = $server->accept; $sel_read->add($client_socket); $client_socket->autoflush(1); ## $clients {$client_socket}={ socket=>$client_socket ,thread=>threads->create( \&clientHandler, $client_so +cket ) }; ## } # fh = server ## my @opened=keys(%clients); my @eofable=(); foreach $open ( @opened ) { my $thread=$opened{$open}{thread}; if( $thread->is_joinable() ) { $thread->join(); push @eofable,$open; } } foreach $open ( @eofable ) { my $socket=$opened{$open}{socket}; # magic lost in string +ified key close($socket); $sel_read->remove($socket) if ($sel_read->exists($socket +)) ; $sel_write->remove($socket) if ($sel_write->exists($socke +t)); $sel_error->remove($socket) if ($sel_error->exists($socke +t)); delete $opened{$open}; # destroys socket and thread objec +ts; } ## } # superloop


In reply to Re: Improve my tcp-mqtt server by huck
in thread Improve my tcp-mqtt server by leostereo

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.