Fix race condition on closure variable