go runtime_error ai_generated true

panic: runtime error: goroutine stack exceeds 1000000000-byte limit

ID: go/goroutine-stack-growth-panic

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active
1.20 active

Root Cause

A goroutine has infinite recursion or excessive stack usage, causing stack overflow.

generic

中文

goroutine存在无限递归或栈使用过多,导致栈溢出。

Workarounds

  1. 95% success Convert recursion to iteration
    // Before: func factorial(n int) int { if n <= 1 { return 1 }; return n * factorial(n-1) }
    // After:
    func factorial(n int) int {
        result := 1
        for i := 2; i <= n; i++ {
            result *= i
        }
        return result
    }
  2. 90% success Use a bounded depth-first search with explicit stack
    type frame struct { n int; result int }
    stack := []frame{{n: initial}}
    for len(stack) > 0 {
        // process
    }

Dead Ends

Common approaches that don't work:

  1. Increasing stack size via debug.SetMaxStack 90% fail

    Only postpones the crash; doesn't fix the underlying bug.

  2. Using more goroutines to distribute work 80% fail

    Each goroutine has its own stack; doesn't solve recursion issue.