go runtime_error ai_generated true

panic: 运行时错误:无效内存地址或空指针解引用(发生在 goroutine 中)

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

ID: go/goroutine-nil-pointer

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-07-02首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.20 active — — —
1.22 active — — —

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 80% 失败

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