Skip to contentSkip to conversation

Linux poll() <sigh> again

DISCUSSION Robert Hancock 6 participants 8 messages May 12, 2006 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 10 lines · linux-os (Dick Johnson)

The bug relates to Linux implementation of poll() on a connected socket. If poll() is set to detect changes on a connected socket, with an infinite timeout (-1), and the client disconnects, it returns with a positive value (correct). The returned events (revents member), shows only POLLIN bit set. This, according to all known documentation including man pages on the web, is supposed to mean that there are data to be read. In fact, there are no data and a read will return 0.

According to the Single UNIX Specification:

http://www.opengroup.org/onlinepubs/007908799/xsh/poll.html

POLLIN means "Data other than high-priority data may be read without blocking. For STREAMS, this flag is set in revents even if the message is of zero length." The way I read it, all this is telling you is that a read on that file descriptor will not block at that particular moment. It doesn't mean there is actually any data to be read. On a device like a socket, read returning 0 tells you that the connection's been closed.

POLLHUP means "The device has been disconnected." This would obviously be appropriate for a device such as a serial line or TTY, etc. but for a socket it is less obvious that this return value is appropriate.

Quoted 6 lines

I have used the subsequent read() with a returned value of zero, to indicate that the client disconnected (as a work around). However, on recent versions of Linux, this is not reliable and the read() may wait forever instead of immediately returning.

If you want nonblocking behavior, you should set the socket to nonblocking. This is a bit strange though, unless the data was stolen by another thread or something. Are you sure you've seen this?

Signature · 3 lines
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from [email protected]
Home Page: http://www.roberthancock.com/
#2 lore raw
Quoted 23 lines · Robert Hancock
Quoted 10 lines · linux-os (Dick Johnson)

The bug relates to Linux implementation of poll() on a connected socket. If poll() is set to detect changes on a connected socket, with an infinite timeout (-1), and the client disconnects, it returns with a positive value (correct). The returned events (revents member), shows only POLLIN bit set. This, according to all known documentation including man pages on the web, is supposed to mean that there are data to be read. In fact, there are no data and a read will return 0.

According to the Single UNIX Specification:

http://www.opengroup.org/onlinepubs/007908799/xsh/poll.html

POLLIN means "Data other than high-priority data may be read without blocking. For STREAMS, this flag is set in revents even if the message is of zero length." The way I read it, all this is telling you is that a read on that file descriptor will not block at that particular moment. It doesn't mean there is actually any data to be read. On a device like a socket, read returning 0 tells you that the connection's been closed.

Quoted 4 lines

POLLHUP means "The device has been disconnected." This would obviously be appropriate for a device such as a serial line or TTY, etc. but for a socket it is less obvious that this return value is appropriate.

Hardly "less obvious". SunOs has returned POLLHUP as has other Unixes like Interactive, from which the software was ported. It went from Interactive, to SunOs, to Linux. Linux was the first OS that required the hack. This was reported several years ago and I was simply excoriated for having the audacity to report such a thing. So, I just implemented a hack. Now the hack is biting me. It's about time for poll() to return the correct stuff.

Quoted 11 lines
Quoted 6 lines

I have used the subsequent read() with a returned value of zero, to indicate that the client disconnected (as a work around). However, on recent versions of Linux, this is not reliable and the read() may wait forever instead of immediately returning.

If you want nonblocking behavior, you should set the socket to nonblocking. This is a bit strange though, unless the data was stolen by another thread or something. Are you sure you've seen this?

I don't use threads. The hang under the specified conditions was first observed on 2.6.16.4 (that I'm running on this system). The hack, previously used, i.e., the read of zero was used since 2.4.x with success except it's a hack and shouldn't be required. It was not ever required on SunOs from which the software was ported.

Quoted 6 lines

--
Robert Hancock Saskatoon, SK, Canada
To email, remove "nospam" from [email protected] Home Page: http://www.roberthancock.com/

Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.4 on an i686 machine (5592.89 BogoMips). New book: http://www.lymanschool.com
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#3 lore raw
Quoted 5 lines · linux-os (Dick Johnson)
Quoted 4 lines

POLLHUP means "The device has been disconnected." This would obviously be appropriate for a device such as a serial line or TTY, etc. but for a socket it is less obvious that this return value is appropriate.

Quoted 8 lines

Hardly "less obvious". SunOs has returned POLLHUP as has other Unixes like Interactive, from which the software was ported. It went from Interactive, to SunOs, to Linux. Linux was the first OS that required the hack. This was reported several years ago and I was simply excoriated for having the audacity to report such a thing. So, I just implemented a hack. Now the hack is biting me. It's about time for poll() to return the correct stuff.

The standard doesn't require that a close on a socket should report POLLHUP. Thus this behavior may differ between UNIX implementations. If your software is requiring a POLLHUP to indicate the socket is closed I think it is being unnecessarily picky since read returning 0 universally indicates that the connection has been closed. Such are the compromises that are sometimes required to write portable software.

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

I have used the subsequent read() with a returned value of zero, to indicate that the client disconnected (as a work around). However, on recent versions of Linux, this is not reliable and the read() may wait forever instead of immediately returning.

If you want nonblocking behavior, you should set the socket to nonblocking. This is a bit strange though, unless the data was stolen by another thread or something. Are you sure you've seen this?

Quoted 5 lines

I don't use threads. The hang under the specified conditions was first observed on 2.6.16.4 (that I'm running on this system). The hack, previously used, i.e., the read of zero was used since 2.4.x with success except it's a hack and shouldn't be required. It was not ever required on SunOs from which the software was ported.

This may be a bug somewhere.. however, once again if you don't want read to block under any circumstances, set your sockets to non-blocking!

Signature · 3 lines
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from [email protected]
Home Page: http://www.roberthancock.com/
#4 lore raw
Quoted 27 lines · Robert Hancock
Quoted 5 lines · linux-os (Dick Johnson)
Quoted 4 lines

POLLHUP means "The device has been disconnected." This would obviously be appropriate for a device such as a serial line or TTY, etc. but for a socket it is less obvious that this return value is appropriate.

Quoted 8 lines

Hardly "less obvious". SunOs has returned POLLHUP as has other Unixes like Interactive, from which the software was ported. It went from Interactive, to SunOs, to Linux. Linux was the first OS that required the hack. This was reported several years ago and I was simply excoriated for having the audacity to report such a thing. So, I just implemented a hack. Now the hack is biting me. It's about time for poll() to return the correct stuff.

The standard doesn't require that a close on a socket should report POLLHUP. Thus this behavior may differ between UNIX implementations. If your software is requiring a POLLHUP to indicate the socket is closed I think it is being unnecessarily picky since read returning 0 universally indicates that the connection has been closed. Such are the compromises that are sometimes required to write portable software.

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

I have used the subsequent read() with a returned value of zero, to indicate that the client disconnected (as a work around). However, on recent versions of Linux, this is not reliable and the read() may wait forever instead of immediately returning.

If you want nonblocking behavior, you should set the socket to nonblocking. This is a bit strange though, unless the data was stolen by another thread or something. Are you sure you've seen this?

Quoted 8 lines
Quoted 5 lines

I don't use threads. The hang under the specified conditions was first observed on 2.6.16.4 (that I'm running on this system). The hack, previously used, i.e., the read of zero was used since 2.4.x with success except it's a hack and shouldn't be required. It was not ever required on SunOs from which the software was ported.

This may be a bug somewhere.. however, once again if you don't want read to block under any circumstances, set your sockets to non-blocking!

But that's another hack. AFAICS why ppl (mostly) use select/poll wud be to know if their send/recv/read/write would go thru rather than getting blocked!

-jb

Signature · 1 line
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. - Albert Einstein
#5 lore raw
Quoted 16 lines · jimmy
Quoted 15 lines · Robert Hancock
Quoted 13 lines · linux-os (Dick Johnson)
Quoted 4 lines

POLLHUP means "The device has been disconnected." This would obviously be appropriate for a device such as a serial line or TTY, etc. but for a socket it is less obvious that this return value is appropriate.

Hardly "less obvious". SunOs has returned POLLHUP as has other Unixes like Interactive, from which the software was ported. It went from Interactive, to SunOs, to Linux. Linux was the first OS that required the hack. This was reported several years ago and I was simply excoriated for having the audacity to report such a thing. So, I just implemented a hack. Now the hack is biting me. It's about time for poll() to return the correct stuff.

Quoted 6 lines
Quoted 6 lines

The standard doesn't require that a close on a socket should report POLLHUP. Thus this behavior may differ between UNIX implementations. If your software is requiring a POLLHUP to indicate the socket is closed I think it is being unnecessarily picky since read returning 0 universally indicates that the connection has been closed. Such are the compromises that are sometimes required to write portable software.

This is from the Linux man-page shipped with recent distributions

SOCKET(7) Linux ProgrammerâEUR(tm)s Manual SOCKET(7)

        +--------------------------------------------------------------------+
        |                            I/O events                              |
        +-----------+-----------+--------------------------------------------+
        |Event      | Poll flag | Occurrence                                 |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLIN    | New data arrived.                          |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLIN    | A connection setup has been completed (for |
        |           |           | connection-oriented sockets)               |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLHUP   | A disconnection request has been initiated |
        |           |           | by the other end.                          |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLHUP   | A connection is broken (only  for  connec- |
        |           |           | tion-oriented protocols).  When the socket |
        |           |           | is written SIGPIPE is also sent.           |
        +-----------+-----------+--------------------------------------------+
        |Write      | POLLOUT   | Socket has enough send  buffer  space  for |
        |           |           | writing new data.                          |
        +-----------+-----------+--------------------------------------------+
        |Read/Write | POLLIN|   | An outgoing connect(2) finished.           |
        |           | POLLOUT   |                                            |
        +-----------+-----------+--------------------------------------------+
        |Read/Write | POLLERR   | An asynchronous error occurred.            |
        +-----------+-----------+--------------------------------------------+
        |Read/Write | POLLHUP   | The other end has shut down one direction. |
        +-----------+-----------+--------------------------------------------+
        |Exception  | POLLPRI   | Urgent data arrived.  SIGURG is sent then. |
        +-----------+-----------+--------------------------------------------+

If linux doesn't support POLLHUP, then it shouldn't be documented. I got the same king of crap^M^M^M^Mresponse the last time I reported this __very__ __obvious__ defect! The information is available in the kernel. It should certainly report it, just like other operating systems do, including <shudder> wsock32.

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 10 lines
Quoted 7 lines

I have used the subsequent read() with a returned value of zero, to indicate that the client disconnected (as a work around). However, on recent versions of Linux, this is not reliable and the read() may wait forever instead of immediately returning.

If you want nonblocking behavior, you should set the socket to nonblocking. This is a bit strange though, unless the data was stolen by another thread or something. Are you sure you've seen this?

I don't use threads. The hang under the specified conditions was first observed on 2.6.16.4 (that I'm running on this system). The hack, previously
used, i.e., the read of zero was used since 2.4.x with success except it's
a hack and shouldn't be required. It was not ever required on SunOs from which the software was ported.

Quoted 7 lines
Quoted 3 lines

This may be a bug somewhere.. however, once again if you don't want read to block under any circumstances, set your sockets to non-blocking!

But that's another hack. AFAICS why ppl (mostly) use select/poll wud be to know if their send/recv/read/write would go thru rather than getting blocked!

Yes. You need to know if something has changed. This could mean many things such as new data available or a disconnection. This is a communications link for crysake, one needs to handle communications events.

Quoted 6 lines

-jb
--
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. - Albert Einstein

Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.4 on an i686 machine (5592.89 BogoMips). New book: http://www.lymanschool.com
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#6 lore raw
Quoted 67 lines · linux-os (Dick Johnson)
Quoted 18 lines · jimmy
Quoted 16 lines · Robert Hancock
Quoted 14 lines · linux-os (Dick Johnson)
Quoted 5 lines

POLLHUP means "The device has been disconnected." This would obviously be appropriate for a device such as a serial line or TTY, etc. but for a socket it is less obvious that this return value is appropriate.

Hardly "less obvious". SunOs has returned POLLHUP as has other Unixes like Interactive, from which the software was ported. It went from Interactive, to SunOs, to Linux. Linux was the first OS that required the hack. This was reported several years ago and I was simply excoriated for having the audacity to report such a thing. So, I just implemented a hack. Now the hack is biting me. It's about time for poll() to return the correct stuff.

Quoted 7 lines
Quoted 7 lines

The standard doesn't require that a close on a socket should report POLLHUP. Thus this behavior may differ between UNIX implementations. If your software is requiring a POLLHUP to indicate the socket is closed I think it is being unnecessarily picky since read returning 0 universally indicates that the connection has been closed. Such are the compromises that are sometimes required to write portable software.

This is from the Linux man-page shipped with recent distributions

SOCKET(7) Linux ProgrammerâEUR(tm)s Manual SOCKET(7)

        +--------------------------------------------------------------------+
        |                            I/O events                              |
        +-----------+-----------+--------------------------------------------+
        |Event      | Poll flag | Occurrence                                 |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLIN    | New data arrived.                          |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLIN    | A connection setup has been completed (for |
        |           |           | connection-oriented sockets)               |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLHUP   | A disconnection request has been initiated |
        |           |           | by the other end.                          |
        +-----------+-----------+--------------------------------------------+
        |Read       | POLLHUP   | A connection is broken (only  for  connec- |
        |           |           | tion-oriented protocols).  When the socket |
        |           |           | is written SIGPIPE is also sent.           |
        +-----------+-----------+--------------------------------------------+
        |Write      | POLLOUT   | Socket has enough send  buffer  space  for |
        |           |           | writing new data.                          |
        +-----------+-----------+--------------------------------------------+
        |Read/Write | POLLIN|   | An outgoing connect(2) finished.           |
        |           | POLLOUT   |                                            |
        +-----------+-----------+--------------------------------------------+
        |Read/Write | POLLERR   | An asynchronous error occurred.            |
        +-----------+-----------+--------------------------------------------+
        |Read/Write | POLLHUP   | The other end has shut down one direction. |
        +-----------+-----------+--------------------------------------------+
        |Exception  | POLLPRI   | Urgent data arrived.  SIGURG is sent then. |
        +-----------+-----------+--------------------------------------------+
Quoted 6 lines

If linux doesn't support POLLHUP, then it shouldn't be documented. I got the same king of crap^M^M^M^Mresponse the last time I reported this __very__ __obvious__ defect! The information is available in the kernel. It should certainly report it, just like other operating systems do, including <shudder> wsock32.

Hi Dick

On socket disconnection, POLLIN set in poll()->revents and recv() returning 0 is the only portable and reliable method.

This is well explained in Stevens book (The absolute reference imho). It was writen well before Linus wrote a single line of C code.

If you dont have this book (that would be a shame !!! )

Please refer to http://www.opengroup.org/onlinepubs/000095399/functions/poll.html

POLLHUP

The device has been disconnected. This event and POLLOUT are mutually-exclusive; a stream can never be writable if a hangup has occurred. However, this event and POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI are not mutually-exclusive. This flag is only valid in the /revents/ bitmask; it shall be ignored in the /events/ member.

So you should not set POLLHUP in the mem->pfd.events member, since POLLHUP is non maskable.

Also you might find this interesting : http://www.greenend.org.uk/rjk/2001/06/poll.html

More over this comment found in the linux kernel (file net/ipv4/tcp.c) is quite good :

/*
* POLLHUP is certainly not done right. But poll() doesn't
* have a notion of HUP in just one direction, and for a
* socket the read side is more interesting.
*
* Some poll() documentation says that POLLHUP is incompatible
* with the POLLOUT/POLLWR flags, so somebody should check this
* all. But careful, it tends to be safer to return too many
* bits than too few, and you can easily break real applications
* if you don't tell them that something has hung up! *
* Check-me.
*
* Check number 1. POLLHUP is _UNMASKABLE_ event (see UNIX98 and
* our fs/select.c). It means that after we received EOF,
* poll always returns immediately, making impossible poll() on
write()
* in state CLOSE_WAIT. One solution is evident --- to set POLLHUP
* if and only if shutdown has been made in both directions.
* Actually, it is interesting to look how Solaris and DUX
* solve this dilemma. I would prefer, if PULLHUP were maskable,
* then we could set it on SND_SHUTDOWN. BTW examples given
* in Stevens' books assume exactly this behaviour, it explains
* why PULLHUP is incompatible with POLLOUT. --ANK
*
* NOTE. Check for TCP_CLOSE is added. The goal is to prevent
* blocking on fresh not-connected or disconnected socket. --ANK */
if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == TCP_CLOSE)
mask |= POLLHUP;

So basically a POLLHUP could be stick in revent if POLLOUT was not given in event, but it would be of litle interest...

Eric

#7 lore raw
In reply to linux-os (Dick Johnson)
Quoted 5 lines · linux-os (Dick Johnson)

If linux doesn't support POLLHUP, then it shouldn't be documented. I got the same king of crap^M^M^M^Mresponse the last time I reported this __very__ __obvious__ defect! The information is available in the kernel. It should certainly report it, just like other operating systems do, including <shudder> wsock32.

Try to search the list (and the source) for POLLRDHUP ...

- Davide

#8 lore raw
In reply to jimmy
Quoted 8 lines
Quoted 4 lines

This may be a bug somewhere.. however, once again if you don't want read
to block under any circumstances, set your sockets to non-blocking!

But that's another hack. AFAICS why ppl (mostly) use select/poll wud be to know if their send/recv/read/write would go thru rather than getting blocked!

It's not another hack. If you don't want to block, you must tell the kernel
that. As for select/poll telling you that a write won't block, it has never done that. The select/poll functions, just like almost every other system call, do *not* provide future guarantees.

DS