go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

Prevent OOM cases with closed channels.

Open fogfish opened this issue 1 year ago • 1 comments

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 avatar Oct 16 '24 07:10 fogfish

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

fogfish avatar Oct 16 '24 07:10 fogfish