Fix race condition while restarting (fixes #560)