Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-16 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found