Fix deadlock when terminating app