Skip to content
systemcoding.dev
Networking

Level-Triggered vs Edge-Triggered epoll

Compare epoll level-triggered and edge-triggered modes with EPOLLET, see exactly when each reports readiness, and pick the right one for your server.

6 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the C, Linux and GNU logos.

Every epoll tutorial reaches the same fork: leave the flags alone and get level-triggered behavior, or add EPOLLET and get edge-triggered behavior. The two modes use the same API and the same data structures, and they differ in a single question, "when does epoll_wait tell me about a descriptor?" Pick wrong and you either burn CPU on repeated wakeups or lose data and hang a connection with no error. This post compares the two with a program whose output you can predict, on Linux 6.12 with glibc 2.40, and ends with a decision table. It builds on the step-by-step epoll loop.

The definitions

Level-triggered (the default, same as poll): epoll_wait reports a descriptor for as long as the condition holds. If a socket has unread bytes, every call to epoll_wait lists it as EPOLLIN, until you read the bytes.

Edge-triggered (EPOLLET in events): epoll_wait reports a descriptor only when its state changes, for example when new data arrives. The epoll(7) page describes it as a notification on the transition, and states that you should use non-blocking descriptors and read until EAGAIN, because a notification will not repeat for data you leave unread.

The vocabulary comes from electronics: a level-sensitive input fires while the signal is high, and an edge-sensitive input fires on the change from low to high.

A program that shows the difference

The program uses a socket pair so it needs no network. It writes 10 bytes to one end, then calls epoll_wait on the other end three times, reading only 4 bytes after the first wakeup. Run it once in each mode and watch how many times the descriptor is reported.

lt_vs_et.cc
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>

static void run(const char *label, uint32_t mode)
{
    int sv[2];
    socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sv);

    int ep = epoll_create1(0);
    struct epoll_event ev = { .events = EPOLLIN | mode, .data.fd = sv[0] };
    epoll_ctl(ep, EPOLL_CTL_ADD, sv[0], &ev);

    write(sv[1], "0123456789", 10);

    printf("%s\n", label);
    for (int round = 1; round <= 3; round++) {
        struct epoll_event out;
        int n = epoll_wait(ep, &out, 1, 100);   /* 100 ms timeout */
        if (n == 0) {
            printf("  round %d: no event\n", round);
            continue;
        }
        char buf[4];
        ssize_t got = read(sv[0], buf, sizeof buf);   /* read only 4 bytes */
        printf("  round %d: ready, read %zd bytes\n", round, got);
    }
    close(sv[0]); close(sv[1]); close(ep);
}

int main(void)
{
    run("level-triggered", 0);
    run("edge-triggered", EPOLLET);
    return 0;
}

Walk through the level-triggered case. Ten bytes are queued. Round 1 reports readable, and the program reads 4, leaving 6. Round 2 still sees unread data, so it reports again, and the program reads 4 more, leaving 2. Round 3 reports again and reads the last 2. In the edge-triggered case the single write produced a single transition. Round 1 reports it and reads 4 bytes. Nothing new arrives, so rounds 2 and 3 time out even though 6 bytes sit unread.

Text
level-triggered
  round 1: ready, read 4 bytes
  round 2: ready, read 4 bytes
  round 3: ready, read 2 bytes
edge-triggered
  round 1: ready, read 4 bytes
  round 2: no event
  round 3: no event

That last block is the entire hazard of edge-triggered mode. Six bytes are stuck in the socket buffer, epoll_wait says nothing, and the connection looks idle. The data is not lost, but nothing will ever tell you to go and get it unless the peer sends more.

What edge-triggered requires from you

With EPOLLET you take on a contract: after every notification, drain the descriptor completely before waiting again. For reads that means looping until read returns EAGAIN, which is only possible on a non-blocking descriptor:

et_drain.cc
static void drain(int fd)
{
    char buf[4096];
    for (;;) {
        ssize_t n = read(fd, buf, sizeof buf);
        if (n > 0) {
            /* consume buf[0..n) */
            continue;
        }
        if (n == 0) {
            close(fd);              /* peer closed */
            return;
        }
        if (errno == EAGAIN || errno == EWOULDBLOCK)
            return;                 /* fully drained */
        if (errno == EINTR)
            continue;
        close(fd);                  /* real error */
        return;
    }
}

The same applies to accept on a listener registered edge-triggered: accept in a loop until EAGAIN, or connections queued behind the first stay in the backlog unannounced. The mistakes that follow from partial draining are detailed in handling partial reads with edge-triggered epoll.

If a descriptor is used blocking, edge-triggered mode is dangerous: the drain loop's final read would block instead of returning EAGAIN, stalling the whole loop.

What level-triggered requires from you

Level mode asks for nothing special. Read what you want, and anything left is reported again. The cost is that a descriptor you do not want to read right now, for example because the application is applying back pressure, is reported on every wait and wakes the loop uselessly. The standard remedy is to remove the descriptor from the interest set with EPOLL_CTL_MOD (dropping EPOLLIN) until you are ready, and add it back after.

The same effect shows up for writes. A socket with free buffer space is writable almost always, so registering EPOLLOUT level-triggered on every connection makes the loop spin. In level mode you add EPOLLOUT only while output is queued, as covered in registering EPOLLOUT only when output is pending. In edge mode you can register EPOLLOUT permanently, because the notification fires only when buffer space opens after being full.

Edge triggering and threads

Edge-triggered mode has a second wrinkle: if two threads wait on the same epoll instance, a single readiness edge can wake both, and they can race to read the same descriptor. The kernel offers EPOLLONESHOT for that case, which disables the descriptor after one event until you re-arm it with EPOLL_CTL_MOD; see using EPOLLONESHOT in multithreaded servers.

Which mode to choose

SituationChooseWhy
Learning, small tools, code others will maintainLevel-triggeredForgiving: leftover data is reported again
Porting a poll or select loopLevel-triggeredSame semantics as before
High connection counts, tuned hot pathEdge-triggeredFewer wakeups, fewer epoll_ctl calls
Multithreaded loop on one epoll instanceEdge-triggered with EPOLLONESHOTAvoids duplicate wakeups
Descriptors you cannot make non-blockingLevel-triggeredEdge mode needs EAGAIN
Handlers that sometimes read partiallyLevel-triggeredA partial read cannot strand data

The speed argument for edge mode is real but narrower than it sounds. It saves repeated reports of descriptors you have not finished with, and it spares you the EPOLL_CTL_MOD calls to toggle EPOLLOUT. It does not make the kernel's readiness tracking cheaper, and a well-written level-triggered loop that drains until EAGAIN makes about the same number of system calls on busy connections. Measure your own workload before deciding; no figure in this post is a benchmark.

When neither is the answer

If you find yourself fighting readiness semantics because the work per connection is heavy, the problem is usually the handler, not the trigger mode. Move the slow work to a thread pool or to an interface that completes operations instead of reporting readiness, such as io_uring.

Migrating a loop from level to edge

Switching a working level-triggered server to EPOLLET is a code change, not a flag flip. Audit every handler for an assumption that a single read or accept is enough. A handler that reads once and returns relied on the level behavior to be called again, and under edge mode it strands data. Convert each read and accept into a drain loop, confirm every descriptor in the set is non-blocking, and add a test that sends a payload larger than your read buffer, because that is the case that distinguishes a correct drain from a one-shot read. Without such a test, the bug tends to appear only in production when a client sends a large request.

Share

Keep reading

Flat illustration of a microcontroller wired to an LED and a sensor, with the Bluetooth, Nordic Semiconductor and Arm logos.
Embedded

Create a Custom BLE GATT Service on nRF52

Define a custom 128-bit GATT service on nRF52 with Zephyr: characteristic properties, read and write callbacks, notifications through the CCC and MTU limits.

9 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.