0

Is it possible that a write() call on a socket has failed but the poll() doesn't detect any error? Are there any category of errors that can cause the write to fail but are not considered an error by the poll() system call?

I have a dispatcher thread that keeps monitoring the sockets and is responsible for detecting and handling socket errors. I have a worker thread that does the actual read and write on the sockets when notified by the dispatcher thread. The write() calls by the worker thread fails but the poll() system call by the dispatcher thread never reports back an error. How can this happen!

siri
  • 123
  • 1
  • 13

1 Answers1

3

If write() fails, it returns -1 and sets errno. This is the only indication you get of the error. If you need to dispatch that error somewhere else, you need to do that yourself.

poll() will separately detect certain exceptional conditions which could cause this error, like a closed TCP connection. This is not the same as "detecting" the failed write() -- poll() does not, and cannot, detect those errors.

  • Wouldn't the write() fail only due to socket errors that would be detected by the poll() too? – siri May 23 '17 at 00:14
  • 1
    @siri Not for all errors. Example: if a socket is set as non-blocking, writes will return EAGAIN if the buffer is full. Or, if the socket is UDP, errors like EHOSTUNREACH may come and go as the network state changes. –  May 23 '17 at 00:17
  • `EINTR` (if you have interrupting signal handlers installed) would be another possible error `poll` could not see. – R.. GitHub STOP HELPING ICE May 23 '17 at 01:01
  • @R.. Also true! I didn't mean to suggest that those were the _only_ possible errors -- just a couple of the more obvious examples. –  May 23 '17 at 01:01