shutdown() and SHUT_RD on TCP sockets - broken?
Subsystems (2)
Hello,
There was no response to the note below. Is netdev the right place to raise this subject?
Cheers
Michael
------- Forwarded message follows -------
Date sent: Tue, 1 Jul 2003 13:23:49 +0200 (MEST) From: [email protected] To: [email protected] BCC to: [email protected] Subject: shutdown() and SHUT_RD on TCP sockets - broken?
Hello,
I've done quite some searching, but have so far not found an answer to the question of why does the behaviour described below occur on Linux...
According to SUSv3, if we perform a shutdown(fd, SHUT_RD) on a socket, then further reads on that socket should be disabled. In the AF_UNIX domain, all is fine -- things operate as I expect. However, for TCP sockets, things are different (tested on 2.2.14, and 2.4.20):
1. If we perform a read() on the socket and there is no data, then 0 (EOF) is (immediately) returned. (This is what I expected.)
2. However, the peer can still write() to the socket, and afterwards we can read() that data from the socket, even though the reading half of the socket should be shut down. Instead of this behaviour, I expected the read() to continue to return 0 as in point 1. This is what we see for example in FreeBSD 4.8, Tru64 5.1B, and HP/UX 11.
I thought that most implementations (other than Linux) did things this way, but I've just now gone and tested things on Solaris 8, and it seems to behave in the same way as Linux.
I've read the relevant source code to confirm the anomalous behaviour described here. But, why do things happen in this way on Linux?
3. (A side point.) Looking at Stevens UNPv1, p161, there is a statement that after a SHUT_RD, "any data for a TCP socket is acknowledged and then silently discarded". This implies to me that the sender could keep on writing to the socket and never block. However, on Linux, if the peer keeps sending to a socket, then eventually (the channel is filled and) it blocks. I see that this also occurs on FreeBSD 4.8, Tru64 5.1B, HP/UX 11 and Solaris 8. Have I misunderstood Stevens, or has something changed since the implementation he described (or was his statement wrong)? (In the AF_UNIX domain on Linux, the peer gets SIGPIPE/EPIPE if it keeps writing after a local SHUT_RD.)
Thanks
Michael
Signature · 3 lines
+++ GMX - Mail, Messaging & more http://www.gmx.net +++ Jetzt ein- oder umsteigen und USB-Speicheruhr als Prämie sichern!
Quoted 4 lines · On Tue, 8 Jul 2003 [email protected] wrote:
Hello,
There was no response to the note below. Is netdev the right place to raise this subject?
Yes. I believe Alexey has some reservations about the specified behavior.
- James
Signature · 2 lines
James Morris <[email protected]>
Quoted 8 lines · On Tue, 8 Jul 2003 18:09:14 +0200 (MEST) [email protected] wrote:
1. If we perform a
read()on the socket and there is no data, then 0 (EOF) is (immediately) returned. (This is what I expected.)2. However, the peer can still
write()to the socket, and afterwards we canread()that data from the socket, even though the reading half of the socket should be shut down. Instead of this behaviour, I expected theread()to continue to return 0 as in point 1. This is what we see for example in FreeBSD 4.8, Tru64 5.1B, and HP/UX 11.
The problem is that it adds a new check to the input path. It's not clear how the check can be done outside the fast path (one way would be to shrink the window forcedly and drop the receiver into slow path, but that would be a severe protocol violation if the shrunk window leaks out with some ACK). I don't think it's a good idea to add a check for such an obscure situation to the fast path.
Quoted 7 lines
3. (A side point.) Looking at Stevens UNPv1, p161, there is a statement that after a
SHUT_RD, "any data for a TCP socket is acknowledged and then silently discarded". This implies to me that the sender could keep on writing to the socket and never block. However, on Linux, if the peer keeps sending to a socket, then eventually (the channel is filled and) it blocks. I see that this also occurs on FreeBSD 4.8, Tru64 5.1B, HP/UX 11
That's because the data is not discarded so the window fills.
Quoted 2 lines
and Solaris 8. Have I misunderstood Stevens, or has something changed since the implementation he described (or was his statement wrong)? (In
Probably Stevens was confused.
-Andi
Hello!
Quoted 2 lines
blocks. I see that this also occurs on FreeBSD 4.8, Tru64 5.1B, HP/UX 11 and Solaris 8. Have I misunderstood Stevens,
Most likely, it is that rare case when Stevens forgot to check the statement.
Quoted 1 line
From viewpoint of TCP the behaviour described in Stevens' book
is highly unnatural. SHUT_RD on TCP does not make any sense.
Quoted 1 line
described here. But, why do things happen in this way on Linux?
Actually, you could check one more thing. What does happen after freebsd 4.8 returns 0 on read()? Does it open window eventually?
As you checked, all the stacks ignore SHUT_RD, when receiving data and queue it anyway. And when read()ing Linux and, apparently Solaris, prefer to return this data.
Alexey