http://qs1969.pair.com?node_id=11118278


in reply to Improve my tcp-mqtt server

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