I would like to know if the following scenario is real?!
- select() (RD) on non-blocking TCP socket says that the socket is ready
- following recv() would return EWOULDBLOCK despite the call to select()
For recv()
you would get EAGAIN
rather than EWOULDBLOCK
, and yes it is possible. Since you have just checked with select()
then one of two things happened:
select()
and recv()
.I am aware of an error in a popular desktop operating where O_NONBLOCK
TCP sockets, particularly those running over the loopback interface, can sometimes return EAGAIN
from recv()
after select()
reports the socket is ready for reading. In my case, this happens after the other side half-closes the sending stream.
For more details, see the source code for t_nx.ml
in the NX library of my OCaml Network Application Environment distribution. (link)
Yes, it's real. Here's one way it can happen:
A future modification to the TCP protocol adds the ability for one side to "revoke" information it sent provided it hasn't been received yet by the other side's application layer. This feature is negotiated on the connection. The other side sends you some data, you get a select
hit. Before you can call recv
, the other side "revokes" the data using this new extension. Your read
gets a "would block" error because no data is available to be read.
The select
function is a status-reporting function that does not come with future guarantees. Assuming that a hit on select
now assures that a subsequent operation won't block is as invalid as using any other status-reporting function this way. It's as bad as using access
to try to ensure a subsequent operation won't fail due to incorrect permissions or using statfs
to try to ensure a subsequent write won't fail due to a full disk.