Skip to content
WebsiteHosting.com

concept Linux & administration

Epoll edge- versus level-triggered readiness

A partial read can leave bytes waiting in either epoll mode. Level-triggered readiness can be reported again while those bytes remain; edge-triggered readers cannot count on a fresh edge just because they stopped early.

Latest recorded source check · P0174

What it means

Edge-triggered epoll reports a change in readiness. Level-triggered epoll can keep reporting a descriptor as readable while data remains. If you read only part of the available data, the rest is still there in either mode. The difference is whether you can rely on that continuing readiness as a reason for another notification.

Try it

Simulation: Start with one nonblocking file descriptor in edge mode, 8 bytes initially ready, 3 bytes consumed in the callback, and scenario evidence provided. The result is 5 unread bytes. Those bytes are available to read now, but they do not guarantee a new edge.

Epoll edge- versus level-triggered readiness

Explain missed-readiness pitfalls and nonblocking drain loops

Simulation
Modeled outcome: Remaining edge

Unread data remains; no new edge is guaranteed without a new readiness transition.

Epoll edge- versus level-triggered readiness: synthetic scenario inputs
Replaces all inputs with the documented starting example and the indicated change. Reset restores the starting example.
Readiness after partial consumption

Readiness

  1. Trigger modeedge
  2. Outcomeremaining-edge
Initially available8 bytes (fixture units)
Consumed3 bytes (fixture units)
Remaining5 bytes (fixture units)

Remaining bytes are computed; number of notifications actually delivered is not modeled. Edge-triggered readiness is not a per-byte delivery guarantee.

Evaluated inputs

Required scenario evidence
provided
Bytes consumed in callback
3 count
Bytes initially ready
8 count
Readiness mode
edge

Single read-ready nonblocking FD, no concurrent writers, one-shot or event flags; no actual socket calls.

About this model

The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.

Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.

  1. Change only Readiness mode to level. The result is still 5 unread bytes. This time the descriptor can be reported again because it remains readable; no new arrival is needed.
  2. Change Bytes consumed in callback to 8. The result is now 0 bytes left: the callback consumed all 8 bytes in the example. The mode no longer matters to that result.
  3. Set Required scenario evidence to missing. The result is unknown, not zero. The selected counts are no longer used to infer what remains. Reset restores edge, 8 bytes ready, 3 consumed, and evidence provided.

Use whole byte counts: 1–100 initially ready and 0–100 consumed. With evidence provided, consuming more than was ready is an input error, not a drained buffer.

How a reader can stall with bytes still waiting

Follow the first notification. A descriptor changes from not readable to readable when 8 bytes arrive. The event loop receives the notification and its callback reads 3 bytes. The byte count changes from 8 to 5, but the answer to “Can I read data?” stays yes. There has been no second change from not readable to readable.

If the callback returns and the loop waits for a new edge before doing any more work on that descriptor, the 5 bytes can sit unread. No event had to get lost: the mistake was treating the first successful read as evidence that the work was finished. Under level triggering, the same 5 bytes keep the readiness condition true, so a subsequent wait can report the descriptor again. Neither result predicts exactly when a callback will run.

The full-drain case makes the difference between readiness and delivered notifications easier to see. Consume all eight initially available bytes: both edge and level modes report drained. Consume only three and five remain, while the trigger mode changes the interpretation of future readiness. The model never counts notification deliveries, and a real read can encounter EOF or another error rather than EAGAIN.

In practice

For an edge-triggered reader, use nonblocking I/O and keep track of the descriptor as needing service until its current readiness is exhausted. A common drain strategy is to keep reading while making progress. When a nonblocking read returns -1 with EAGAIN (or EWOULDBLOCK on a socket), the operation would block now. The loop can wait for fresh readiness instead of blocking on that descriptor. If the handler yields earlier, it still has work to revisit; it should not depend on another edge to remind it.

Distinguish that temporary stopping point from end of stream. For a stream read requesting at least one byte, a return value of 0 can mean end of stream. That is different from both EAGAIN and the simulation's “0 bytes left,” which is a subtraction result, not a read return value. Other errors need handling according to the descriptor and operation.

The simulation covers one read-ready descriptor with no concurrent writers, one-shot behavior, or other event flags. It makes no socket calls and counts no delivered notifications. When investigating a real reader that appears stuck, compare the readiness mode and last notification with the results of the reads that followed. The useful question is not just “Did the callback run?” but “What told the loop it was time to wait again?” Readiness indicates that an operation may make progress; it is not completion.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Does edge-triggered mean faster universally?

No universal speed advantage follows. In the single-descriptor example, edge-triggered readiness changes when another notification is available; nonblocking I/O must continue until the relevant readiness is exhausted. Actual performance requires measurement under the intended workload.

Why nonblocking reads?

To drain without blocking the event loop.

What ends a drain loop?

With nonblocking stream I/O, EAGAIN or EWOULDBLOCK indicates that further reading would block. A zero result from a nonempty read may indicate EOF; handle errors and EOF separately, rather than treating every end of a read as readiness exhaustion.

Does zero bytes always mean EAGAIN?

No. For a nonempty read request on a stream, zero bytes can indicate end of stream. EAGAIN or EWOULDBLOCK indicates that an appropriate nonblocking operation would block. A zero-length request is a separate case. The modeled remaining count is not a read result or notification count.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. epoll(7)

    Level-triggered and edge-triggered. With edge-triggered readiness use nonblocking I/O and preserve readiness until space is exhausted (e.g. EAGAIN).

    Checked .

  2. read(2)

    DESCRIPTION; ERRORS. For a nonempty stream read, a zero result can denote EOF. EAGAIN or EWOULDBLOCK can mean the nonblocking operation would block; zero-length requests are separate.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.