Re: PROBLEM: select() says closed socket readable

Ton Hospel (linux-kernel@ton.iguana.be)
Sat, 18 Aug 2001 22:52:42 +0000 (UTC)


In article <200108181627.UAA19351@ms2.inr.ac.ru>,
kuznet@ms2.inr.ac.ru writes:
> Hello!
>
>> For linux 2.4.2, select() indicates socket ready for read on a
>> socket that's never been connected.
>
> Right. It does not block on read, hence it is readable.
>

mm, that's not really the rule, since e.g. you want to be able to select
on read and write on the write side of a pipe, and would expect to only
get readable on error/close:

But there's weird stuff going on here (examples need perl >= 5.6.0):

perl -wle 'use POSIX; pipe(my $rd, my $wr)||die $!; my $fd = fileno($wr); my $mask=""; vec($mask, $fd, 1)=1; select(my $r=$mask, my $w=$mask, "", undef); print "readable" if vec($r, $fd, 1); print "writable" if vec($w, $fd, 1); my $rc = POSIX::read($fd, my $buf, 4096) || die "read returns $!"; print STDERR $rc'

prints:

writable
read returns Bad file descriptor at -e line 1.

Which is in fact weird, since I still have the $rd descriptor around, so
the read should block (it does on solaris). This seems a bug.

perl -wle 'use POSIX; pipe(my $rd, my $wr)||die $!; close $rd; my $fd = fileno($wr); my $mask=""; vec($mask, $fd, 1)=1; select(my $r=$mask, my $w=$mask, "", undef); print "readable" if vec($r, $fd, 1); print "writable" if vec($w, $fd, 1); my $rc = POSIX::read($fd, my $buf, 4096) || die "read returns $!"; print STDERR $rc'

prints:

readable
writable
read returns Bad file descriptor at -e line 1.

Which is the behaviour you want (the read does not block in either case),
closing the other side causes readability. This allows you to watch a
connection for errors without being bothered by EOF (so it's good, but
contrary to your statement).
However, the reaction on read is still weird. on solaris I get in fact EOF,
not Bad file descriptor

In fact, somewhat related, i last week ran into this:

perl -wle 'use Socket; use POSIX; socketpair(my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC)||die $!; shutdown($rd, 1); shutdown($wr, 0); my $fd = fileno($wr); my $mask=""; vec($mask, $fd, 1)=1; select(my $r=$mask, my $w=$mask, "", undef); print "readable" if vec($r, $fd, 1); print "writable" if vec($w, $fd, 1); my $rc = POSIX::read($fd, my $buf, 4096) || die "read returns $!"; print STDERR $rc'

which prints:

readable
writable
0 but true

(so the read returns EOF). Which is very unfortunate, because it means a
pipe is not equivalent to a socketpair where you make one side only
readable and one only writable (the way it's supposedly implemented on
some OSes) Solaris and freebsd behaves the same as linux by the way.
On the other hand, you *do* want to know if the other side does a
shutdown, so in that sense the behaviour is good.

Damn, i wish this socket stuff would finally get completely standardized.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/