Fix race condition with refs writing.