Visionary has asked for the wisdom of the Perl Monks concerning the following question:

I'm going nuts over here. And now that I've almost pulled out all the hair - so I've got to ask for a hand.

I've got a sever background process running. It accepts connections, does its stuff and works fine... Unless a client gets disconnected without logging out.

If a client losses his connection to the server, in a matter of 5 seconds the server process will be using 95 - 99% of the CPU. I don't know whats causing the sudden pig behavor.... I've tried all kinds of error checking (and dropping clients that seem bad) but to no avail.

It should be noted, that hundreds of clients can connect and logout properly and this server doesn't even register on TOP... But the second one drops its connection... BOOM.

Here is my code: (please excuse the mess)

#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; use POSIX; # Become a daemon my $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined($pid); POSIX::setsid() or die "Can't start a new session: $!"; my ($msg, $nread, $in, $message); my $max_msglen = 1024; my $max_clients = 1; my $port = 9000; $0 = "hcdemo: Accepting clients on port $port"; my $new_client = IO::Socket::INET->new(Proto=>"tcp", LocalPort=>$p +ort, Listen=>$max_clients, Reuse=>1); my $sel = IO::Select->new($new_client); print "listening at port $port\n"; main(); sub main { # ------------------------------------------------------------------- # while (my @ready = $sel->can_read) { foreach my $client (@ready) { if ($client == $new_client) { # New connection my $add = $client->accept; $sel->add($add); # $msg = "LIVEHELP\n"; # syswrite($add, $msg, length($msg)); } else { # Existing connection $msg = ""; $nread = sysread($client, $msg, $max_msglen); chop($msg); chop($msg); if ($msg =~ /alex/i) { $msg = "Other user says to kiss his ass\n"; foreach ($sel->can_write) { if ($_ ne $client) { syswrite($_, $msg, length($msg)); } } } elsif ($msg =~ /end/i) { my($end, $msg) = split(/\|/,$msg); foreach ($sel->can_write) { if ($_ ne $client) { syswrite($_, $msg, length($msg)); } } $sel->remove($client); } elsif ($msg =~ /update/i) { my $users="Ususally I sub to count users"; $msg = "$users\n"; syswrite($client, $msg, length($msg)); } elsif ($nread) { foreach ($sel->can_write) { if ($_ ne $client) { if (!syswrite($_, $msg, length($msg))) { $sel->remove($_); } } } } } } } } sub sendall { my ($msg) = @_; foreach ($sel->can_write) { if (!syswrite($_, $msg, length($msg))) { $sel->remove($_); } } } sub getmsg { my ($handle) = @_; my $msg = ""; do { $nread = sysread($handle, $in, 1024); $msg .= $in; } while ($nread > 0); return($msg); }
Any help here would be sooo apprecaited.

Thanks,

Alex

Replies are listed 'Best First'.
Re: io::select bogs cpu on disconnect
by gbarr (Monk) on Oct 03, 2001 at 23:45 UTC
    When a client closes its connection or gets disconnected select() will report it as being readable. However when you actually read from it sysread will return zero. You should check the result of sysread for this condition and close the connection, but you do not. Because you do not, the select() will continue to report the handle as readable and so you have a loop that never sleeps
      Gbarr,

      Thank you I owe you my sanity. I was only trapping the writes; not the reads. One simple line at your suggestion:
      if (!$nread) { $sel->remove($client); }
      did the trick.

      Again, thanks!

      Alex

      PS - This is my first visit here, and it is like the holy grail I've been searching for.... You guys are great.