Fix deadlock during shutdown