go runtime_error ai_generated true

panic: runtime error: invalid memory address or nil pointer dereference (in goroutine)

ID: go/goroutine-nil-pointer

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.20 active — — —
1.22 active — — —

Root Cause

A nil pointer is dereferenced inside a spawned goroutine. Because the panic happens in a child goroutine, it crashes the whole program and cannot be recovered from the parent.

generic

中文

在派生的 goroutine 中解引用了空指针。由于 panic 发生在子 goroutine 中,会导致整个程序崩溃,父 goroutine 无法 recover。

Workarounds

  1. 93% success
    go func() {
        defer func() {
            if r := recover(); r != nil {
                log.Printf("goroutine panic: %v", r)
            }
        }()
        doWork()
    }()
  2. 90% success
    if cfg == nil { return errors.New("nil cfg") }
    go func(c Config) { use(c) }(*cfg)

Dead Ends

Common approaches that don't work:

  1. 95% fail

    recover only works in the panicking goroutine's own deferred function; the parent cannot catch it.

  2. 80% fail

    Race-dependent nil dereference only triggers under specific interleavings; production crashes intermittently.