Empty For Spin
ID |
go.empty_for_spin |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Efficiency |
Language |
Go |
Tags |
efficiency, suspicious |
Description
Reports a bare for {} loop — a for with no clause, no condition and an empty body. The
loop has nothing to evaluate and nothing to do, so it spins as fast as the scheduler allows.
Rationale
An empty infinite loop pins a CPU core at 100% and starves other goroutines of scheduler time
without making any progress. It is almost always a placeholder that should block on a channel
receive, sleep, or run real work guarded by a loop condition. Loops with a condition
(for cond {}) or a clause (for i := 0; …; i++ {}) are not reported even with an empty
body — they make progress or terminate.
for {} // FLAW — busy-spins, pinning a core
for { // OK — blocks on a channel
select {
case <-done:
return
}
}
Remediation
Block on a channel receive, add a time.Sleep, or give the loop a real condition so it makes
progress or yields the processor.