# How to detect and stop an endless loop in a gateway tag change event script

**URL:** <https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039>\
**Category:** General Discussion\
**Created:** [December 19, 2024, 12:09pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039 "2024-12-19T12:09:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Reed](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Robert\_Reed](https://forum.inductiveautomation.com/u/Robert_Reed)\
**Post date:** [December 19, 2024, 12:09pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039/1 "2024-12-19T12:09:08Z")

</div>

I am going to create a script that will use many consecutive loops waiting on a tag changes to progress. How can I tell if a script is stuck waiting for the tag change event to exit the loop? I do plan on haveing a timeout feature but as I develop the script I expect to have failures so want to be prepared for finding and stopping endless loops.

pseudo code would be:  
Set transaction group handshake tag to zero  
Trigger transaction group  
Loop while transaction group handshake tag = zero .

Set transaction group handshake tag to zero  
Trigger transaction group  
Loop while transaction group handshake tag = zero .

Set transaction group handshake tag to zero  
Trigger transaction group  
Loop while transaction group handshake tag = zero .

and so on for 100 repeats.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 19, 2024, 1:03pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039/2 "2024-12-19T13:03:50Z")

</div>

> [@Robert\_Reed](#):
>
> script that will use many consecutive loops waiting on a tag changes to progress

This is an extremely dangerous script design in Ignition. Don't do any kind of busy-wait or sleep-wait in any Ignition event script. Full stop.

Instead, create a state machine, using either a memory tag, or, more robustly, a top-level project script collection object (typically a dictionary). Use gateway tag change events and gateway timer events to drive forward progress. Use a `threading.RLock` object instance to protect against simultaneous changes from different threads.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 19, 2024, 2:40pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039/3 "2024-12-19T14:40:48Z")

</div>

You may also want to look at the SFC module.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 19, 2024, 2:53pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039/4 "2024-12-19T14:53:48Z")

</div>

> [@JordanCClark](#):
>
> SFC

Ewww! Or not. 😢

---

<div class="post-metadata">

**Author:** ![Robert\_Reed](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Robert\_Reed](https://forum.inductiveautomation.com/u/Robert_Reed)\
**Post date:** [December 19, 2024, 2:56pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039/5 "2024-12-19T14:56:41Z")

</div>

Per your comments I am changing my plan to have zero wait loops..  
Thanks

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 19, 2024, 3:06pm UTC](https://forum.inductiveautomation.com/t/how-to-detect-and-stop-an-endless-loop-in-a-gateway-tag-change-event-script/100039/6 "2024-12-19T15:06:20Z")

</div>

SFC has been reliable where I've used them. OTOH, I don't use them outside of a gateway scope either. I seem to notice that when people try to use them to interact with a client that things go pear shaped. Just my observation. That and a couple bucks will buy you a soda. 😉
