Blog · Copenhagen · October 2026
The missing reader: pipes, channels and mailboxes
The program yes prints the letter y, one per line, forever. The program head -3 prints the
first three lines of its input and exits. Join them with a pipe, yes | head -3, and three y's
appear and the whole thing stops, at once. Asked through its variable PIPESTATUS, Bash reports
that yes ended with status 141. Nothing in yes ends it: no line of its code says when to stop.
The kernel ended it, with a signal created for exactly this case, a write to a pipe whose reader
has gone.
About fifteen minutes of reading, with one Go program in four versions and one Erlang program; the code and the sources follow the story.
That case has two counterparts inside today's programs: a Go sender that waits forever, and an Erlang mailbox that fills while its sender keeps going. This page follows the case from a memo at Bell Labs in 1964 to those two counterparts, and ends with the two models of communication that session 8 of my Programming Paradigms course teaches, channels and mailboxes, and what each does when the reader is missing.
Pipes at Bell Labs, 1964 to 1975
On 11 October 1964 Doug McIlroy, at Bell Labs, ended a memo with a page headed “Summary—what's most important”. Its first point reads: “We should have some ways of connecting programs like garden hose”. One program's output would flow into another's input, as water flows from one length of hose into the next, and programs would be written small, to be screwed together.
It took eight years. Unix existed by then, written by Ken Thompson and Dennis Ritchie, and its
programs already read a standard input and wrote a standard output that the shell could redirect to
a file. Ritchie's history of Unix dates pipes to 1972 and says they came at McIlroy's suggestion, or
perhaps insistence. McIlroy's own account is the better story. He “finally persuaded” Thompson, and
in “one feverish night” Thompson wrote the pipe system call, added pipes to the shell, and changed a
few utilities to work as filters. The next day, McIlroy writes, saw “an unforgettable orgy of
one-liners” as everybody at the lab joined in. The first notation reused the redirection characters,
sort input >pr>opr>, and lasted only a couple of months; the vertical bar came with the fourth
edition and is still there.
A pipe is a small buffer in the kernel with a writer at one end and a reader at the other. By default two rules make it work. A reader that finds the buffer empty waits until something is written; a writer that finds it full waits until something is read. The second rule matters more than it looks. The buffer is bounded, of a fixed size, so a fast writer cannot run away from a slow reader: it is made to wait. On Linux a pipe holds sixteen pages by default, 65,536 bytes with the usual page of 4,096 bytes, before the writer waits.
The two rules leave one case open: the reader is not slow but gone. head has printed its three
lines and exited, and yes writes another y. In the fifth edition of Unix the write simply returned
a short count, and a program that did not check it went on writing into nothing. The sixth edition,
of 1975, added signal 13 to the system. Its manual page gives the reason in one line: “write on a
pipe with no one to read it”. The signal is called SIGPIPE, and its default action is to end the
process. That is the 141 of the first paragraph: Bash reports a process ended by signal n
as 128 + n, and 128 + 13 is 141. POSIX keeps the rule today: a write to a pipe that no process has open for reading
fails with the error EPIPE, and SIGPIPE is sent to the writer.
The change is a few lines of the kernel, in the function writep of the file ken/pipe.c, which
writes into a pipe. The count i_count of the pipe's inode is the number of open
ends that refer to it; below two, one side has gone. In the fifth edition the function returns, quietly, with the count of what it did not write:
if(ip->i_count<2 || c==0) {
prele(ip);
u.u_count = c;
return;
}
In the sixth edition the same test has its own branch and its own comment:
/*
* If there are not both read and
* write sides of the pipe active,
* return error and signal too.
*/
if(ip->i_count < 2) {
prele(ip);
u.u_error = EPIPE;
psignal(u.u_procp, SIGPIPE);
return;
}
The error reaches a program that catches or ignores the signal and checks what its write returned; the signal, by default, ends a program that does neither.
So by 1975 Unix had an answer for a reader that has gone, as well as for one that is slow. A busy
reader makes the writer wait a little; a slow reader lets the buffer fill, and the writer waits
longer; a reader that has exited is reported to the writer by an error and a signal, and the signal
by default ends the writer, which for yes is exactly right. One state stays without an answer: a
reader that is alive and never reads again leaves its writer waiting for good, since the kernel
cannot tell a reader that has stopped from one that is slow.
McIlroy's pipeline and Knuth's program, 1986
Eleven years later the pipe had its most famous day. In 1986 Jon Bentley, who wrote the “Programming Pearls” column of Communications of the ACM, asked Donald Knuth to demonstrate literate programming, his method of writing a program as an essay for people, on a textbook problem: given a text and a number k, print the k most common words with their counts. Knuth's answer, published in the June issue, was a program in his WEB system, careful and complete; along the way he invented a new data structure for it, the hash trie, a cross between a hash table and the tree that spells words letter by letter. Bentley then asked McIlroy to review the program.
McIlroy praised the exposition and then wrote six lines of shell:
tr -cs A-Za-z '\n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
sed ${1}q
Split the text into one word per line; fold upper case to lower; sort, so that equal words are adjacent; count each run of equal words; sort by count, largest first; print the first k lines. The pipeline joins six programs with five pipes, each program written years before by someone who had never heard of the problem. McIlroy's verdict on Knuth's program: “a sort of industrial-strength Faberge egg”, wonderfully worked and, he added, a museum piece from the moment it was made. He did not claim his pipeline was faster, and suggested how a production version would borrow Knuth's data structure. His point was composition: small programs, connected like garden hose, answer the question today.
The last stage meets the case of this page, a reader that exits before its writer has finished.
sed ${1}q quits after k lines, and sort -rn, in front of it, may still be writing the rest of
the counts. The stages before sort -rn have already finished: a sort reads all of its input before
it writes its first line. When sed quits, sort -rn has lost its reader, and its next write
brings the signal of 1975, which ends it the way it ended the writer of the first paragraph. Nobody wrote a line of code for
this; the pipe's rules take care of it.
A Go sender without a receiver
Go's designers name the ancestors of its concurrency: languages inspired by Tony Hoare's Communicating Sequential Processes of 1978, such as Newsqueak and Limbo. A Go program runs its concurrent activities as goroutines, lightweight threads that the language's runtime schedules onto the operating system's threads. Goroutines share the program's memory; Go's advice is to share it by communicating, passing values to each other over channels. A channel has a capacity, fixed when it is made. With no buffer, capacity zero, sender and receiver must meet: the specification says that communication succeeds only when a sender and a receiver are both ready, and until then whichever arrives first waits. With a positive capacity the sender waits only when the buffer is full, as on a pipe.
Unlike a pipe, a channel has no reader's end that closes when the reader leaves. A goroutine that stops receiving simply stops; the channel stays open, and nothing in the language reports that no goroutine will ever receive from it again. A send that then finds no room in the buffer and no receiver waits forever, unless some other goroutine turns up to receive, and the program goes on around it.
Five pieces of Go syntax are enough to read the code below:
make(chan int, n)makes a channel of integers with capacity n;go f()starts the function f as a new goroutine;ch <- vsends the value v on the channel ch;<-chreceives a value from the channel ch;selectwaits until one of its cases, each a send or a receive, can proceed, and runs that case.
Go programmers meet the case of this page in the following shape: a helper computes one result for a caller that waits for it at most a millisecond.
func handoff(capacity int) {
ch := make(chan int, capacity) // 0: unbuffered, a rendezvous
go func() {
time.Sleep(2 * time.Millisecond) // the work, longer than the wait
ch <- 42 // waits for a receiver or a free slot
}()
select {
case r := <-ch:
_ = r
case <-time.After(time.Millisecond):
// the caller gives up and returns; nothing tells the helper
}
}
The select waits for the result or the timeout, whichever can proceed first. The timeout normally
comes first, after one millisecond against the helper's two. If both can proceed when the select
is reached, the specification has it choose one by uniform pseudo-random selection, which is why the
program below counts instead of assuming. The caller returns, and with it goes the only receiver the
channel ever had. Two milliseconds in, the helper reaches its send and waits for a partner that will
never come. It is not an error, and nothing is printed. A goroutine is cheap, a few kilobytes of
stack to begin with, and a blocked one keeps alive everything it refers to, for as long as the
program runs.
The program story/reader/reader.go calls handoff(0) a thousand times, waits 50 milliseconds, and
counts the goroutines still alive, each of them stuck in its send. Then it makes the same thousand
calls with capacity 1. Then it runs the pipe's own shape, yes | head -3 in Go: a writer that sends
numbers forever and a reader that takes three and returns. One run on a laptop printed:
handoff, unbuffered 1000 calls, writers still blocked: 1000
handoff, buffer of 1 1000 calls, writers still blocked: 0
stream, buffer of 1 1000 calls, writers still blocked: 1000
In this run all thousand helpers were left behind. A run can show 999: when a helper's send is ready
by the time its caller's select is reached, the select may take the result, and an earlier run
on the same laptop did so once. The timing decides how often, not the program's text. The buffer of
one fixes the handoff: the helper's one send goes into the buffer and the helper finishes, whether or
not anyone ever reads it. It does not fix the stream, and no buffer would: a writer that writes
forever fills any finite buffer and then waits, forever, for a reader that has left. A capacity bounds
how far a writer can get ahead of its reader. It carries no news of the reader's departure.
Goroutine leaks at Kubernetes, 2019, and Uber, 2024
The handoff above is not invented. At ASPLOS 2019, Tengfei Tu, Xiaoyu Liu, Linhai Song and Yiying
Zhang published the first study of concurrency bugs in Go: 171 bugs, collected from six large Go
systems, Docker, Kubernetes, etcd, gRPC,
CockroachDB and BoltDB. Their Figure 1 is a function of Kubernetes, finishReq: a child goroutine
sends its result on an unbuffered channel, the parent waits in a select on that channel and a
timeout, and when the timeout wins the child blocks forever. The fix in Kubernetes was the buffer of one.
The paper's count is the surprise. Go's documentation recommends sharing memory by communicating, and the paper names the common belief that message passing is less error-prone. Among the bugs of its collection that left a program stuck, about 58% came from message passing, not from shared memory. Among the bugs that involved no waiting at all, those that let a program run on and misbehave, it was the other way round: about four in five came from shared memory that was unprotected or wrongly protected. In those six systems, the bugs of message passing mostly made a program wait, and those of shared memory mostly let it run on wrongly; the study does not say whether message passing created or prevented bugs.
Five years later the same shape turned up at Uber, measured across a whole company's code. Georgian-Vlad Saioc, Dmitriy Shirchenko and Milind Chabbi studied goroutine leaks in the company's Go code: about 75 million lines and some 2,500 microservices. Their tools found 857 leaks already in the code and prevented about 260 new ones over a year. In production, one leak had left about three million goroutines blocked across 800 instances of a single service. Fixing another cut the memory of its service by a factor of 9.2. Their catalogue of patterns reads like this page: a premature return, a timeout, a sender that sends once more than its receiver receives. In each of these, a writer waits for a reader that has gone. Other patterns of the catalogue run the other way and leave a reader waiting for a writer that will never send, such as a loop over a channel that nobody closes.
Go's goroutine leak profile, 2026
Unix answered the missing reader in the kernel in 1975. Go's answer took until 2026 and came from
the garbage collector. In a second paper, at ASPLOS 2025, Saioc, with I-Ting Angelina Lee, Anders
Møller and Milind Chabbi, turned the collector's reachability into a detector. The collector's
marking starts from the goroutines that can run; a goroutine blocked on a channel or a lock that none
of them can reach will never wake, and finding what a program can no longer reach is the collector's
job. Go 1.26, released on 10 February 2026, shipped this as an experimental profile named
goroutineleak, enabled at build time by the experiment goroutineleakprofile, with an early return
from a loop over an unbuffered channel as its example; the release notes thank Vlad Saioc of Uber.
Go 1.27 made the profile generally available. Built on reachability, it finds a large class of
leaks, not all of them: a channel still reachable from a global variable, or from a goroutine that
can run, hides the goroutines blocked on it. At the end of the run above, reader.go reads the profile:
goroutineleak profile: total 2000
That is 1000 + 1000: the helpers left behind by the unbuffered handoff and the thousand writers of
the stream, and none of the others. The profile reports such leaks; it does not stop the send, and
nothing reaches the writer. For that, Go programs make a signal of their own: a second channel, by
convention called done, that the reader closes when it leaves, and a writer that waits on its send
and on done at the same time.
func streamDone() {
ch := make(chan int)
done := make(chan struct{})
go func() {
for i := 1; ; i++ {
select {
case ch <- i:
case <-done:
return // the reader has gone: stop writing
}
}
}()
defer close(done) // runs when the reader returns
for i := 0; i < 3; i++ {
<-ch
}
}
The reader takes three numbers and returns, and defer closes done at that return. A receive from
a closed channel proceeds at once, so the writer's select takes that case and the writer returns.
The reader closes done and not ch: a send on a closed channel panics, so closing ch would crash
the writer instead of stopping it.
stream, done channel 1000 calls, writers still blocked: 0
The kernel's signal is imposed on the writer. The done channel has to be written into every
writer, by a programmer who thought of it; the leaks Uber counted are the places where nobody did.
Erlang mailboxes at Discord, 2017
Erlang, older than Go by more than two decades, made the opposite choice. Its processes have no
channels; each has a mailbox, with no fixed limit on its size. A send to a process on the same
machine puts the message into the receiver's mailbox and returns without waiting for the receiver to
take it, whatever the receiver is doing. The writer does not wait. The program
story/reader/mailbox.erl has a writer that sends lines forever and a reader that takes three and
then waits for something else:
writer(Reader, I) ->
Reader ! {line, I}, % returns at once, whatever the reader does
writer(Reader, I + 1).
reader(0) ->
receive stop -> ok end; % read three lines, then wait for something else
reader(N) ->
receive {line, _} -> reader(N - 1) end.
Four pieces of Erlang syntax are enough to read it:
- a name in capitals, such as
Reader, is a variable; Reader ! Msgsends the message Msg to the process Reader;receive Pattern -> Body endwaits for a message that matches the pattern;_in a pattern matches anything.
Each function loops by calling itself; reader has two clauses, one for the count 0 and one for any
other count. The program starts the two with spawn and measures the reader's mailbox every
100 milliseconds. One run:
after 100 ms: reader's mailbox 481568 messages, 57 MB
after 200 ms: reader's mailbox 976008 messages, 117 MB
after 300 ms: reader's mailbox 1394882 messages, 167 MB
reader dead: true; writer still sending: true
No send waits. The megabytes are the reader process's memory, as Erlang reports it, and in that run
they grow by about half a gigabyte a second. Then the program kills the reader, and the writer goes
on sending, untroubled: Erlang's reference manual says that a message sent to a process that has
ended does not arrive, and nothing tells the sender. A writer that needs that information must request
it, by placing a monitor on the reader, which delivers a message, 'DOWN', when the reader ends. A monitor reports a
death. It does nothing about a reader that is alive and not reading.
Discord, the chat service, runs its real-time layer on Erlang's virtual machine, in Elixir. In July 2017 Stanislav Vishnevskiy described how it had grown to nearly five million users connected at once, and one of the failures on the way. Each connected user has a session process, and sessions made calls, a message and then a wait for the answer, to a set of registry processes. The failure came in four steps. The registry fell behind under load. The calls timed out, and their requests stayed in the registry's queue and were retried, so the pile only grew. The sessions, each waiting in its call, stopped reading their own mailboxes. And other services went on writing to them. The mailboxes ballooned, and in the post's words would “eventually OOM the whole Erlang VM”, run it out of memory, with outages cascading from there. Discord's fix was to stop the sessions from calling where the call was likely to fail: an atomic counter of the requests in flight, raised before each call, which lets a session give up at once when the count is already too high.
The reader in Discord's story is not gone. It is waiting for someone else, which, to its writers, looks the same. An Erlang writer does not wait for its reader, so the cost of a reader that does not read appears somewhere else, in memory, and nothing limits it until the machine does. RabbitMQ, a message broker written in Erlang, met the same thing inside itself and added a remedy in 2012, in version 2.8.0: credit-based flow control, under which one internal process may send another only as many messages as it holds credit for, and then waits for more. The settings described in 2015 give each link 200 credits to start. The credit is a finite buffer like the pipe's, rebuilt out of messages.
The answers of pipes, channels and mailboxes
Session 8 of the course is about processes that share no memory and communicate only by messages, on one machine, with delivery assumed. Go's goroutines share one memory, so in Go the discipline holds only as long as what is sent is a value and not a pointer to memory both sides still use. The session's first half is channels, Hoare's and Go's; its second is mailboxes, the actors of Hewitt and Agha, Erlang's processes. This page has been about one question that every such design must answer: what happens to a writer when its reader stops reading.
| Reader slow | Reader exited | Reader alive, not reading | |
|---|---|---|---|
| Unix pipe | the writer waits until the reader catches up | EPIPE; SIGPIPE ends the writer | the writer waits for good |
| Go channela send with no free slot waits | for a receiver or a free slot | forever, unless it also waits on a done channel the reader closes | forever; done is never closed |
| Erlang mailboxthe send returns at once | the mailbox grows, then drains | the messages are dropped; a monitor reports 'DOWN' | the mailbox grows until memory runs out |
A rendezvous and a bounded buffer turn a reader that does not read into a writer that waits: the
queue stays bounded, but the waiting writers, and all they hold, are not, and the wait may never end.
An unbounded mailbox turns it into memory: the writer goes on, and the machine pays. The remedies
cross over, each aimed at one state of the reader. A done channel lets a Go writer stop waiting for
a reader that has left; a monitor tells an Erlang writer that its reader has ended; and RabbitMQ's
credit, a bound built out of messages, makes a writer wait for a reader that is alive but slow or stalled. Unix
made the reader's departure the system's business in 1975. Go and Erlang provide the means, a done
channel, a monitor, a credit, and leave their use to each program.
Lessons of the case
- A send that finds no room and no receiver waits, and the wait never ends when no receiver will come. In Go that is not an error. A buffer cures it only for a writer that sends a bounded number of times: a capacity bounds how far a writer can get ahead of its reader and says nothing of a reader that has left. Kubernetes shipped one such send, and Uber's tools found 857 leaks.
- A writer that never waits moves the cost into memory. A local Erlang send returns without waiting, so a reader that stops reading becomes a mailbox that grows, about half a gigabyte a second in the program above and a whole virtual machine at Discord.
- What the reader's departure does must be stated in the program. The sixth edition of Unix stated
it in its kernel, and in one line of a manual. In Go it is a
donechannel or a context in every writer; in Erlang, a monitor for a reader that ends, and a limit on what may be sent before an answer comes back for a reader that is alive and slow.
Sources
- M. D. McIlroy, memo of 11 October 1964, page 10, “Summary—what's most important”, published by Dennis Ritchie as “Prophetic Petroglyphs” on his Bell Labs pages, with a scan of the typescript: the garden hose.
- Dennis M. Ritchie, “The Evolution of the Unix Time-sharing System”, 1979, section “Pipes”: the year, McIlroy's suggestion and persistence, the first notation. M. D. McIlroy, A Research UNIX Reader: Annotated Excerpts from the Programmer's Manual, 1971–1986, 1987, entry “PIPE (v3 page 43)”: the feverish night, the one-liners; and the shell's entry for the vertical bar in the fourth edition.
- The Unix Heritage Society's archive of the fifth and sixth editions: the function
writepinusr/sys/ken/pipe.cof V5, which returns with the unwritten count when the pipe has lost its reader, and the same function in V6, which sets EPIPE and sends SIGPIPE; the V6 manual page signal(2) with signal 13. POSIX, write(), section ERRORS, entry EPIPE; Linux pipe(7), sections “I/O on pipes and FIFOs” and “Pipe capacity”. The Bash Reference Manual, “Exit Status”, for 128 + n. - Jon Bentley, with Donald E. Knuth and M. D. McIlroy, “Programming Pearls: A Literate Program”, Communications of the ACM 29(6), June 1986, pp. 471–483: the problem, Knuth's program and hash tries, McIlroy's six commands (p. 480) and his review.
- The Go Programming Language Specification, sections “Channel types”, “Send statements” and “Select statements”; the Go FAQ, “What are Go's ancestors?” and “Why goroutines instead of threads?”.
- Tengfei Tu, Xiaoyu Liu, Linhai Song, Yiying Zhang, “Understanding Real-World Concurrency Bugs in Go”,
ASPLOS 2019: the 171 bugs and six systems, Figure 1 (
finishReq), the 58% of blocking bugs caused by message passing (sections 1 and 5.1), the non-blocking bugs, those that involve no blocking, and the 80% of them due to unprotected or wrongly protected shared memory (section 6). - Georgian-Vlad Saioc, Dmitriy Shirchenko, Milind Chabbi, “Unveiling and Vanquishing Goroutine Leaks in Enterprise Microservices: A Dynamic Analysis Approach”, CGO 2024: the code base, the 857 and 260 leaks, the three million goroutines on 800 instances, the factor 9.2, the patterns (sections I, VI and VII, Figure 1). Georgian-Vlad Saioc, I-Ting Angelina Lee, Anders Møller, Milind Chabbi, “Dynamic Partial Deadlock Detection and Recovery via Garbage Collection”, ASPLOS 2025: the detection of blocked goroutines by the collector's reachability.
- The Go Team, Go 1.26 Release Notes, section “Runtime”, experimental goroutine leak profile, its
GOEXPERIMENTsetting and its limits; “Go 1.26 is released”, the Go Blog, 10 February 2026; Go 1.27 Release Notes, “Goroutine leak profile”, the profile made generally available. - Stanislav Vishnevskiy, “How Discord Scaled Elixir to 5,000,000 Concurrent Users”, Discord blog, 6 July 2017, section “Limited Concurrency”: the registry, the timed-out calls, the growing queues and the out-of-memory failure, the atomic counter of outstanding requests.
- Erlang/OTP, Erlang Reference Manual, “Processes”, sections on signal delivery and monitors: the
dropped message to a process that has ended, the
'DOWN'message. Álvaro Videla, “New Credit Flow Settings on RabbitMQ 3.5.5”, RabbitMQ blog, 6 October 2015, and the announcement of RabbitMQ 2.8.0 on rabbitmq-discuss, 19 March 2012: credit-based flow control, and the 200 credits of the 2015 settings. - The programs
story/reader/reader.goandstory/reader/mailbox.erl, run with Go 1.27.1 and Erlang/OTP 29 on a laptop. The scriptstory/reader/run.shruns them and the pipeline of the first paragraph, and records the output beside them; the counts are one sample each, and an earlier run of the unbuffered handoff left 999 helpers behind instead of 1000.
Written with an AI assistant; every source was opened and every quotation checked. This post is the story page of session 8 of my course Programming Paradigms at Aalborg University Copenhagen, autumn 2026, in the first person.