Fix deadlock while shutting down.