Go 服务中 context 取消不生效导致 goroutine 泄漏的排查与修复
线上一个用 Go 写的 HTTP 服务跑了几周后内存缓慢上涨,pprof 看到 goroutine 数量从几百涨到上万不回落。服务逻辑不复杂:接收请求后启动几个后台 goroutine 查数据库、调下游接口,请求结束时应该都退出。但实际情况是不少 goroutine 卡在 channel 发送或等待下游响应上,父 context 已经 cancel 了它们却感知不到。问题出在对 context 传播的误解上:很多代码把 context 当参数传下去,却在 goroutine 内部用 context.Back...