Fix deadlock on shutdown