Fix deadlock: take mutex for as short as possible