3
man select

BUGS

Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it
may be safer to use O_NONBLOCK on sockets that should not block.

But I see netcat, socat, wget (but not curl) putting FDs without O_NONBLOCK to select or poll.

I've implemented a special library to test apps for this and they are failing...

/* Inspired by seeing a hung wget that was reading from a stale socket and not timing out like it should */

Should I report this as bugs or they are doing it it right?

Possible answers:

  1. "No, blocking FD in poll/select => bug";
  2. "Only AF_INET[6] sockets can misfire on select, so a bug only if a blocking network socket is in poll/select";
  3. "Yes, report bugs only if/when you see a real-world failure because of this" (like in wget).
Vi.
  • 37,014
  • 18
  • 93
  • 148
  • Is there a function to tell how many bytes are available for reading, and is that function in those applications called and it's result checked before calling recv? – xception Oct 16 '12 at 00:27
  • The select and poll functions return how many filehandles _maybe_ available for reading. I don't remember about function to get available bytes. – Vi. Oct 16 '12 at 00:32
  • The function to see how many bytes are available is the traditional `ioctl` example: `ioctl(socket,FIONREAD,&rbytes);` Or the return value from `read` or `recvfrom` or anything ... – Memos Electron Oct 16 '12 at 11:31
  • If an application uses blocking sockets isn't a bug ... this is the way they want to handle the connections. The non blocking sockets are useful on multi-tasking/threading applications and i don't think `netcat`, `socat`, `wget` are one of these applications ... i'm not sure ... never needed to check. – Memos Electron Oct 16 '12 at 11:41
  • The problem is not blocking sockets by itself, but trusting select/poll output and thinking "this time this read won't block". Blocking FDs are good for multi-threaded things. – Vi. Oct 16 '12 at 11:51

0 Answers0