cookie: fix loop invariant assert and exit on cookie exhaustion