go
resource_error
ai_generated
true
warning: goroutine sleeping in loop (goroutine 9)
ID: go/goroutine-sleep-in-loop
80%Fix Rate
82%Confidence
0Evidence
2024-09-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
Root Cause
A goroutine calls time.Sleep in a tight loop, consuming resources and potentially blocking.
generic中文
goroutine 在紧密循环中调用 time.Sleep,消耗资源并可能阻塞。
Workarounds
-
90% success
ticker := time.NewTicker(time.Second) defer ticker.Stop() for range ticker.C { // do work } -
85% success
select { case <-time.After(time.Second): // do work case <-ctx.Done(): return }
Dead Ends
Common approaches that don't work:
-
80% fail
Still busy-waits; doesn't solve the underlying issue.
-
70% fail
Gosched doesn't block; it just yields, but loop continues.
-
90% fail
Wastes CPU and may cause performance issues.