Go traps · Chapter 11 of 16

A goroutine stuck on a channel

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.

  1. Task 1

    repro.go: leave goroutines behind on purpose

    Write reproduce() in repro.go, which returns an int. Make a lookup that blocks until you release it (a func() string that receives from a channel you made, then returns), note runtime.NumGoroutine(), and call StatusWithin(lookup, time.Millisecond) from status.go 10 times, so every call times out. Then release the lookups (close the channel), give the goroutines up to 200 ms to finish (stop as soon as the count is back), and return how many more goroutines there are than before the calls.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original status.go it must return 10 (one goroutine stuck for every timeout), and beside a fixed status.go it must return 0. So it must really make 10 calls time out and then release the lookups. No input, and no waiting beyond those 200 ms: reproduce() must finish within 3 seconds.

  2. Task 2

    status.go: no goroutine left behind after a timeout

    Fix StatusWithin(lookup func() string, wait time.Duration) (string, error) in status.go so the goroutine it starts always finishes once lookup has returned, whether the caller got the answer or had already given up. When lookup answers within wait, it returns the answer and a nil error; when it does not, it returns "" after exactly wait, with an error wrapping ErrNoAnswer whose message is central system: no answer within <wait> (for 2s: central system: no answer within 2s).

    Keep ErrNoAnswer, the message, the wait and StatusWithin's name and types as they are. The check runs StatusWithin inside a synctest bubble with a fake clock, lets its slow lookups answer after the timeout, and fails if any goroutine is still blocked when the bubble ends. Waiting longer is not a fix: the caller must still get its error after exactly wait.

  3. Task 3

    status_test.go: a test that would have caught it

    Write a Go test file, status_test.go, that would have caught this leak before the evening peak. The check runs your file twice, each in a fresh build: beside the original status.go, where at least one test must fail, and beside the fixed status.go, where every test must pass.

    So, inside synctest.Test, call StatusWithin with a lookup that blocks until you release it (make the channel inside the bubble), check the error wraps ErrNoAnswer, then release the lookup and let the bubble end: a goroutine still blocked makes synctest fail the test with blocked goroutines remain. Test a fast lookup too. With synctest's fake clock no test waits for real; each must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: leave goroutines behind on purpose
  • status.go: no goroutine left behind after a timeout
  • status_test.go: a test that would have caught it

More in Defer

Build "A goroutine stuck on a channel" 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.