# While Loop in Binding does not stop the Script when removed

**URL:** <https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [September 6, 2023, 4:48pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825 "2023-09-06T16:48:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zachary\_Kacel1](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zachary\_Kacel1](https://forum.inductiveautomation.com/u/Zachary_Kacel1)\
**Post date:** [September 6, 2023, 4:48pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/1 "2023-09-06T16:48:12Z")

</div>

While experimenting with While loops, I've concluded that I should avoid using them. However, during my usage of While loops, I believe I've identified a bug in Property Binding Scripts.

If I unintentionally create an infinite while loop within a Property Binding and then remove it, the script continues to run even after the code is removed. Furthermore, if I create an infinite loop and then hit the apply button, I end up with two threads running even after removing the while loop and clicking apply. I've only allowed these threads to run for a few minutes, so I'm unsure if they will eventually terminate. Is this behavior normal for Property Binding Scripts, or is something unusual happening?

I can duplicate this every time by just creating a new property binding and adding an infinite while loop then removing the loop from the script.

 ![while_loop](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/7/87b26c8638eb43ad1735aa0ef4a197dbc40cd52d.jpeg)  
 ![while_loop_2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1d36b19c1ef447ccb8812068d5ecbc321e5718e8.jpeg)

---

<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:** [September 6, 2023, 4:50pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/2 "2023-09-06T16:50:57Z")

</div>

Not a bug in Ignition. It is a bug in your code. An infinite loop (or a very long duration loop) within an event is pathological in an event-driven system. If you can't make your events run in a few milliseconds, then you should be using timers and state machines. (And, no, that doesn't mean using any form of `.sleep()`. The event routine must **exit**.)

And no, they won't eventually end. They will burn CPU time until you restart the gateway.

---

<div class="post-metadata">

**Author:** ![Zachary\_Kacel1](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zachary\_Kacel1](https://forum.inductiveautomation.com/u/Zachary_Kacel1)\
**Post date:** [September 6, 2023, 4:54pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/3 "2023-09-06T16:54:53Z")

</div>

So even after I correct my mistake in the script I will still need to restart the gateway to free it back up?

---

<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:** [September 6, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/4 "2023-09-06T16:56:08Z")

</div>

Yes, editing an event script doesn't stop threads already running it. (This is true everywhere in Ignition.)

---

<div class="post-metadata">

**Author:** ![Zachary\_Kacel1](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zachary\_Kacel1](https://forum.inductiveautomation.com/u/Zachary_Kacel1)\
**Post date:** [September 6, 2023, 4:57pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/5 "2023-09-06T16:57:04Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 6, 2023, 9:33pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/6 "2023-09-06T21:33:25Z")

</div>

> [@pturmel](#):
>
> And no, they won't eventually end. They will burn CPU time until you restart the gateway.

I thought I had been able to kill someone else's infinite loop scripts using the gateway's Status | Diagnostics | Running Scripts | Cancel. This didn't require a gateway restart as far as I can remember.

---

<div class="post-metadata">

**Author:** ![carlosbqz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carlosbqz/32/52324_2.png) [@carlosbqz](https://forum.inductiveautomation.com/u/carlosbqz)\
**Post date:** [September 6, 2023, 9:48pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/7 "2023-09-06T21:48:06Z")

</div>

> [@Transistor](#):
>
> I thought I had been able to kill someone else's infinite loop scripts using the gateway's Status | Diagnostics | Running Scripts | Cancel.

I second this, I was able to stop an accidental infinite loop by cancelling it using the _Running Scripts_ tab (clarifying that I am not an expert on how Java works, so not totally sure if it's necessary to restart the gateway 😅).

---

<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:** [September 6, 2023, 10:35pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/8 "2023-09-06T22:35:15Z")

</div>

I wouldn't do it on a production server. The JVM can become unstable, supposedly. Killing threads is a legacy java feature that comes with big warnings.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 6, 2023, 10:45pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/9 "2023-09-06T22:45:07Z")

</div>

The 'cancel' mechanism hooks into Jython to interrupt the next stack frame/call/etc performed.  
So, it's not using the same thread death mechanism Java explicitly warns you against... it also won't stop, in theory, a totally pointless busy loop, but as long as there's _some_ statement inside the loop (which there (always?) should be, it will stop just that executing thread.

---

<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:** [September 6, 2023, 10:47pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/10 "2023-09-06T22:47:29Z")

</div>

> [@paul-griffith](#):
>
> but as long as there's _some_ statement inside the loop

The example in this topic did not have any such statement. Just `pass`. (Must be I/O or synchronization/locking operations to be interruptible, IIRC.)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 6, 2023, 10:58pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/11 "2023-09-06T22:58:16Z")

</div>

Still works. We're adding a `TraceFunction` to the interpreter thread, and the `pass` is hit on `traceLine`, even though it's not actually a 'real' statement, so we're still able to stop there (by simply throwing an unexpected exception).

[https://www.javadoc.io/static/org.python/jython-standalone/2.7.1/org/python/core/TraceFunction.html](https://www.javadoc.io/static/org.python/jython-standalone/2.7.1/org/python/core/TraceFunction.html)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/2/b2c121f3382b63ec9a757d98520491f5c85fd5b6.png)

```auto
com.inductiveautomation.ignition.common.script.ScriptManager$ScriptCanceledError: Script canceled: 
	at com.inductiveautomation.ignition.common.script.ScriptManager$BreakTraceFunction.doBreak(ScriptManager.java:1043)
	at com.inductiveautomation.ignition.common.script.ScriptManager$BreakTraceFunction.traceException(ScriptManager.java:1066)
	at org.python.core.PyException.<init>(PyException.java:55)
	at org.python.core.PyException.<init>(PyException.java:43)
	at org.python.core.Py.JavaError(Py.java:547)
	at org.python.core.PyTableCode.call(PyTableCode.java:182)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:306)
	at org.python.core.PyFunction.function ___call__ (PyFunction.java:474)
	at org.python.core.PyFunction. __call__ (PyFunction.java:469)
	at org.python.core.PyFunction. __call__ (PyFunction.java:464)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:846)
	at com.inductiveautomation.ignition.client.script.DesignerSystemUtilities.lambda$_invokeAsyncImpl$1(DesignerSystemUtilities.java:140)
	at java.base/java.lang.Thread.run(Thread.java:829)

Ignition v(Dev Version)
Java: Azul Systems, Inc. 11.0.17

```

---

<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:** [September 6, 2023, 11:08pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/12 "2023-09-06T23:08:53Z")

</div>

> [@paul-griffith](#):
>
> We're adding a `TraceFunction` to the interpreter thread

Sneaky. Wonder what the performance consequences are.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 6, 2023, 11:24pm UTC](https://forum.inductiveautomation.com/t/while-loop-in-binding-does-not-stop-the-script-when-removed/78825/13 "2023-09-06T23:24:02Z")

</div>

Jython lets you swap in a trace function 'live', so no implication until we try to interrupt.
