请描述 goroutine 永不退出的典型原因(channel 无人接收/发送、context 未取消、WaitGroup 误用等)及定位手段。
1. channel 阻塞:向无缓冲或已满的 channel 发送,但接收方已退出;或从已无发送方的 channel 永久接收。
2. context 未传播/未取消:下游 goroutine 在 select { case <-ctx.Done(): ...; case <-ch: ...} 中若只等 ch 而 ctx 没被上层 cancel,会一直挂起。
3. WaitGroup 计数不匹配:Add 与 Done 次数不一致,导致 Wait() 永不返回,等待方卡死。
4. time.After 在循环里被 select 提前跳出,定时器未被触发而堆积(应改用 time.NewTimer 并 Stop)。
5. http/连接未设超时,读写出错时 goroutine 阻塞在 I/O。
runtime.NumGoroutine() 观察数量只增不减;pprof 的 goroutine profile(go tool pprof + goroutine 栈)定位阻塞点。ctx.Done();明确 channel 的关闭方;限制并发(worker pool);为网络/IO 设置超时;在测试中断言 goroutine 数回落。题目与答案就在本页;收藏、批注和错题重练在应用内