asifnoor
Kilo Patron

Hello Everyone,

Recently, when I was working on a project, I found 1 issue which was very strange and I wanted to share this with you all.  This issue has caused the instance to go into the infinite loop before I find and kill the transaction.

I have replicated the issue with some dummy variable on my developer instance and I am sharing those details here.  

Script Include

Name: TestFunction

Script:

var TestFunction = Class.create();
TestFunction.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    test1:function(k) {
		
		gs.log("You entered into infinite loop");
		for(i=0;i<k;i++) {
			
		}
	},
	test2: function() {
		for (i=0;i<4;i++) {
			for (j=0;j<4;j++) {
				this.test1(1);
			}
		}
	},
    type: 'TestFunction'
});

This is a very simple Script Include and you can call this from Background script like below

new TestFunction().test2();

The function test2 is called which has some simple loop and it internally calls test1(). once both the loops are ended, the script will stop.

But BEWARE, this goes into an infinite loop.

WHY?????

After seriously analyzing the code, I found that the variable i is used in both test2() and test1(). It is NOT a problem in general as the scope will be different.

But in our case, since we are calling test1() from test2() using the THIS object, the scope of variables in test2() is available in test1() and because of which, the variable I that was used in test2() is getting reset to 0 in test1() and hence the loop in test2() never ends.

So we shall be careful with the variable names. Just use another variable name, it solves the problem.

While I have shown a simple example here, in my project, I was actually triggering an event in test1() which fires a notification.

Imagine how many events and notifications got fired 🙂 🙂

 

And lastly, how do we stop when we get into such a situation where something got triggered and went into an infinite loop.

System Diagnostics -> Active Transactions

Look for the transaction based on the age column. If the age is more like (5 minutes), then select that transaction by checking the checkbox and under actions dropdown, select kill. This will kill the transaction.

Let me know if you have any questions in the comments.

Mark the article as helpful and bookmark if you found it useful.

 

 

Version history
Last update:
‎10-17-2019 03:47 AM
Updated by: