go
runtime_error
ai_generated
true
panic: sync: Once is reused
ID: go/once-reuse-panic
80%Fix Rate
80%Confidence
0Evidence
2024-01-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.19 | active | — | — | — |
Root Cause
Attempting to reuse a sync.Once after it has been used, which is not allowed.
generic中文
在 sync.Once 已被使用后再次尝试使用它,这是不允许的。
Workarounds
-
90% success
Use a flag with atomic operations to implement single execution: var done int32 go func() { if atomic.CompareAndSwapInt32(&done, 0, 1) { // do once } }() -
85% success
Ensure Once is used only once per lifecycle, and create a new instance if needed.
Dead Ends
Common approaches that don't work:
-
90% fail
Copying a sync.Once is unsafe and can cause races; it's not designed to be copied.
-
40% fail
If the Once is embedded in a struct that's reused, it may still be copied.