Go 的 goroutine 泄漏:没人接收的消息去哪了
一个 Go 服务上线三天,内存从 200MB 涨到 1.2GB,QPS 却没变。pprof 的堆内存看起来正常,直到打开 goroutine 面板:同一个调用栈,挂了四万多个 goroutine,全都停在 chan send 上。
goroutine 泄漏之所以难查,是因为它不像内存泄漏那样有明显的“对象”,也没有 panic。程序照常跑,只是每隔一段时间就多一些永远不会醒来的函数。
便宜,但不免费
新建一个 goroutine 的初始栈只有 2KB 左右,调度器还能按需扩容。这让 go 关键字用起来毫无心理负担,也让泄漏变得毫无心理负担。
但一个阻塞中的 goroutine 并不是“什么都不占”:
- 它的栈还在,栈上引用的对象 GC 收不掉;
- 它可能握着锁、数据库连接、文件描述符;
- 它挂在 channel 的等待队列上,runtime 需要维护这些结构;
- 它可能还在等一个永远不会到来的信号。
最关键的是:goroutine 没有外部句柄,你无法从外面杀掉它。它只能自己返回。所以每一个 go 关键字,都必须能回答一个问题:这个函数在什么条件下会退出?
四种最常见的泄漏
1. 往没人接收的 channel 发送
1func firstResult(urls []string) string {
2 ch := make(chan string) // 无缓冲
3 for _, u := range urls {
4 go func(u string) {
5 ch <- fetch(u) // 调用方一走,这里永远阻塞
6 }(u)
7 }
8 return <-ch // 只等第一个,其余全部卡死
9}firstResult 拿到第一个结果就返回了。剩下的 goroutine 还在往 ch 里发,而没有接收方,它们会一直挂在 channel 的发送队列上。QPS 一百,每次泄漏几十个,一天就是几百万。
修法有两条:给 channel 足够缓冲,让发送方不必等待;同时用 context 兜底。
1func firstResult(ctx context.Context, urls []string) (string, error) {
2 ctx, cancel := context.WithCancel(ctx)
3 defer cancel()
4
5 ch := make(chan string, len(urls)) // 缓冲够大,发送不会阻塞
6 for _, u := range urls {
7 go func(u string) {
8 v, err := fetch(ctx, u)
9 if err != nil {
10 return
11 }
12 select {
13 case ch <- v:
14 case <-ctx.Done():
15 }
16 }(u)
17 }
18
19 select {
20 case v := <-ch:
21 return v, nil
22 case <-ctx.Done():
23 return "", ctx.Err()
24 }
25}注意 defer cancel():函数返回时取消子 context,让还在跑的 fetch 尽快收工。忘了这一行,泄漏的就不只是 goroutine,还有 context 内部的定时器。
2. 等一个永远不会关闭的 channel
1func consume(ch <-chan int) {
2 for v := range ch { // ch 不关闭,循环永不结束
3 handle(v)
4 }
5}range 一个 channel,只有它被 close 才会退出。如果发送方在某个错误分支上直接 return,忘了 close,接收方就会等到天荒地老。
约定很简单:谁发送,谁关闭;接收方永远不要关 channel。如果发送方有多个分支,用 defer close(ch)。
另一个变体是 nil channel:
1var ch chan int
2go func() {
3 ch <- 1 // 永久阻塞
4}()nil channel 上的收发永远阻塞,且不会被调度器唤醒。反过来,select 里 nil channel 的分支会被直接禁用,所以 <-ctx.Done() 在 context.Background() 下返回 nil 也是安全的——那个 case 只是永远不触发。
3. context 传了,但没人听
1func start(ctx context.Context) {
2 go func() {
3 for {
4 doSomething() // 从不检查 ctx
5 time.Sleep(time.Second)
6 }
7 }()
8}参数里写着 ctx,函数体里一次都没看。调用方 cancel() 之后,这个 goroutine 仍然每秒醒一次,直到进程结束。context 只是传菜员,不会自动把刀架在 goroutine 脖子上。
1func start(ctx context.Context) {
2 go func() {
3 t := time.NewTicker(time.Second)
4 defer t.Stop()
5 for {
6 select {
7 case <-t.C:
8 doSomething()
9 case <-ctx.Done():
10 return
11 }
12 }
13 }()
14}顺带一提,doSomething() 本身如果会阻塞在 IO 上,也得把 ctx 传下去,比如 http.NewRequestWithContext,否则取消信号依然进不去。
4. 有界并发变成了无界堆积
1func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
2 go s.report(r) // 上报端一慢,goroutine 无限堆积
3 // ...
4}每个请求起一个 goroutine,看起来只是“异步化”。但下游如果抖一下,消费速度跟不上生产速度,goroutine 就会像队列一样无限增长——而这个队列没有长度上限,也没有拒绝策略。
正确做法是把并发度固定下来,队列满了就丢:
1type Reporter struct {
2 ch chan event
3}
4
5func NewReporter(n int) *Reporter {
6 r := &Reporter{ch: make(chan event, 1024)}
7 for i := 0; i < n; i++ {
8 go func() {
9 for e := range r.ch {
10 send(e)
11 }
12 }()
13 }
14 return r
15}
16
17func (r *Reporter) Report(e event) {
18 select {
19 case r.ch <- e:
20 default: // 队列满就丢,宁可少一条,也不要多一万个 goroutine
21 }
22}监控上报、日志投递、审计事件都适合这个模型:有界队列 + 固定 worker + 明确的丢弃策略。
把泄漏抓出来
最省事的办法是挂上 net/http/pprof:
1import _ "net/http/pprof"
2
3go func() {
4 log.Println(http.ListenAndServe("localhost:6060", nil))
5}()goroutine profile 和 CPU、heap 不同,它是全量快照,不做采样:
1# 按调用栈聚合,适合看“同一个栈有 3000 个”
2curl -s 'http://localhost:6060/debug/pprof/goroutine?debug=1' | head -40
3
4# 展开每个 goroutine 的完整栈
5curl -s 'http://localhost:6060/debug/pprof/goroutine?debug=2' | less
6
7# 图形化查看
8go tool pprof -http=:8080 'http://localhost:6060/debug/pprof/goroutine'判断方法很直接:连续抓两次,如果某个栈的数量只增不减,基本就是泄漏。重点关注栈顶停在 chan send、chan receive、select、IO wait 的那几类。
测试里可以用 runtime.NumGoroutine() 做粗检,或者直接上 goleak:
1func TestMain(m *testing.M) {
2 goleak.VerifyTestMain(m)
3}它会在测试结束时检查是否有非预期的残留 goroutine,把泄漏挡在合并之前。
一份检查清单
| 现象 | 常见原因 | 处理 |
|---|---|---|
栈顶是 chan send | 无缓冲或队列已满,接收方提前退出 | 加缓冲、select 加 ctx.Done() |
栈顶是 chan receive | 发送方没 close | 明确所有权,发送方 defer close |
栈顶是 select | 没有任何退出分支 | 补上 ctx 或超时 |
栈顶是 IO wait | resp.Body 没关、连接没释放 | defer resp.Body.Close(),读完 io.Copy(io.Discard, ...) |
| 数量随 QPS 线性增长 | 每请求起 goroutine,无界 | worker pool + 有界队列 + 丢弃策略 |
| 数量随超时时间增长 | 派生了 ctx 却没 cancel | defer cancel() |
goroutine 的生命周期必须有人负责。写 go 之前先想清楚三件事:它在等谁、谁会叫醒它、叫不醒时谁来收场。想不清这三件事,那行 go 大概率就是下一个泄漏点。
参考文档
- Bash/帮助手册/return - Primers 编程伙伴
- Bash/帮助手册/curl - Primers 编程伙伴
- Bash/帮助手册/head - Primers 编程伙伴
- Bash/帮助手册/less - Primers 编程伙伴
本文由 AI 辅助生成并经人工校验,如有疏漏欢迎指正。