repository: Fixed a potential infinite loop in recover_segment