Hello,

I wonder if there would be interest in trying to standardise
POLLRDHUP.  It originated on Linux, and has spread also to illumos and
FreeBSD.

The use case I am aware of:  A client has sent a message (say, query),
a server (say, a database) is expending large amounts of CPU computing
a response, and in the meantime, the TCP stack has learned that the
client has gone away, gracefully or not, by shutting down, closing,
exiting, crashing, or losing power or network link, which in that last
case requires socket-level keepalive to be configured.  The server
application can't discover this until it eventually completes its work
and tries to send a response and learns that the connection is reset.
One might think that it could periodically try recv(sock, &buf, 1,
MSG_PEEK) to see if EOF is buffered already, but this approach doesn't
work if the application-level protocol allows pipelining of requests
or for some other reason there is data buffered, including a goodbye
message from the client.  Arguably, the application protocol could be
redesigned to include periodic application-level heartbeat messages
during processing, but many applications have discovered the technique
of polling for POLLRDHUP periodically, because the kernel in fact
already knows about this condition.  There is just no standardised way
to ask about it.

Examples of users:

https://github.com/Exim/exim/blob/e4e884faa7f5a04d937282113681d97a355ed2af/src/src/acl.c#L3523
https://github.com/mysql/mysql-server/blob/7ed30a748964c009d4909cb8b4b22036ebdef239/vio/viosocket.cc#L750
https://github.com/postgres/postgres/blob/c30f54ad732ca5c8762bb68bbe0f51de9137dd72/src/backend/libpq/pqcomm.c#L1936

The FreeBSD change for poll():

https://github.com/freebsd/freebsd-src/commit/3aaaa2efde896e19d229ee2cf09fe7e6ab0fbf6e

Other systems with shared history probably implement poll() similarly,
but some related OSs have re-implemented poll() internally on top of
kqueue machinery.  Kqueue/kevent() already exposes this condition as
EV_EOF, which is reported independently of whether data is buffered
and available to read.  I am not aware of any other programming
interfaces that expose the condition, despite searching.  If you know
of any, I'd love to hear about those too.

Thanks,

Thomas Munro

Reply via email to