arthrop0d

Brief articles on varied topics.

Linux epoll Interest and Ready Lists

linuxoperating systems

epoll is Linux’s interface for waiting on many file descriptors—integer handles for sockets, pipes, files, and similar kernel objects—without repeatedly scanning every descriptor to find out which ones are usable.

It tracks interest in particular descriptors and maintains a separate set of descriptors that are currently ready. An event loop waits for entries in that ready set, then performs I/O on them.

The problem epoll solves

A server might manage thousands of sockets while only a few have data available at any moment. With older interfaces such as select and traditional poll, the program supplies the whole descriptor set on each wait. The kernel checks the set and returns the descriptors that are ready. The application then repeats this process, often causing work proportional to the total number of connections rather than the number that actually have work.

epoll makes the set of watched descriptors persistent inside an epoll instance, a kernel object created with epoll_create1. The application adds, changes, or removes registrations with epoll_ctl, then asks for ready entries with epoll_wait.

This is not a queue of application messages. epoll does not know that a complete HTTP request, database result, or protocol frame is waiting. It tracks whether an operation on a descriptor is likely to proceed without blocking—for example, whether a socket has bytes to read or has room for more output.

Two lists, two different jobs

An epoll instance has a useful conceptual model:

When you call epoll_ctl(…, EPOLL_CTL_ADD, …), you put a descriptor on the interest list. If it is already readable, epoll can put it on the ready list immediately. Later, when data arrives or buffer space becomes available, the kernel’s notification path marks the registration ready.

A call to epoll_wait consumes ready entries and copies events into an array supplied by the application. The event can include application data stored in event.data, commonly the file descriptor or a pointer to connection state.

The same descriptor can therefore be present in both conceptual lists: it remains registered in the interest list while its current readiness is reported through the ready list.

Readiness means “an operation should not block now,” not “the operation will finish” and not “all the work is done.”

Errors and hangups are also reported as events. An event loop must check them even if it primarily registered interest in input or output.

Level-triggered readiness

Level-triggered behavior is the default. In this mode, epoll reports a descriptor whenever the watched condition remains true.

Suppose a socket has 100 bytes available and the handler reads only 40. The socket is still readable, so a later epoll_wait can report it again. If the handler does nothing, it will continue to be reported rather than silently disappearing from consideration.

This makes level-triggered loops forgiving and straightforward:

  1. Wait for events.
  2. Handle each returned descriptor.
  3. Wait again.

Internally, if a descriptor is still ready when epoll processes the event, it can remain or be placed back on the ready list. The important behavior is that readiness is persistent: leaving work in the kernel-visible condition does not lose the notification.

Level triggering does not remove the need for nonblocking descriptors. A handler that performs a blocking read after epoll says “readable” can still stall—for example, because another thread consumed the data first or because the descriptor’s state changed between waiting and reading.

Edge-triggered readiness

Passing EPOLLET requests edge-triggered behavior. Here, epoll reports a change into a ready state rather than repeatedly reporting the state itself.

If data arrives on an empty socket, epoll reports an edge. If the handler reads only part of that data and returns to epoll_wait, the socket may still be readable but no new edge has occurred. The remaining bytes can be ignored indefinitely.

Therefore, edge-triggered handlers normally use nonblocking I/O and keep operating until the call says there is currently nothing more to do:

for (;;) {
    ssize_t n = read(fd, buffer, sizeof buffer);

    if (n > 0) {
        handle_bytes(buffer, (size_t)n);
        continue;
    }

    if (n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
        break;              /* drained for now */

    if (n == 0) {
        close_connection(fd); /* peer closed */
        break;
    }

    handle_read_error(fd);
    break;
}

The same rule applies to listeners: after a readable event, repeatedly accept connections until accept or accept4 reports EAGAIN. For writable sockets, keep sending queued output until it is all sent or write reports EAGAIN.

The payoff is fewer repeated notifications and often fewer trips through the event loop. The cost is that the handler must drain the available work correctly. A slow or unfair handler can also monopolize the loop, so production loops commonly limit how much one connection can process before returning to other events.

How a loop avoids missing work

A sound event loop follows a few rules:

There is always a race between observing readiness and performing I/O. Another thread or callback may consume the data, or the peer may close the connection. That is why nonblocking operations must treat EAGAIN as a normal result, not as a fatal error.

The key distinction is simple: the interest list says what the loop wants to hear about; the ready list says what epoll currently has to report. Level triggering keeps reporting an unhandled condition. Edge triggering reports the transition once, requiring the handler to drain the condition before waiting again.

← All articles