Cherry-pick changes to while-loop.