This is node.js.
I have a function that might become an infinite loop if several conditions are met. Untrusted users set these conditions so for the purpose of this question please assume the infinite loop is unfixable.
Still I need a way to stop the infinite loop.
Here is some sample code for what i’m trying to do:
var infiniteloop = false;
var condition = true
function loop () {
while (condition) {
console.log('hi')
if (infiniteloop) {
condition = false
console.log('oh last one')
}
}
}
loop()
So a few questions based on what I’m trying to do.
- If the
infiniteloopvariable is set to true, the loop will stop right? - How do I detect the infinite loop? Something that checks every 3 seconds would be good.
- The
infiniteloopvariable cannot be changed while it’s looping if it’s on the same process. I have to store the variable in a different process? - Whatever detects the infinite loop needs to live in a different process? Ideally same process would be nice but whatever works?
Thanks for your help.
A solution based on a mix of the other proposals:
Not sure this is optimal, but that should work as expected.
The use of setTimeout to resume the loop allows the main node.js event loop to process other events, such a w.Stop.