go resource_error ai_generated true

fatal error: runtime: out of memory

ID: go/goroutine-not-scheduled

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-06-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

Creating an excessive number of goroutines without limit, exhausting system memory.

generic

中文

无限创建 goroutine,导致系统内存耗尽。

Workarounds

  1. 95% success
    Use a worker pool pattern: 
    
    const maxWorkers = 10
    jobs := make(chan int)
    var wg sync.WaitGroup
    for i := 0; i < maxWorkers; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            for j := range jobs {
                process(j)
            }
        }()
    }
    // send jobs and close
  2. 90% success
    Use semaphore channel to limit goroutine count: 
    
    sem := make(chan struct{}, 10)
    for _, task := range tasks {
        sem <- struct{}{}
        go func(t task) {
            defer func() { <-sem }()
            process(t)
        }(task)
    }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Temporary fix; doesn't solve the underlying goroutine leak.

  2. 70% fail

    WaitGroup doesn't limit concurrency; it only waits for all to finish.