Sockets and readiness
Quick answer
Readiness APIs notify a program that a descriptor may support an operation without blocking. An event loop waits for readable or writable descriptors, performs bounded work and repeats. Linux provides poll and the Linux-specific epoll interface.
Readiness is a prompt to attempt I/O, not proof that a complete application message is available. With edge-triggered epoll and nonblocking descriptors, the event loop normally drains available data until the operation would block. Handle short reads, EOF and errors distinctly.
Prepare the practice environment
Run python3 lab-fixtures/syscalls/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Examples use Python 3 standard-library wrappers on Linux, private files, bounded child processes and local socketpairs. No root or external network is needed. Optional strace inspection requires an authorized environment. Review the lab guide.
Prerequisites
Review: Pipes, signals and IPC.
How it works
For stream sockets, send and recv operate on bytes and may transfer less than requested; recv with positive count returning empty bytes indicates orderly peer shutdown. Readiness is not completion: another consumer or changed state can invalidate it, and EOF or an error can be reported as readable. Use nonblocking descriptors and handle EAGAIN; watch errors and hangups. poll scans supplied descriptors; epoll maintains a registered interest set and a ready list. Default level-triggered epoll continues reporting a ready condition; edge-triggered EPOLLET signals changes and requires draining until EAGAIN with careful scheduling. Avoid assuming epoll is always faster; measure your workload. Examples use local socketpairs, finite timeouts and no external network. See socket(7), poll(2), epoll(7) and recv(2).
Try the examples
python3 - <<'PYCODE'import socket, selecta, b = socket.socketpair()with a, b: b.setblocking(False) p = select.poll(); p.register(b, select.POLLIN) assert p.poll(0) == [] a.sendall(b'x') events = dict(p.poll(1000)) assert events[b.fileno()] & select.POLLIN assert b.recv(8) == b'x' print('poll: readable after send')PYCODEAn empty zero-timeout poll becomes readable after one byte is sent.
Expected stdout:
poll: readable after sendpython3 - <<'PYCODE'import socket, errnoa, b = socket.socketpair()with a, b: b.setblocking(False) try: b.recv(1) except BlockingIOError as e: assert e.errno in (errno.EAGAIN, errno.EWOULDBLOCK) print('would block; try after readiness') else: raise AssertionError('expected no data')PYCODEAn empty live stream is different from EOF. The nonblocking call raises a would-block error.
Expected stdout:
would block; try after readinesspython3 - <<'PYCODE'import socket, selecta, b = socket.socketpair()with a, b, select.epoll() as ep: b.setblocking(False) ep.register(b.fileno(), select.EPOLLIN) a.sendall(b'xy') assert dict(ep.poll(1))[b.fileno()] & select.EPOLLIN assert b.recv(1) == b'x' assert dict(ep.poll(1))[b.fileno()] & select.EPOLLIN assert b.recv(1) == b'y' a.shutdown(socket.SHUT_WR) assert dict(ep.poll(1))[b.fileno()] & select.EPOLLIN assert b.recv(8) == b'' print('level readiness repeated; EOF observed')PYCODELevel-triggered epoll reports the remaining byte and later EOF. A real event loop must unregister finished descriptors.
Expected stdout:
level readiness repeated; EOF observedCommon mistakes
Never equate readable with a complete application message or guaranteed successful I/O. Avoid edge-triggered mode until you can correctly drain and handle errors.
Practice
Repeat the epoll example with three bytes and drain all of them in a loop. Describe what a real event loop must do on EOF or EAGAIN.
Related references
Study: ss. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sockets and readiness — Knowledge Check
Apply Linux systems programming interfaces to concrete local examples.
What does readiness mean?
What does nonblocking EAGAIN mean here?
What happens in level-triggered epoll while unread data remains?
What is EPOLLET?