Node-red stuck on while loop in function node

#1

Hello.
I've such test flow for async actions in function node:


Function code is:

    var success=0;
    node.send([{"payload":1},null]);
    do {
        success=flow.get("success");
    } while (!success) 
    return [null,{"payload":2}];

If I inject a timestamp, node-red will stuck, cause 100% cpu usage.
I've tried to add a delay by setTimeout, but it doesn't help.

0 Likes

#2

Sooooo, where do you store anything in the flow variable?

0 Likes

#3

In the Change node at the right.
Message from inject will pass function node and then go through first output via node.send(). After that function have to wait until flow.success will be changed after delay, and send a message to second output

0 Likes

#4

I may be wrong but I believe Node-RED is single threaded. When it first starts up and the inject node is fired the function runs. Now at this point flow.success has not been set, but you are grabbing it.

It could be that what ever is returned will evaluate to TRUE causing the loop to run forever...

0 Likes

#5

Node.js is single threaded. Putting a tight loop in your function node will tie up that single thread so nothing else can happen; your flow context value will never change.

You could use setInterval to poll the value at regular intervals and use node.send() to send from within the interval function. You'll also need to remember to clear the interval when done with it.

0 Likes

#6

I've rewrited function in such way:

    var success = 0;
    node.send([{ "payload": 1 }, null]);
    var timerId = setTimeout(function check() {
        success = flow.get("success");
        if (!success) { timerId = setTimeout(check, 50); } else {
            flow.set("success", 0);
            node.send([null, { "payload": 2 }]);
        }
    }, 50);

Works fine.
Thanks.

0 Likes

#7

Hello,

I wonder however if there is still a problem with Node Red and the 'while' and 'do while' functions. I have a node that parses (string, text) weather information I get as an ATOM feed (because Open Weather is never up to date for my area) and the following simple loop freezes Node Red every time (must open a CLI and stop/restart Node Red). I'm just trying to remove any sentences about wind speeds. BTW, just upgraded distro to stretch, happens same as w/ Jessie. NR is all up to date.

var endtag, cutout, content = msg.payload; // msg is the ATOM feed
while (content.indexOf("Wind" > -1))
{
endtag = content.indexOf(".", "Wind");
cutout = content.slice("Wind", endtag + 1);
content = content.replace(cutout, "");
}

Why does 'while' freeze NR with this?
TIA

0 Likes

#8

Insert node.warn statements in the loop to see what the values are and no doubt it will become clear.

0 Likes

#9

Do you have an example msg that can be fed into the function ?

0 Likes

#10

Hi,

there are a number of problem with your code that means it is not doing what you think it is doing and will be stuck in the while loop forever.

var endtag, cutout, content = msg.payload; // msg is the ATOM feed
while (content.indexOf("Wind" > -1))

Your condition in the while loop is content.indexOf("Wind" > -1) - you have the > -1 inside the argument passed to the indexOf function. It should be content.indexOf("Wind") > -1. That is why your while loop repeats forever.

{
endtag = content.indexOf(".", "Wind");

The indexOf function can take two arguments, but the second argument is expected to be the character position to start searching from. You are passing Wind as that second argument, so I'm not sure what result you'll get.

cutout = content.slice("Wind", endtag + 1);

The slice function also takes two arguments - the start position and the end position. Again, you are passing in the string Wind as the start position, rather than a number.

content = content.replace(cutout, "");
}

What exactly is the goal of the while loop? Is it to remove all instances of the word Wind from msg.payload? If so, you can do it without a while loop using:

msg.payload = msg.payload.replace(/Wind/g,"");
0 Likes

#11

Sometimes you spend so much time on something you get lost in it. This drove me nuts for some time, especially the lengthy effort for each NR / freeze reboot.

The objective is to remove all sentences that begin with "Wind", up to the next "." after "Wind". The reason for this is I have all output of my NR processes (weather updates, weather alerts, you left the garage door open for >2hrs, alarm system, solar alarm, etc..) result in a spoken message (Cepstral) over ceiling speakers throughout the house. Having "Wind" info included in the weather announcement when I construct 'today/ tonight/ tomorrow/ tomorrow night and next day' weather info was too much, too long and too annoying. Here is what I get now .....

"Good morning everyone. Here is the latest weather information. Today. Mainly cloudy with 60 percent chance of showers. High 10. For tonight. Mainly cloudy with 60 percent chance of showers this evening. Clearing near midnight. Low minus 2 with frost. Tomorrow. Mainly sunny. High 7. Tomorrow night: Clear. Low plus 4. For Friday. A mix of sun and cloud with 30 percent chance of showers. High 15. End of weather update."

You guys are awesome!. knolleary, thank you. :slight_smile:

0 Likes