Simple automation with a while loop completely locks HA

I’ve setup a simple automation to turn off a light after a fixed amount of time, but only if another switch (bath fan) isn’t on. The automation sometimes works, but other times it completely locks up HA entirely and I have to restart the VM. I’ve tried to look at the trace, but even viewing the most recent trace locks up HA as well as memory leaks the local browser until it crashes. Yikes! Code below. How can I begin diagnosing this?

id: '1658176525901'
alias: Turn off Master Bath Lights
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: fbb9748934f195c222d548b0684302d2
    entity_id: light.master_bath_lights
    domain: light
condition: []
action:
  - repeat:
      until:
        - condition: device
          type: is_off
          device_id: 009b8d522334124c706d35382e49d204
          entity_id: switch.master_bath_main_fan
          domain: switch
          for:
            hours: 0
            minutes: 10
            seconds: 0
      sequence: []
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: fbb9748934f195c222d548b0684302d2
    entity_id: light.master_bath_lights
    domain: light
mode: single

There is nothing in the while loop. I suspect you wanted to do this:

action:
  - repeat:
      until:
        - condition: device
          type: is_off
          device_id: 009b8d522334124c706d35382e49d204
          entity_id: switch.master_bath_main_fan
          domain: switch
          for:
            hours: 0
            minutes: 10
            seconds: 0
      sequence:
        - delay:
            hours: 0
            minutes: 2
            seconds: 0
            milliseconds: 0
        - type: turn_off
          device_id: fbb9748934f195c222d548b0684302d2
          entity_id: light.master_bath_lights
          domain: light
mode: single

OK. I crafted the automation in the UI, and that’s what seemed logical. I reworked it via the UI to put the actions into the sequence as you suggested. It hasn’t locked up HA yet, which is good. It’s horribly unreliable, though, which is worse. It’s evaluating the fan as off after a few iterations of the loop, even though it was on throughout the automation.

What happens if you use a state condition rather than a device condition, keeping it simpler?

action:
  - repeat:
      until:
        - condition: state
          entity_id: switch.master_bath_main_fan
          state: 'off'
          for:
            hours: 0
            minutes: 10
            seconds: 0

I didn’t try the state condition, but probably will in the future. I think I finally have it working as I want, although it is at least slightly odd that HA can’t handle a loop without an action inside it. There was a long-running earlier version of the script that was in the background messing with me. Once I clobbered that and put an action in the loop, it started behaving.

It will only do what you tell it to.

If you tell it to loop forever with nothing to do and no delay it’s going to consume an enormous amount of resources jumping back and forth between address pointers as fast as possible.