LPS-30376 Fix possible infinite loop + tests