# Monitor Function Node

**URL:** <https://discourse.nodered.org/t/monitor-function-node/84714>\
**Category:** General\
**Created:** [19 January 2024 09:09 UTC](https://discourse.nodered.org/t/monitor-function-node/84714 "2024-01-19T09:09:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dunken](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dunken](https://discourse.nodered.org/u/Dunken)\
**Post date:** [19 January 2024 09:09 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/1 "2024-01-19T09:09:09Z")

</div>

I happens time and again that someone adds code in a Function Node which is error prone. The Node might work as expected in normale cases but if for example an input is not as expected the Node ends up in an endless loop (e.g. consuming all CPU).

At best I would like to monitor behavior like this (certain percentage of CPU usage or certain amount of time spent in any Node or ...) and in case any threshold is exceeded the watchdog should abort the run (and maybe even provide an error message why the run was aborted).

Any best practice how to do this?

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [19 January 2024 09:16 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/2 "2024-01-19T09:16:35Z")

</div>

I would say learn to use guards - check if an element exists, the type is it correct, avoid infinite loops with predefined iteration limits...

---

<div class="post-metadata">

**Author:** ![Dunken](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dunken](https://discourse.nodered.org/u/Dunken)\
**Post date:** [19 January 2024 09:25 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/3 "2024-01-19T09:25:52Z")

</div>

Well, the code is not written by me... let's assume the code is written by a customer and I simply want to protect the system (Node-RED) that in case something goes wrong, it does so gracefully.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [19 January 2024 09:30 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/4 "2024-01-19T09:30:27Z")

</div>

I can suggest in this case to use `link call` with a timeout - this will trigger an error that you can catch with the `catch` node

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 January 2024 09:40 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/5 "2024-01-19T09:40:29Z")

</div>

As of Node-RED v3.1.x the function node has a timeout option that _should_ kill a runaway loop

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0dc7cef47390da1bacf1f06cf2b46bb32fe4564c.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c48ee6e82990873291d7edb3deca2243e7a0032f.png)

> [@Dunken](#):
>
> Any best practice

Dont do loops in function nodes - use the `split` and `join` nodes.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 January 2024 10:12 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/6 "2024-01-19T10:12:34Z")

</div>

> [@Dunken](#):
>
> happens time and again that someone adds code in a Function Node

Have procedures in your organisation that require a code review before changes are committed to the mainline of your code version control system.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 January 2024 10:13 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/7 "2024-01-19T10:13:32Z")

</div>

> [@Steve-Mcl](#):
>
> As of Node-RED v3.1.x the function node has a timeout option that _should_ kill a runaway loop

Does that start a separate thread that watches for a timeout?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 January 2024 10:26 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/8 "2024-01-19T10:26:53Z")

</div>

> [@Colin](#):
>
> Does that start a separate thread that watches for a timeout

No, it uses the node vm `timeout` and `breakOnSigint` options: [adding timeout attribute to function node by k1ln · Pull Request #4177 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/4177/files#diff-24cd715c3b7405ea194bfdc0dc2a350ceb2f5d18696b8163c3e40105b981a666R399-R402)

> - `timeout` Specifies the number of milliseconds to execute `code` before terminating execution. If execution is terminated, an [`Error`](https://nodejs.org/api/errors.html#class-error) will be thrown. This value must be a strictly positive integer.
> - `breakOnSigint` If `true`, receiving `SIGINT` (Ctrl+C) will terminate execution and throw an [`Error`](https://nodejs.org/api/errors.html#class-error). Existing handlers for the event that have been attached via `process.on('SIGINT')` are disabled during script execution, but continue to work after that. **Default:** `false`.

---

<div class="post-metadata">

**Author:** ![Dunken](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dunken](https://discourse.nodered.org/u/Dunken)\
**Post date:** [19 January 2024 10:30 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/9 "2024-01-19T10:30:21Z")

</div>

> [@Colin](#):
>
> Have procedures in your organisation that require a code review before changes are committed to the mainline of your code version control system.

We offer Node-RED as an internal service and don't want to review "custom" code... the problem is that customers simply don't realize that it's because if their code and they simply complain that Node-RED is using all the resources...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 January 2024 10:41 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/10 "2024-01-19T10:41:23Z")

</div>

On top of the suggestion i made:

> [@Steve-Mcl](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0dc7cef47390da1bacf1f06cf2b46bb32fe4564c.png)
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c48ee6e82990873291d7edb3deca2243e7a0032f.png)
> 
> > [@Dunken](#):
> >
> > Any best practice
> 
> Dont do loops in function nodes

You can also set a default for the function timeout `functionTimeout` in the Node-RED settings.js file

PR: [Adding function timeout to settings file (#4265) by knolleary · Pull Request #4309 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/4309/files#diff-2d41b29f642358bd960d10dfb88f1a87c14a95da148dc68cf69cf7920739f177R472-R473)

which means any function node added by the user will get a default timeout value. I would suggest 5 seconds is MORE than big enough.

---

<div class="post-metadata">

**Author:** ![Dunken](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dunken](https://discourse.nodered.org/u/Dunken)\
**Post date:** [19 January 2024 10:43 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/11 "2024-01-19T10:43:28Z")

</div>

Many thanks. I will definitively look into this. Is this setting also released with 3.1 or was this available even before?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 January 2024 10:46 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/12 "2024-01-19T10:46:32Z")

</div>

> [@Dunken](#):
>
> Is this setting also released with 3.1 or was this available even before?

Was introduced in 3.1.0: [release notes](https://github.com/node-red/node-red/releases/tag/3.1.0)

---

<div class="post-metadata">

**Author:** ![Dunken](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dunken](https://discourse.nodered.org/u/Dunken)\
**Post date:** [19 January 2024 12:05 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/13 "2024-01-19T12:05:41Z")

</div>

> [@Steve-Mcl](#):
>
> Dont do loops in function nodes - use the `split` and `join` nodes.

Though I'm going to tackle the current problem with the timeout setting (many thanks!) I would like to understand your recommendation to not use loops in Function Nodes... Can you elaborate why this is supposed to be an anti-pattern? At first sight `split` and `join` Nodes would seem like overhead (= slow down single flow execution)... is it mainly to make sure we don't do anything blocking on the event loop?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 January 2024 12:48 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/14 "2024-01-19T12:48:40Z")

</div>

> [@Dunken](#):
>
> is it mainly to make sure we don't do anything blocking on the event loop?

Exactly that.

While you may consider there is a cost to split/join, they permit the event loop to at least get a shout (unlike a hard loop in a function)

And, it is anti pattern as you say.

* * *

To be fair, if a user knows what they are doing and programs defensively with guards and limits, then there is less reason to avoid loops in a function node (small fixed size or limited iterations are not a major issue) but then this topic would not have started (and the timeout feature would not have been added) if we all programmed defensively.

One good reason to avoid function node loops is that blocking the eventloop will affect schedule/timer nodes. They WILL be blocked and WILL NOT execute on time (cron nodes, timed inject nodes etc).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 February 2024 12:48 UTC](https://discourse.nodered.org/t/monitor-function-node/84714/15 "2024-02-02T12:48:54Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
