http://www.perlmonks.org?node_id=928200

dd-b has asked for the wisdom of the Perl Monks concerning the following question:

In a fairly simple server (simulator; the actual guts aren't there, just the server infrastructure) using INET sockets running on Centos, I'm getting <$sock> returning undef with $! blank. SIGPIPE is ignored. I'm running out of ideas.

This chunk of code is in a subroutine, called to do all the input from the client:

$iologger->debug("socket blocking state ", $sock->blocking()); my $l = <$sock>; my $err = $!; $iologger->debug("Client input call err ", safe($err), " line ", safe($l)); if (!defined($l)) { my $d = Dumper($sock); $iologger->debug("Socket at error: $d"); die "eiread failed: $err"; } $iologger->info("Client input: ", safe($l)); return $l;

("safe()" is a function that turns undef into "[undef]" and an empty string into "[empty]" and passes through a non-empty string; so the log shows more clearly what's happening.)

Log output below shows it reading a number of lines, down to "h1,h2", and then on the next read call getting undef back. How can it get undef back, without $! being set? (The "Bad file descriptor" messages are lingering $! values, being reported when <$sock> returned a value, and hence don't mean anythying. The code is logging the $! value even when it's not meaningful. Lazy debug code, okay?)

2011-09-27 17:23:14,723 INFO simkserver: Accepted cmdcnt 1 starttime +17:23:14 2011-09-27 17:23:14,744 DEBUG simkserver.io: socket blocking state 1 2011-09-27 17:23:14,745 DEBUG simkserver.io: Client input call err Bad + file desc riptor line Proxy,127.0.0.1 2011-09-27 17:23:14,746 INFO simkserver.io: Client input: Proxy,127.0 +.0.1 2011-09-27 17:23:14,746 INFO simkserver: Connection is from proxy for + 127.0.0.1 2011-09-27 17:23:14,747 DEBUG simkserver.io: socket blocking state 1 2011-09-27 17:23:14,747 DEBUG simkserver.io: Client input call err Bad + file desc riptor line foo,3 2011-09-27 17:23:14,748 INFO simkserver.io: Client input: foo,3 2011-09-27 17:23:14,748 DEBUG simkserver: command foo arg1 3 2011-09-27 17:23:14,749 DEBUG simkserver.io: socket blocking state 1 2011-09-27 17:23:16,557 DEBUG simkserver.io: Client input call err Bad + file desc riptor line h1,h2 2011-09-27 17:23:16,574 INFO simkserver.io: Client input: h1,h2 2011-09-27 17:23:16,575 DEBUG simkserver.io: socket blocking state 1 2011-09-27 17:23:16,575 DEBUG simkserver.io: Client input call err [em +pty] line [undef] 2011-09-27 17:23:16,576 DEBUG simkserver.io: Socket at error: $VAR1 = +bless( \*S ymbol::GEN1, 'IO::Socket::INET' ); 2011-09-27 17:23:16,577 ERROR simkserver: Aborted transaction on excep +tion eirea d failed: at ./simkserver line 103, <GEN1> line 3.

(The logging calls are log4perl.)

Replies are listed 'Best First'.
Re: "Impossible" socket error return
by ikegami (Patriarch) on Sep 27, 2011 at 23:36 UTC

    How can it get undef back, without $! being set?

    readline (for which <$fh> is a shortcut) returns undef on end of file as well as on error.

    By the way, there's no way to know if $! is meaningful after readline because readline has no way of signaling an error occurred.

      Thanks! Yes, it was indeed the obvious. Bug in another bit entirely was causing it to disconnect at an inopportune moment.
Re: "Impossible" socket error return
by onelesd (Pilgrim) on Sep 27, 2011 at 22:51 UTC

    Looks to me like the client finished sending and disconnected, and you get undef when that happens.