0

I see that the poll() system call returns the POLLIN event even after the socket has been closed by the peer. I see both POLLIN and POLLERR set. And this continues to happen even after the read() call has returned -1.

My logic handles POLLERR only if there is no POLLIN to make sure I read any packets that are already arrived before the socket got disconnected. As a result I never end up handling POLLERR since POLLIN is always set.

Why is poll() returning POLLIN along with POLLERR after a socket error? And how should I handle this scenario.

Thanks!

siri
  • 123
  • 1
  • 13

1 Answers1

0

The real question here is why you are still polling on the socket, and indeed why he socket still exists? If you got -1 from read() you should have closed the socket and removed it from the poll set. If you don't do that, POLLIN will recur forever.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I have a async model where a dispatcher thread keeps polling and posts a work item to a worker thread to do the actual read. The worker thread only reads and re-enables for polling. I am relying on the poller to detect errors and handle them. I should probably not handle any events if there is a POLLERR. But as a general question - why would the poll return POLLIN and POLLERR? Doesn't that mean there is data to be read while also having a socket error? – siri May 07 '17 at 01:12
  • 1
    @siri: One of the main reasons why people use `poll()` is so they don't have to dispatch IO to worker threads. Why not just have the same thread do `poll()`, `recv()` and `send()`? – Dietrich Epp May 07 '17 at 01:15
  • I want to have a group of threads that can work on multiple connections simultaneously. And since all of them can't be polling, I ended up with a design where a dispatcher thread does the polling and the worker threads do IO on sockets that are ready? What is the general pattern for doing something like this? – siri May 07 '17 at 01:19
  • @siri: A typical asynchronous design might have one thread that does all the IO, and have it pass buffers back and forth to worker threads. – Dietrich Epp May 07 '17 at 01:22
  • But what if I wanted a group of threads to keep doing the IO on different connections in parallel? Can multiple threads call poll if the same poll thread needs to do read and write? – siri May 07 '17 at 01:26
  • It would return POLLIN because there is still an EOS to be handled on the socket and you haven't handled it. It would return POLLERR if there has been a socket error. Your model is wrong. You should do the I/O in the poll thread and despatch the *data* to the worker threads. – user207421 May 07 '17 at 01:43