On Fri, Nov 22, 2024 at 1:53 AM 'Hartmut Wieselburger' via golang-nuts
<
golan...@googlegroups.com> wrote:
>
> var configOnce sync.Once
> func Reset() {
> configOnce = sync.Once{}
> }
This is not resetting. You are creating a new sync.Once and assigning
that to a variable. That variable, configOnce, is shared among
multiple goroutines, because that is what Once is used for, to limit
the execution of a function called by multiple goroutines to a single
execution. The Reset function sets this shared variable without any
synchronization, so this is a data race.
Also note that reassigning a Once instance like this may not behave as
you expect. Any goroutine that is waiting on that Once instance will
continue waiting until the Once function is done. The behavior of a
goroutine that reads Once after Reset() is undefined. Since there is a
data race, there is no guarantee on which instance of Once another
goroutine will see. A goroutine may see the completed instance of Once
even after you Reset() it.
> To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/f3df358a-d4ae-441c-9ff6-30af9ad1c568n%40googlegroups.com.