Open markuspeloquin opened 7 months ago
Original implementation from #1319.
@jquirke @abhinav
Thanks for raising this. I agree atomic load and store are one solution, but a more elegant pattern might be to just return a new Core as has been suggested before, and avoid that torn interface assignment
Describe the bug A race exists in the WithLazy implementation.
lazyWithCore
's own methods are protected from it, but sinceCore
is embedded, methods fromCore
(in this case,Enabled()
) do an unsynchronized read.https://github.com/uber-go/zap/blob/fcf8ee58669e358bbd6460bef5c2ee7a53c0803a/zapcore/lazy_with.go#L41-L43
The trace below is for v1.27.0.
To Reproduce Create a logger using WithLazy, then use it in multiple goroutines in parallel.
Expected behavior No race.
Additional context Personally I would fix by replacing the embedded
Core
with an atomic.Pointer (as gross as these are for holding interfaces).