Skip to content

WHILE loop #4084

Closed
Closed

Description

@pekkaklarck

We've had FOR loops for a long time and having WHILE loops would be handy as well. The most important use case would probably be implementing custom polling solutions. That would be more flexible and powerful syntax than what can be currently done with Wait Until Keyword Succeeds. The syntax would be something like this:

WHILE    <expression>
    Keyword
    Another Keyword
END

Naturally you should be able to nest WHILE loops with other control structures. For example, IF in combination with planned BREAK and CONTINUE (#4079) would often be useful.

Activity

  1. added this to the v5.0 milestone on Sep 10, 2021
  2. changed the title [-]WHILE loop[/-] [+]`WHILE` loop[/+] on Sep 20, 2021
  3. pekkaklarck commented on Jan 23, 2022

    @pekkaklarck
    MemberAuthor

    This is now mostly ready. Remaining things to do are:

  4. added a commit that references this issue on Jan 26, 2022
  5. pekkaklarck commented on Jan 27, 2022

    @pekkaklarck
    MemberAuthor

    --flattenkeywords is now supported and that ought to be the last functional change related to this issue. Documentation still missing but that's covered by #4203.

  6. pekkaklarck commented on Feb 15, 2022

    @pekkaklarck
    MemberAuthor

    A problem with WHILE loops is that it is very easy to create a loop that never terminates. There could be a bug in the condition itself or code that should be updating a variable used in the condition could have problems. This is pretty severe problem, because the only way to stop such a rampant loop is stopping the whole execution forcefully. That means that that output.xml is broken and log.html cannot be created.

    To ease avoiding infinite loops, we decided to allow limiting how many times or how long time a WHILE loop is executed. The idea is that if the limit is searched, the loop is not executed anymore and execution fails. The limit would be specified using an optional second argument limit that needed to be used as named argument like limit=<value>. The value could either contain the maximum number of loop iterations or the maximum time to try running the loop. The value should have same semantics as the retry argument used with Wait Until Keyword Succeeds:

    WHILE    $condition    limit=20x
        Keyword
    
    WHILE    $condition    limit=1 minute
        Keyword
    

    To avoid infinite loops by default, it probably would be a good idea to have a default limit. That limit needed to be high enough to not affect normal valid usages but small enough to actually break the loop at some point. Probably something like 100 times would be good. It should be possible to disable the limit with level=NONE (case-insensitive value) as well.

    Because execution would fail if the limit is reaches, this functionality couldn't be used for executing some keywords a certain number of times. That isn't a big loss because FOR IN RANGE loops can be used instead in such situations.

  7. pekkaklarck commented on Feb 15, 2022

    @pekkaklarck
    MemberAuthor

    Implementing the limit functionality shouldn't be too complicated and we can do it still before RF 5.0. There aren't much open design decisions either. Only things I can think of are:

    • What postfix or postfixes to use when the limit is the number of iterations? Wait Until Keyword Succeeds supports x and times and for consistency they are probably good here as well. Something like limit=20 iterations could be nice in this context as well, but having too many of these probably isn't a good idea.
    • Should limit=20 be considered the max number of iterations (20 iterations) or the max time (20 seconds)? It's hard to choose and the best solution could be making it an error.
  8. pekkaklarck commented on Mar 9, 2022

    @pekkaklarck
    MemberAuthor

    On Slack not everyone was happy about the addition as there are use cases, especially when doing RPA, to have very long or even infinite loops. Endless loop requiring the execution to be forcefully stopped and leaving a broken output.xml and no log or report was nevertheless considered so bad that the limit functionality was added. Based on the feedback the default limit was raised to 10000 iterations, though.

    It was also decided that just using a number like limit=100 means iterations. Prefixes like x or times are not allowed. It is still possible to use limit as a time by using e.g. limit=100s.

  9. pekkaklarck commented on Mar 11, 2022

    @pekkaklarck
    MemberAuthor

    The above commit made the default limit a module level constant. It's now fairly easy to configure it if needed:

    from robot import run_cli
    from robot.running import bodyrunner
    
    bodyrunner.DEFAULT_WHILE_LIMIT = 10_000_000_000_000_000
    run_cli()

    This isn't ideal because you cannot simply disable the limit and because bodyrunner isn't part of the public API. We can add a better API for configuration in RF 5.1. Possibly there could also be a command line option controlling it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions