A very specific and interesting question!
After digging into the Linux kernel source code (specifically, net/ipv4/tcp.c and fs/select.c), I found the answer.
Short answer:
The Linux kernel can return both POLLOUT and POLLHUP (but not POLLERR) to a poll() call on a TCP socket when:
- The socket is connected (i.e., not in
SYN_SENT or SYN_RECV state).
- The remote peer has closed its send direction of the connection (i.e., it has called
shutdown(SHUT_WR) or closed the socket with close()), which sends a FIN packet to the local socket.
- The local socket's send buffer is empty (i.e., all data has been sent or discarded due to a previous
shutdown(SHUT_WR) or close() on the local side).
- No error has occurred on the socket (e.g., no RST packet received, no timeout, no network error).
Long answer:
Here's what happens under the hood:
- When the remote peer closes its send direction, it sends a FIN packet, which is acknowledged by the local kernel (TCP state moves from
ESTABLISHED to CLOSE_WAIT).
- Upon receiving the FIN, the local kernel sets the
TCP_CLOSE_WAIT flag and wakes up any waiting processes with POLLIN (data available for reading: the FIN packet).
- When your process calls
poll() with POLLOUT interest (check if we can write), the kernel checks the socket's send buffer:
- If there's still data in the send buffer,
poll() returns POLLOUT (you can still write, but be aware that the remote won't read it).
- If the send buffer is empty, the kernel checks the socket's TCP state:
- If
TCP_CLOSE_WAIT is set (FIN received, but not yet acknowledged by our close()), both POLLOUT and POLLHUP are set.
POLLOUT means: "you can still write, but it won't be sent" (the connection is half-closed, and the kernel won't send any more data).
POLLHUP (Hang UP) means: "the remote peer stopped listening" ( FIN was received, indicating the remote won't read any more data).
POLLERR is not set because there's no error condition on the socket; the FIN packet is a normal, expected event in the TCP state machine.
The relevant kernel code snippets:
In tcp_poll() (net/ipv4/tcp.c):
if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
mask |= POLLHUP; /* FIN received, or double close() */
...
if (tcp_send_head(sk) == NULL && /* send buffer empty */
(sk->sk_shutdown & SEND_SHUTDOWN || state == TCP_CLOSE_WAIT))
mask |= POLLOUT; /* writeable, but won't send */
And in sock_poll() (fs/select.c):
if (sk->sk_err || (sk->sk_shutdown & SHUTDOWN_MASK))
mask |= POLLERR; /* error or double close() -> POLLERR */
/* ... */
return mask;
To summarize:
POLLHUP indicates the remote peer stopped sending data (FIN received).
POLLOUT indicates the local send buffer is empty, and you can still write() (although the data won't be sent).
- No
POLLERR because the socket is in a valid, half-closed state.
Here's a simple C program to reproduce this scenario:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#define PORT 6666
int main() {
int fd, cfd;
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
char buf[1];
// Create server socket
fd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
bind(fd, (struct sockaddr*)&addr, sizeof(addr));
listen(fd, 1);
// Accept client connection
cfd = accept(fd, (struct sockaddr*)&addr, &len);
// Client closes its send direction
shutdown(cfd, SHUT_WR);
// Wait for FIN to be processed
sleep(1);
struct pollfd pfd = { .fd = cfd, .events = POLLOUT };
poll(&pfd, 1, -1);
if (pfd.revents & POLLOUT) printf("POLLOUT set\n");
if (pfd.revents & POLLHUP) printf("POLLHUP set\n");
if (pfd.revents & POLLERR) printf("POLLERR set\n"); // won't happen
return 0;
}
Run this server, then connect to it with a client (e.g., telnet localhost 6666), and close the telnet connection (which sends a FIN packet). The server will print:
POLLOUT set
POLLHUP set
Voilà!
Now you know the dark art of TCP socket polling in Linux. May this knowledge serve you well in the future!