go runtime_error ai_generated true

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x...]

ID: go/nil-pointer-in-goroutine

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.16+ active

Root Cause

A goroutine dereferences a nil pointer, often because a shared struct field or captured variable was set to nil by another goroutine, or the goroutine started before its dependency was initialized.

generic

中文

goroutine 解引用了空指针,通常是因为共享结构体字段或被捕获的变量被另一个 goroutine 置为 nil,或 goroutine 在依赖初始化之前就启动了。

Workarounds

  1. 90% success
    mu.Lock()
    local := cfg
    mu.Unlock()
    if local == nil { return errors.New("cfg not initialized") }
    local.Do()
  2. 87% success
    cfg := loadConfig()
    go func(c *Config) {
        if c == nil { return }
        c.Do()
    }(cfg)

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Silently dropping the panic leaves the program in an inconsistent state and the nil root cause is never fixed.

  2. 75% fail

    The pointer may be mutated to nil by another goroutine after the check, so a TOCTOU race still crashes.