go runtime_error ai_generated true

panic: sync: Cond.Broadcast called without lock held

ID: go/condition-broadcast-without-lock

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-10-09First Seen

Version Compatibility

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

Root Cause

sync.Cond.Broadcast() or Signal() is called without holding the associated Locker, violating the documented contract.

generic

中文

在未持有相关联的 Locker 的情况下调用 sync.Cond.Broadcast() 或 Signal(),违反了文档约定。

Workarounds

  1. 96% success
    c.L.Lock()
    // mutate predicate
    c.Broadcast()
    c.L.Unlock()
  2. 85% success
    close(done) // wakes all waiters without a Cond

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Still not holding the lock when Broadcast executes; panic persists.

  2. 70% fail

    Burns CPU and reintroduces races on the shared predicate.