go-tools
go-tools copied to clipboard
Prevent OOM cases with closed channels.
The following program runs with Out of Memory due to forgotten return statement after "ctrl" channel is closed. Would there be any possibility for capturing the cases through code analysis?
package main
import (
"fmt"
"time"
)
func main() {
ctrl := make(chan struct{})
go func() {
for {
select {
case <-ctrl:
case <-time.After(5 * time.Second):
fmt.Printf("==> print status\n")
}
}
}()
time.Sleep(10 * time.Second)
close(ctrl)
// OOM: here
time.Sleep(3600 * time.Second)
}
The issue has been fixed in Go 1.23 https://pkg.go.dev/time#After. I think worth spending time, WDYT?