# Infinite loop in web dev freezes the gateway

**URL:** <https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 3, 2021, 11:06am UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388 "2021-06-03T11:06:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 11:06am UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/1 "2021-06-03T11:06:18Z")

</div>

I know, it’s the programmer’s fault. But it still shouldn’t take up all the CPU and memory, and cause the gateway to be unresponsive.  
Once, I forgot a _i+=1_. As soon as the web page is opened, the gateway will be frozen.  
Maybe it shouldn’t, but I replicated the phenomenon on my server.

```auto
	path=[]
	LineNum=14
	i=1
	while i<=LineNum:
		if i<10:
			path.append('[default]Capacity/ZuJian2/currentPlanOutput/L0'+str(i))
		else:
			path.append('[default]Capacity/ZuJian2/currentPlanOutput/L'+str(i))
		i+=1
	PlanData=system.tag.readBlocking(path)
	PlanOut=[]
	PlanHour=[]
	i=0
	print(PlanOut)
	H=system.date.getHour24(system.date.now())
	while i<LineNum:
		PlanOut.append(PlanData[i].value)
		PlanHour.append(int(PlanData[i].value)*H/24)
		i+=1
	
	path=[]
	i=1
	while i<=LineNum:
		if i<10:
			path.append('[default]Capacity/ZuJian2/currentCompletedOutput/L0'+str(i))
		else:
			path.append('[default]Capacity/ZuJian2/currentCompletedOutput/L'+str(i))
		i+=1
	Data=system.tag.readBlocking(path)
	RealOut=[]
	i=0
	while i<LineNum:
		RealOut.append(Data[i].value)
		i+=1
		
	info=[]
	i=0
	while i<LineNum:
		info.append({"PlanOut":PlanOut[i],"PlanHour":PlanHour[i],"RealOut":RealOut[i]})
		i+=1
	
	return {'json': system.util.jsonEncode(info)}

```

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [June 3, 2021, 1:12pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/2 "2021-06-03T13:12:00Z")

</div>

> [@nideyijuyidong](#):
>
> it still shouldn’t take up all the CPU and memory, and cause the gateway to be unresponsive.

With an infinite loop that's exactly what I would expect. TheGateway is having to allocate more memory each time through the loop. Incrementally it's not a large amount of memory but computers are extremely fast. It makes sense to me that the gateway could use all of the available memory instantly.

---

<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:** [June 3, 2021, 1:12pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/3 "2021-06-03T13:12:19Z")

</div>

Consider using a for loop instead of while. It would certainly avoid infinite loops.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 1:21pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/4 "2021-06-03T13:21:14Z")

</div>

But the gateway should not be unresponsive. What is the use of a server that is not responding.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [June 3, 2021, 1:23pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/5 "2021-06-03T13:23:55Z")

</div>

Not very useful. Be more careful about what you program. You’ve been given a tool with a sharp edge.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 1:25pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/6 "2021-06-03T13:25:54Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 3, 2021, 1:28pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/7 "2021-06-03T13:28:10Z")

</div>

The server was just trying to do what you told it. Infinite loops are not the servers fault. Definitely try using for loops as @JordanCClark mentioned, or list comprehensions.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 1:29pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/8 "2021-06-03T13:29:23Z")

</div>

Is there any way to kill a single process instead of the entire gateway? This might make debugging more convenient.

---

<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:** [June 3, 2021, 1:30pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/9 "2021-06-03T13:30:12Z")

</div>

As per @JordanCClark, you should use for loops wherever possible. Consider learning to use list comprehensions and some of python/jython’s built-in functions that manipulate lists. Your entire script can be much shorter and, IMNSHO, more readable:

```auto
	LineNum=14
	fmt1 = '[default]Capacity/ZuJian2/currentPlanOutput/L%02d'
	paths1 = [fmt1 % (i+1) for i in range(LineNum)]
	fmt2 = '[default]Capacity/ZuJian2/currentCompletedOutput/L%02d'
	paths2 = [fmt2 % (i+1) for i in range(LineNum)]

	values = [x.value for x in system.tag.readBlocking(paths1+paths2)]
	PlanOut = values[:LineNum]
	RealOut = values[LineNum:]

	H=system.date.getHour24(system.date.now())
	PlanHour = [x*H/24 for x in PlanOut]
	info = [{'PlanOut': po, 'PlanHour': ph, 'RealOut': ro} for (po, ph, ro) in zip(PlanOut, PlanHour, RealOut)]

	return {'json': system.util.jsonEncode(info)}

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [June 3, 2021, 1:32pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/10 "2021-06-03T13:32:54Z")

</div>

There is an area on the gateway status page that shows running scripts and allows them to be canceled but I don’t know if WebDev is included in this area. It’s under Status \> Diagnostics \> Running Scripts.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 1:33pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/11 "2021-06-03T13:33:11Z")

</div>

Thank you for your advice. Regardless of ignition or python, I am a novice(and English…).

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 1:34pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/12 "2021-06-03T13:34:18Z")

</div>

In fact, this is exactly the problem. The gateway status page is also unresponsive.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [June 3, 2021, 1:36pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/13 "2021-06-03T13:36:25Z")

</div>

I just think that as a user-oriented server, a single thread should not occupy all resources.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [September 30, 2022, 6:34pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/15 "2022-09-30T18:34:35Z")

</div>

I have tried all of the suggestions, but I've yet to successfully recover from this scenario without a restart. If you're successful, please let me know how you did it.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [September 30, 2022, 11:23pm UTC](https://forum.inductiveautomation.com/t/infinite-loop-in-web-dev-freezes-the-gateway/47388/16 "2022-09-30T23:23:24Z")

</div>

I have no idea about it. so I replaced all while to for....  
I evaluate the number of loops and set the breakout condition. Make sure the loop completes when the code is OK, and can end when the code is wrong.
