github.com/alexfalkowski/go-linux/v2/epoll provides explicit Linux readiness sets for standard *os.File sources and typed facility registrations. It requires Linux 7.0+, the Go version in go.mod, and permitted epoll, eventfd, and descriptor duplication operations. Ordinary use needs no extra capabilities. Continue using normal Go I/O and its runtime poller for ordinary file and network operations.
The executable example demonstrates pipe readiness and one-shot rearming. See the verification guide for runner requirements and verified environments.
Good for explicit event loops that wait on several kernel I/O sources. Redis uses an epoll backend for readiness; ordinary Go networking can continue to use the runtime’s poller.
Create an owned poller and register an existing *os.File. This snippet belongs inside an error-returning function, imports epoll and time, and assumes reader is a live pipe reader whose peer may write:
poller, err := epoll.New()
if err != nil {
return err
}
defer poller.Close()
registration, err := poller.Add(reader, epoll.Readable|epoll.OneShot)
if err != nil {
return err
}
defer registration.Close()
events := make([]epoll.Event, 16)
n, err := poller.Wait(events, time.Second)
if err != nil {
return err
}
for _, event := range events[:n] {
if event.Registration == registration && event.Events&epoll.Readable != 0 {
// Consume input using bounded I/O, then rearm when ready to receive again.
if err := registration.Modify(epoll.Readable | epoll.OneShot); err != nil {
return err
}
}
}
Wait reports advisory readiness; other I/O can consume it first. Events do not reserve bytes or writable capacity. Bound subsequent file I/O with its own deadlines or known record lengths. The complete pipe example performs a bounded read before rearming. For EOF or source-error handling, inspect the returned Hangup and Error bits as well. Hangup can coexist with buffered readable data. Priority and ReadHangup request Linux exceptional and stream half-close notifications; their exact meaning depends on the source.
Add borrows the file and privately duplicates it with close-on-exec. Ownership is independent even though both references keep the same kernel I/O source alive:
Caller --owns--> original file -----------+
+--> same kernel I/O source
Poller --owns--> registration's duplicate +
| Successful close | Effect |
|---|---|
| Original file | Releases the caller’s reference; the registration stays alive |
| Registration | Removes the registration and closes its duplicate; the original stays open |
| Poller | Releases all remaining registrations; original files stay open |
A registration can delay EOF or peer teardown after the original file closes. Repeated Add calls produce independent registrations. Add leaves the original usable on failure and never changes source status flags or deadlines. Ordinary disk files are not epoll sources and return Linux EPERM; errors preserve errno with errors.Is and errors.As.
The default epoll.LevelTriggered reports a condition again while it remains true. epoll.EdgeTriggered reports readiness transitions; consume nonblocking input until it would block before expecting another edge. Standard Go file reads may themselves wait, so use deadlines or known lengths rather than an unbounded drain loop. This package does not change a source’s I/O mode or consume its data.
epoll.OneShot disables the registration after delivery. registration.Modify(fullMask) replaces all requested conditions and flags and rearms it, even with an unchanged mask. EdgeTriggered and OneShot can be combined. Unknown input bits match os.ErrInvalid. Zero interests request only the automatically reported Error and Hangup conditions. Those conditions appear without requesting them, and are readiness bits rather than Wait errors.
Each Event holds the exact Registration pointer returned by Add. Keep application state keyed by that pointer. Descriptor numbers are never event identities: closing a source and reusing its number cannot retarget an existing registration. Removed entries are filtered before translation, but events already translated or returned can still reference a removed registration. Such an event retains its old identity; Modify on the closed registration matches os.ErrClosed. Modification can overlap events from the prior mask. Event order and one result per I/O operation are not guaranteed.
Wait requires a nonempty buffer and changes only events[:n] on success. Zero timeout polls immediately, any negative timeout waits indefinitely, and a positive duration bounds the wait against a monotonic deadline. Timeout returns (0, nil). Durations round up to kernel milliseconds; scheduling can overrun the deadline. Long durations are split internally without truncation, and signal interruption retries preserve the original deadline. Source deadlines do not affect Wait.
Close interrupts every pending Wait with os.ErrClosed, joins outstanding waits, and releases the poller’s descriptors. An already translated result may still succeed during a concurrent close. Calls are safe concurrently, but each concurrent Wait needs its own buffer, and waiters compete for source events. No background goroutine is started; each blocked Wait can occupy an OS thread. Close and timeouts are the supported interruption mechanisms.
Zero owners match os.ErrInvalid; operations on closed owners match os.ErrClosed. Nil file, unknown-mask, and empty-buffer validation precede closed-state checks. Do not copy owners after first use or use nil receivers. On a registration deletion error, retain the registration and retry or close its poller; a close error after successful deletion leaves it closed. Do not retry Poller.Close to repeat descriptor cleanup.
For opaque eventfd counters and timerfd timers, call the source’s Register(poller, interests) method. These registrations have the same identity, trigger modes, and independent lifetime as Add results; consume readiness through the source’s typed I/O. Their executable examples demonstrate bounded reads after Wait. Closing a source leaves its registration alive; close the registration or poller as well to release the kernel resource. Neither source deadlines nor Wait timeouts control the other operation.
Pidfd owners currently have no epoll registration operation. Context cancellation, signal masks, exclusive/power-management flags, nested pollers, raw adoption, and newer wait facilities are outside this initial scope. See the package documentation, design note, and verification guide.