Finalizers introduce concurrency, but the synchronization between goroutines accessing the object and the finalizer are not defined. The proposed wording is along the lines of "SetFinalizer happens before execution of the finalizer starts". This makes the following program correctly synchronized:
http://play.golang.org/p/arA1niVAZC
However, the following program contains a data race on x.y and *x.y:
http://play.golang.org/p/cpuvxDu6AW
The program indeed can be compiled in a way that makes the panic to fire. Namely, compiler reorders "x.y = &i" and "*i = 42". This result can be unexpected.
Finalizers introduce concurrency, but the synchronization between goroutines accessing the object and the finalizer are not defined. The proposed wording is along the lines of "SetFinalizer happens before execution of the finalizer starts". This makes the following program correctly synchronized:
http://play.golang.org/p/arA1niVAZC
However, the following program contains a data race on
x.yand*x.y:http://play.golang.org/p/cpuvxDu6AW
The program indeed can be compiled in a way that makes the panic to fire. Namely, compiler reorders "x.y = &i" and "*i = 42". This result can be unexpected.