Problem Statement
During a Google SRE interview, you're asked to explain the nuanced behavior of Go channels. A production outage was caused by a goroutine leak due to improper channel handling. Walk through all channel edge cases.
Channel Behavior Matrix
| Operation | Nil Channel | Closed Channel | Open Channel |
|---|---|---|---|
ch <- value | Blocks forever | PANIC | Sends or blocks |
<-ch | Blocks forever | Returns zero value + false | Receives or blocks |
close(ch) | PANIC | PANIC | Closes successfully |
The Goroutine Leak Scenario
The Fix: Context-Based Cancellation
Interview Questions
- Q: What happens if you read from a nil channel?
A: It blocks forever. This is actually useful in select statements to disable a case. - Q: Why would you intentionally use a nil channel?
A: To dynamically disable a select case without removing the code. - Q: How do you detect a closed channel?
A: Use the two-value receive:val, ok := <-ch. Ifokis false, channel is closed.
Real-World Production Issue
At Twitch, a service leaked 50,000 goroutines because websocket handlers were waiting on a channel that was never closed when clients disconnected. The fix: Always use context cancellation for cleanup.