Systems and protocols · Intermediate

epoll: waiting for readiness, not completed work

Distinguish readiness from completion and explain why edge-triggered loops use nonblocking I/O and drain until EAGAIN.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Readiness is not a result payload
  2. Trace unread data
  3. Recheck the operation outcome
  4. Manage descriptor and handler lifetime
  5. Ask Logeia about this articleCheck your understanding

Readiness is not a result payload

epoll monitors I/O readiness for registered file descriptors on Linux. Distinguish the interest list from the ready list returned through waiting. An event does not contain a completed application message; it indicates a state in which an operation may make progress or expose an end/error condition. The application must still perform the I/O and interpret its result. This separates readiness notification from completion-based interfaces. It can also avoid repeatedly attempting reads on every inactive connection, but event handling still needs correct ownership, parsing and resource limits.

Trace unread data

Suppose a socket has one hundred bytes available and the handler reads only forty. With level-triggered monitoring, readiness can be reported again while the readable condition persists. With edge-triggered monitoring, waiting for a new state transition while sixty bytes remain can leave data unprocessed indefinitely. A typical design uses nonblocking operations and drains until a result such as EAGAIN indicates that progress cannot continue now. Draining strategy and fairness are separate concerns: an event loop may also need application-managed work scheduling so one busy connection does not monopolize execution.

Recheck the operation outcome

State can change between notification and the actual operation, for example when another consumer handles the data first. A nonblocking read returning EAGAIN must therefore be handled as a possible outcome rather than an impossible contradiction. Distinguish positive byte counts, EOF and errors. Assemble application messages using their own framing rules; epoll does not add boundaries to TCP’s stream. Treating one event, one read and one request as identical units breaks when messages are fragmented across reads or several messages arrive in one batch of available data.

Manage descriptor and handler lifetime

Descriptor numbers can be reused after close, so a stored integer is not an eternal connection identity. Design application identifiers, registration changes and object lifetime so that stale work cannot be applied to a different connection. Options such as one-shot notification also introduce rearming responsibilities that must be coordinated with handlers. Performance depends on the number of ready connections and the amount of work per event, not just the number registered. Compare workloads with equivalent parsing, buffering and scheduling before attributing a CPU result to the notification interface alone.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 14

Choose a learning path