EAGAIN
Linux / POSIXWARNCommonI/OHIGH confidence
Resource Temporarily Unavailable
What this means
A non-blocking I/O operation could not be completed immediately because no data is available or the resource is temporarily busy. This is the expected return code for non-blocking sockets and pipes — it is not a fatal error.
Why it happens
- 1Reading from a non-blocking socket when no data has arrived yet.
- 2Writing to a non-blocking socket when the send buffer is full.
- 3Calling accept() on a non-blocking listening socket with no pending connections.
- 4A resource limit (e.g. open file limit) was temporarily exhausted.
How to reproduce
Reading from a non-blocking socket when no data is available.
trigger — this will error
trigger — this will error
int flags = fcntl(sock, F_GETFL, 0); fcntl(sock, F_SETFL, flags | O_NONBLOCK); char buf[1024]; ssize_t n = read(sock, buf, sizeof(buf)); // n == -1, errno == EAGAIN means: try again later
expected output
read() returned -1, errno = EAGAIN (Resource temporarily unavailable)
Fix
Use epoll/poll/select to wait for readiness
WHEN For high-performance non-blocking I/O event loops
Use epoll/poll/select to wait for readiness
// Use epoll to wait until socket is readable
int epfd = epoll_create1(0);
struct epoll_event ev = { .events = EPOLLIN, .data.fd = sock };
epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ev);
struct epoll_event events[1];
epoll_wait(epfd, events, 1, -1); // blocks until readable
read(sock, buf, sizeof(buf)); // now has dataWhy this works
epoll_wait suspends the process until the file descriptor is ready, eliminating busy-wait loops.
What not to do
✕ Spin in a tight loop retrying on EAGAIN
A busy-wait loop wastes CPU. Use epoll/poll to wait efficiently.
Sources
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev