Fix race condition while restarting (fixes #560)

It seems to happen much more often now.