Fixed race condition in shutdown process.