add a limit to prevent deadloop.