2

My application checks for incoming data in a TCP socket by calling poll() with zero timeout. 99% of the time it works fine, when there are data on the socket it sets the POLLIN flag. Sometimes however there are data on the socket(I see the message on wireshark), but poll() returns 0(timed out) and does not set the POLLIN flag. It will set the POLLIN flag after several seconds when another message comes.

Is that normal behavior? Does poll() always checks for incoming data at least once before timing out?

According to the answers in another question, Linux man pages are not very clear about that.

Community
  • 1
  • 1
Chilon
  • 31
  • 4
  • 1
    Are you certain you're seeing the complete data, i.e. not a TCP segment of data where the previous data have been lost, and the TCP stack is thus waiting for a retransmission ? Or if it's UDP, the UDP packet might get dropped ? – nos Dec 02 '13 at 21:38
  • Can you provide a code snippet with poll() call? – Michael Dec 02 '13 at 21:43
  • 2
    `poll` for `POLLIN` with a zero timeout means no more than _"look if there is any (valid) data in the receive buffer that I could read immediately, otherwise return immediately"_. It doesn't mean "verify if any datagrams have arrived on the wire" or any other thing. Have you verified that the datagram's checksum is valid for these datagrams seen by Wireshark and that they're not e.g. resends? Poll would obviously not report POLLIN in such a case, since nothing will be placed in the receive buffer. – Damon Dec 02 '13 at 23:01
  • 3
    The Linux manpage is (in my opinion) very clear about it, too. `poll` never checks for _incoming_ data, it checks for data that _has come in_ (and yes, it always checks exactly once (not more often) _or_ it blocks for the time you specify). – Damon Dec 02 '13 at 23:04

1 Answers1

1

I reproduced the bug with a timeout of 10 millisecs passed in poll(). After further investigation I found out that the problem was caused by a bug in a library that wraps the unix socket API. It was loading data from the socket into a buffer, and then it was polling the socket for further data before consuming all the bytes from the buffer. Thanks for the comments anyway.

So it seems that poll() works fine even with a timeout of 0.

Chilon
  • 31
  • 4