Editing POLLHUP polling

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
'''This page is a work in progress'''
Recently I've been trying to listen on multiple sockets at once for events like reading or writing. This has worked fine, but as I've tried to listen for a socket close event I've found the documentation poor and often contradictory. This page is my attempt to figure this topic out.
Recently I've been trying to listen on multiple sockets at once for events like reading or writing. This has worked fine, but as I've tried to listen for a socket close event I've found the documentation poor and often contradictory. This page is my attempt to figure this topic out.


Line 29: Line 31:
== Existing lore ==
== Existing lore ==


Richard Kettlewell's [https://www.greenend.org.uk/rjk/tech/poll.html poll() and EOF] has a test results for the case of setting POLLIN and closing a socket with no data to read. This is the most common case of POLLHUP use I see online.  
Richard Kettlewell's [https://www.greenend.org.uk/rjk/tech/poll.html ll() and EOF] has a test results for the case of setting POLLIN and closing a socket with no data to read. This is the most common case of POLLHUP use I see online.  


The test results give us some interesting data:  
The test results give us some interesting data:  
Line 117: Line 119:


* 1997: Linux 2.1.23pre1 added the poll system call, with no AF_UNIX socket support
* 1997: Linux 2.1.23pre1 added the poll system call, with no AF_UNIX socket support
* 1998: Linux 2.1.106pre added AF_UNIX socket support. POLLHUP is returned on socket close
* 1998: Linux 2.1.106pre added AF_UNIX socket support
* 2000: Linux 2.3.41pre2 returns POLLIN on empty socket close
* 2000: Linux 2.3.41pre2 returns POLLIN on empty socket close


Line 124: Line 126:


* POLLIN if checking
* POLLIN if checking
* POLLHUP always, even when checking for no events
* POLLHUP always


This is regardless of whether there is data in the socket to read.
This is regardless of whether there is data in the socket to read.
Line 132: Line 134:
It's not documented but the events bitmask can be empty.
It's not documented but the events bitmask can be empty.


The history of FreeBSD's behaviour is as follows:
== NetBSD ==
On NetBSD 9.3 it seems that polling a closed socket will return:
 
* POLLIN if checking
* POLLOUT if checking
 
This is regardless of whether there is data in the socket to read.
 
The [https://man.netbsd.org/poll.2 NetBSD poll man page] specifically notes that "Sockets produce POLLIN rather than POLLHUP when the remote end is closed"


* 1997: [https://svnweb.freebsd.org/base?view=revision&revision=29351 FreeBSD r29351 implements poll] based on NetBSD's code. POLLHUP is not returned by sockets
Leaving the events bitmask empty causes poll to timeout.
* 2009: [https://svnweb.freebsd.org/base?view=revision&revision=195423 FreeBSD r195423 adds POLLHUP for sockets]
* 2009: [https://svnweb.freebsd.org/base?view=revision&revision=196460 FreeBSD r196460 improves poll conformance for sockets] by returning POLLHUP more often instead of POLLIN
* 2009: [https://svnweb.freebsd.org/base?view=revision&revision=196556 FreeBSD r196556 fixes POLLHUP on half-closed sockets] by only returning POLLHUP on fully closed sockets and returning POLLIN on empty buffers


== OpenBSD ==
== OpenBSD ==
Line 143: Line 150:


* POLLIN if checking
* POLLIN if checking
* POLLHUP always, even when checking for no events
* POLLHUP always


This is regardless of whether there is data in the socket to read.
This is regardless of whether there is data in the socket to read.
Line 152: Line 159:


It's not documented but the events bitmask can be empty.
It's not documented but the events bitmask can be empty.
The history of OpenBSD's behaviour is as follows:
* 1996: [https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/kern/sys_generic.c.diff?r1=1.4&r2=1.5 OpenBSD implements poll]. POLLHUP is not returned by sockets
* 2013: [https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/kern/sys_socket.c.diff?r1=1.16&r2=1.17 OpenBSD implements POLLHUP on sockets]
== NetBSD ==
On NetBSD 9.3 it seems that polling a closed socket will return:
* POLLIN if checking
* POLLOUT if checking
This is regardless of whether there is data in the socket to read.
The [https://man.netbsd.org/poll.2 NetBSD poll man page] specifically notes that "Sockets produce POLLIN rather than POLLHUP when the remote end is closed".
Leaving the events bitmask empty causes poll to timeout.
The history of NetBSD's behaviour is as follows:
* 1996: [http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/kern/sys_generic.c.diff?r1=1.27&r2=1.28&only_with_tag=MAIN NetBSD implements poll] and [http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/kern/sys_socket.c.diff?r1=1.14&r2=1.15&only_with_tag=MAIN NetBSD implements poll support for sockets] at the same time. No POLLHUP is implemented for sockets, existing select behaviour is kept


== MSYS2 on Windows ==
== MSYS2 on Windows ==
Line 213: Line 199:


== Conclusions ==
== Conclusions ==
Linux is the only system that documents the behaviour of polling only for POLLHUP. Next to that are FreeBSD and OpenBSD where the behaviour works and probably won't be changed as applications in the wild depend on it. Outside those systems I have no clue if this is a viable solution to this problem.
poll can be used to check for socket closure alone on some systems, but it isn't portable.
 
In general I have a sour feeling towards poll. It mixes up the task select does of waiting for system calls to not block with reporting a vague status of a file descriptors that you would learn about anyway by using system calls. POLLHUP feels like and reads like an afterthought.
 
In the future I'm definitely going to be using epoll or kqueue for my work. These tools have proper documentation and give concrete guarantees about how my program is going to work.
[[Category:Research]]
Please note that all contributions to JookWiki are considered to be released under the Creative Commons Zero (Public Domain) (see JookWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)