A pipe reaches EOF only when its last writer is gone
Pipe EOF is a statement about the lifetime of every write-end descriptor, not merely an empty buffer. Understanding that ownership rule explains common hangs in forked programs and pipelines.
EOF describes the writer set, not an empty buffer
A pipe is a byte stream with a read end and a write end. If the buffer is empty while some writer can still produce bytes, a blocking read waits. If every file descriptor referring to the write end has been closed, a read after the buffered bytes are consumed returns zero: the reader has reached end-of-file. An empty pipe and a finished producer are therefore different states. [1]
This distinction is easy to miss because application code often talks about “the writer” as one process. The kernel’s condition is about all references to the write end. A pipe is not a message queue with an explicit finish packet; closing the final writing reference is the signal that no more bytes can arrive. The mirror condition matters too: when no read-end references remain, a write generates SIGPIPE or, if that signal is ignored, fails with EPIPE. [1]
Fork copies the references that define the protocol
After fork(), both processes have descriptors for the same pipe ends. Each process must close the endpoint it does not use. Suppose a parent feeds bytes to a child that reads until EOF. If the child forgets to close its inherited write end, then the parent can write its final byte and close its own writer, yet the child still has a live writer descriptor. The child drains the bytes and then waits forever for more: its own unused descriptor keeps the pipe’s writer set nonempty. The reverse mistake—leaving an unused read end open in the parent—can keep a writer from seeing SIGPIPE when the intended reader exits. [2]
import os
import threading
import time
r, w = os.pipe()
extra_writer = os.dup(w)
os.write(w, b"ready")
os.close(w)
def close_last_writer():
time.sleep(0.05)
os.close(extra_writer)
threading.Thread(target=close_last_writer).start()
print(os.read(r, 5)) # b'ready'
print(os.read(r, 1)) # b'' after the final writer closes
os.close(r)A duplicate makes the rule visible
This small Unix Python example duplicates a writer. The first read gets the data; the second waits until the delayed thread closes the duplicate, at which point it returns b"". It is intentionally a local demonstration rather than a multiprocessing recipe. [1]
Descriptor inheritance crosses process boundaries
A descriptor can outlive the code that created it when a child process inherits it. Close-on-exec helps at the exec() boundary, but it does not mean “never copied by fork.” On Linux, pipe2(O_CLOEXEC) creates both descriptors with close-on-exec set atomically; this avoids a separate setup interval in which another thread could start a child and inherit them. After fork(), each process still needs to close the ends it does not own. [2]
Python adds another layer of defaults. os.pipe() returns non-inheritable descriptors, meaning they are not passed through an exec by default. subprocess.Popen also defaults to close_fds=True, closing descriptors other than standard input, output and error before executing a child, unless descriptors are deliberately passed. These defaults prevent many accidental exec leaks, but they do not excuse unclear endpoint ownership in code that uses fork() directly or intentionally passes descriptors. [3]
Treat each endpoint like a resource with one owner
A reliable pipe setup can be reviewed as a small ownership table: which process reads, which process writes, and which descriptors each process must close immediately after process creation. Close unused ends before performing work that might block. In a multi-stage pipeline, every stage should close the original descriptors after wiring its standard streams; otherwise a downstream reader may never see EOF just because some unrelated stage still holds a writer open. [2]
When a reader hangs after receiving all expected bytes, inspect descriptors across the whole process tree before adding a timeout or inventing a terminator. A delayed EOF often means a writer reference escaped its intended lifetime. When the protocol genuinely needs to distinguish “temporarily idle” from “finished,” encode that distinction at the application layer or use a transport with explicit framing and shutdown semantics. Pipe EOF itself says only that no writer references remain; it does not say that the bytes formed a complete application message. [1]
The practical rule is simple: close each unused end as soon as ownership is clear, and make intentional descriptor passing explicit. That gives the kernel the information needed to deliver EOF and broken-pipe behavior at the point the program expects. It also makes hangs diagnosable as resource-lifetime bugs instead of mysterious failures in the reader loop.