go runtime_error ai_generated true

panic: 运行时错误:无效的内存地址或空指针解引用 [signal SIGSEGV: 段错误 code=0x1 addr=0x0 pc=0x...]

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

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

版本兼容性

版本状态引入弃用备注
1.16+ active

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 75% 失败

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