Go traps · Chapter 15 of 16
Closing a channel twice
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Defer
- 3 tasks
- Go
- In the all labs plan
The first 2 chapters of Go traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.go: stop a feed twice, on purpose
Write reproduce() in repro.go, which returns a string. Make a feed with NewFeed(charger) from feed.go (any charger), call Stop on it twice, the way the disconnect path and shutdown both do, and return what happened: the panic's message if Stop panicked (recover it in a deferred function and turn it into text with fmt.Sprint), or "stopped" if both calls returned.
The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original feed.go it must return "close of closed channel", and beside a fixed feed.go it must return "stopped". So it must really call Stop twice and catch the panic itself. No real waiting and no input: reproduce() must finish within 3 seconds.
- Task 2
feed.go: a feed stops once, however often it is told to
Fix Stop in feed.go so any number of calls, one after another or at the same moment, are safe. The first Stop prints feed <charger>: stopped and closes Done; every later Stop does nothing (no panic, no second line); before any Stop, Done is open. Keep NewFeed(charger string) *Feed, Done() <-chan struct{}, the line Stop prints, and the names and signatures.
The check runs under the race detector: it calls Stop twice in a row, then from 20 goroutines at once on fresh feeds, recovers any panic, and counts the stopped lines. A flag checked before the close with no lock passes one Stop after another and fails there; recovering from the panic hides it but still prints the line twice.
- Task 3
feed_test.go: a test that would have caught it
Write a Go test file, feed_test.go, that would have caught this bug before the release. The check runs your file three times, each in a fresh build with the race detector on: beside the original feed.go, where at least one test must fail; beside the fixed feed.go, where every test must pass; and beside a wrong fix with a closed flag and no lock, where at least one test must fail.
So call Stop from two or more goroutines at the same time, all started before you wait for any, then check Done is closed. A Stop one after another catches the original but not the flag: only goroutines at once let the race detector see it. A panic in any goroutine fails the test. Each test must finish within a second or two (the whole file is stopped after 8 seconds).
What you will learn
- repro.go: stop a feed twice, on purpose
- feed.go: a feed stops once, however often it is told to
- feed_test.go: a test that would have caught it
More in Defer
Build "Closing a channel twice" yourself
BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go traps are free. This one comes with the all labs plan.