dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.21k stars 377 forks source link

Prevent OOM cases with closed channels. #1611

Open fogfish opened 1 month ago

fogfish commented 1 month ago

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)
}
fogfish commented 1 month ago

The issue has been fixed in Go 1.23 https://pkg.go.dev/time#After. I think worth spending time, WDYT?