# Channel Misuse - ID: go-channel-misuse - Severity: HIGH - CWE: CWE-833 (CWE-833) - Languages: Go ## Description Improper channel usage can cause deadlocks, goroutine leaks, or panics. Common issues include send/receive on unbuffered channel in same goroutine, not closing channels, and sending on closed channels. ## Detection Message Channel misuse can cause: - Deadlocks (blocking operations without receiver/sender) - Goroutine leaks (receivers waiting forever on unclosed channel) - Panics (sending on closed channel) ## Remediation 1. Use buffered channels when sender/receiver in same goroutine: ```go ch := make(chan int, 1) // Buffered ch <- 42 val := <-ch ``` 2. Always close channels when done sending: ```go go func() { defer close(ch) // Prevents goroutine leaks for _, item := range items { ch <- item } }() ``` 3. Use select with timeout/context for network operations: ```go select { case result := <-resultCh: handle(result) case <-ctx.Done(): return ctx.Err() case <-time.After(5 * time.Second): return errors.New("timeout") } ``` 4. Never send on closed channel: ```go // Use sync primitives to coordinate close var once sync.Once closeCh := func() { once.Do(func() { close(ch) }) } ``` ## Documentation [object Object]