Go: Why Goroutines Outlive Canceled Requests
Outline
Transcript
0:00 In Go, a goroutine runs work independently, like fetching a search result. Canceling the request can leave that worker stuck trying to send the result back. We'll trace the bug to one line and show how the worker can exit. For example, in our test the code waiting for the result returned early. That left the worker with no way to finish. That's a goroutine leak, and Go counted one. So let's start with where the worker got stuck. A result channel lets our two goroutines pass a value. The side that takes it is the receiver.
0:33 The channel has no buffer, no place to store the value. When the worker sends 42, it needs that receiver. Take a search request that runs out of time before the lookup finishes. I think it's easy to assume both sides stop there. Why doesn't the worker just drop the result? When the request ended, the only receiver went with it. Go puts the worker on that channel's list of waiting senders. We followed that in Go's code. The links and the Go team's explanation are in the description. When I read code like this, I'd start with the line where the worker stopped.
1:12 Now look at the worker's stack, the list of calls that led to the send. It only shows where the worker is now. A send can be waiting now and still go through later. If I looked just before somebody took the value, would I see the same thing? It would. The leak profile, the report that found our worker, asks a different question. Look at the lines to the channel. Can another goroutine still run and reach it? Follow the first path. Another goroutine can still get to that channel. Go can't rule out a receiver there.
1:48 On the second path, only the blocked sender holds it. It can't make itself a receiver while it's stuck on that line. Go uses garbage collection, which normally looks for memory the program no longer needs, to follow those connections. The report counted our sender. With the send identified, we can give the worker an exit right there. A select lets the worker wait until one of the choices is ready. It can offer 42 or watch the request's done channel for a signal to leave. The stack showed the worker waiting in select.
2:20 Canceling closes that done channel. We waited for the worker's separate exit signal, then took the profile. Zero this time. Consider a buffer for just one result. Could we skip the select here? In our one-send example, yes. The value would fit even with nobody receiving. Watching cancellation is how we let the worker leave without delivering it. That gives the send an exit. I think the location matters, too. If an earlier call is stuck, the worker can't reach this line. You'd have to give that part of the code a way to stop when the request is canceled.
2:57 And that zero count can hide a stuck worker. We ran the send again with its channel stored in a registry, a map that stores channels by request. That map still gives the program a way to find the channel. What did the report say? 0. We checked the stack again. Same stuck sender, much more reassuring number. I'd check both sides. Find where the result could go and what lets the sender leave if nobody takes it. Thanks for listening to Learning Podcasts.