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

This is perhaps more of a general networking question than an Perl question, but I'm writing a multiplexed server using IO::Socket::INET, and I notice that some of the sockets I get back from accept() have no peer host. Here's the framework:
#!/usr/bin/perl # Pragma use strict; use Carp qw(cluck); use IO::Socket; use IO::Select; # Default port(s) on which to listen our @PORTS = qw(1234 1235 1236); # Setup IO::Select objects to manage the sockets. our $LISTENERS = IO::Select->new() or die "IO::Select"; our $CLIENTS = IO::Select->new() or die "IO::Select"; # Open our listening ports foreach my $port (@PORTS) { my $listen_socket = IO::Socket::INET->new( LocalPort => $port, Listen => 20, Proto => 'tcp', Reuse => 1 ); die "Bind failed for port $port: $@" unless $listen_socket; $LISTENERS->add($listen_socket); } print "Waiting for connections\n"; while (1) { # Look for incoming connections my @ready = $LISTENERS->can_read(1); foreach my $socket (@ready) { my $connect = $socket->accept(); if ($connect) { if (!$connect->peerhost()) { cluck("NULL PEERHOST: " . join(" : ", $connect->sockhost(), $connect->sockport(), $connect->peerhost(), $connect->peerport(), $! )); close($connect); next; } print "Connection from ", $connect->peerhost(), " on port ", $connect->sockport(), "\n"; $CLIENTS->add($connect); } } @ready = $CLIENTS->can_read(1); foreach my $socket (@ready) { my $input; my $bytes = sysread($socket, $input, 1024); if ($bytes) { print "Read from client: $input"; } else { print "Disconnect from ", $socket->peerhost(), " on port ", $socket->sockport(), "\n"; $CLIENTS->remove($socket); close($socket); } } }

The question I have is related to that cluck(). Sometimes accept() returns an IO::Socket object, but it has no peerhost. The script output looks like this:

NULL PEERHOST: X.X.X.X : 1234 :  :  : Transport endpoint is not connected at ./ line 45
What causes that?


Replies are listed 'Best First'.
Re: Transport endpoint not connected
by thospel (Hermit) on Mar 16, 2005 at 20:01 UTC
    This should be pretty much impossible and I at least am unable to reproduce it. Do you have some way to trigger this so we can see the effect too ? On which OS and version is this ? If you use the list-form of accept, what is the value of the peer information you get from that for the case the getpeername(2) calls fail ? Can you get an strace/truss from around when this happens ?

      I'm afraid I don't have any easy way to recreate it, other than throwing a lot of clients at it. It's perl 5.8.4 running under Linux 2.6.8. If I use the list form of accept, I get back the peer address correctly. Here's some debugging code:

      foreach my $socket (@ready) { print STDERR "Start accept\n"; my ($connect, $paddr) = $socket->accept(); my ($port, $iaddr) = sockaddr_in($paddr); print STDERR "Peer: ", inet_ntoa($iaddr), ":$port\n"; if ($connect) { if (!$connect->peerhost()) { cluck("NULL PEERHOST: " . join(" : ", $connect->sockhost(), $connect->sockport(), $connect->peerhost(), $connect->peerport(), $! )); close($connect); print STDERR "End accept (no peerhost) +\n"; next; } $CLIENTS->add($connect); print STDERR "End accept (success)\n"; next; } print STDERR "End accept (no connect)\n"; }

      Output looks like this:

      Start accept
      Peer: X.X.X.X:15627
      NULL PEERHOST: Y.Y.Y.Y : 1235 :  :  : Transport endpoint is not connected at ./ line 115
      End accept (no peerhost)
      Where X.X.X.X is the client ip and Y.Y.Y.Y is my server ip. Perhaps this is an IO::Socket::INET bug?

      On a related note, I'm also getting segfaults from perl under heavy load (~2500+ clients). The segfaults don't seem to be related to the null peerhosts. I believe they're occuring on syswrite() calls when I'm sending text back to the clients.


Re: Transport endpoint not connected
by beauregard (Monk) on Mar 16, 2005 at 23:11 UTC
    I've seen this happen to daemons when a badly configured client is hammering them with connections that drop immediately. After enough of these, the server gets a connection, but then it gets an ENOTCONN error when it tried to get a peer address. I assume it's something to do with operation system tables filling up with half-open connections.

    This was on a Linux box.