go runtime_error ai_generated true

fatal error: concurrent atomic operations on misaligned memory

ID: go/goroutine-atomic-load-store-race

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2025-03-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

Using atomic operations on a variable that is not properly aligned, causing a panic on some architectures (e.g., ARM).

generic

中文

对未正确对齐的变量使用原子操作,在某些架构(如 ARM)上导致恐慌。

Workarounds

  1. 95% success Ensure atomic variables are 64-bit aligned, e.g., by using a struct with proper padding.
    type Aligned struct {
        _    [0]int64 // ensure alignment
        val  int64
    }
    var a Aligned
    atomic.StoreInt64(&a.val, 42)
    fmt.Println(atomic.LoadInt64(&a.val))
  2. 90% success Use sync.Mutex instead of atomic for non-critical paths.
    var mu sync.Mutex
    var val int64
    mu.Lock()
    val = 42
    mu.Unlock()
    mu.Lock()
    fmt.Println(val)
    mu.Unlock()

Dead Ends

Common approaches that don't work:

  1. Assuming atomic operations work on any variable regardless of alignment. 90% fail

    Some platforms require 64-bit alignment for 64-bit atomics; misalignment causes panic.

  2. Using a struct with atomic fields without ensuring alignment. 80% fail

    Fields may be packed in a way that causes misalignment.