fix exit race condition