# Function node doesn't have timeout feature

**URL:** <https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483>\
**Category:** General\
**Tags:** function-node\
**Created:** [17 May 2023 06:56 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483 "2023-05-17T06:56:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![KilianHertel](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@KilianHertel](https://discourse.nodered.org/u/KilianHertel)\
**Post date:** [17 May 2023 06:56 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/1 "2023-05-17T06:56:57Z")

</div>

Hi,

the function node doesn't have a timeout feature but which is very well possible. Could i change the function node and make a pull request? Background is that if you create an endless loop in the function node then node-red doesn't respond and hard reset ist the only option.

I cannot believe that this is wanted behaviour.

Kind Regards

Kilian Hertel

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 May 2023 09:01 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/2 "2023-05-17T09:01:09Z")

</div>

Hi @KilianHertel

If an endless loop is introduced in a Function node that ties up the node.js event loop, how do you propose to time it out? The event loop is blocked so no timers will fire.

---

<div class="post-metadata">

**Author:** ![KilianHertel](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@KilianHertel](https://discourse.nodered.org/u/KilianHertel)\
**Post date:** [17 May 2023 09:16 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/3 "2023-05-17T09:16:26Z")

</div>

The vm library has a timeout attribute in runInNewContex =\> f.e. =\>  
const script = new vm.Script(skript);  
const context = {  
input: value,  
parent: parentsource  
};  
script.runInNewContext(context, {  
lineOffset: 0,  
displayErrors: true,  
timeout: 1000,  
});  
vm is what you use to run your function node.

---

<div class="post-metadata">

**Author:** ![KilianHertel](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@KilianHertel](https://discourse.nodered.org/u/KilianHertel)\
**Post date:** [17 May 2023 09:20 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/4 "2023-05-17T09:20:55Z")

</div>

You use this function:

[https://nodejs.org/api/vm.html#scriptrunincontextcontextifiedobject-options](https://nodejs.org/api/vm.html#scriptrunincontextcontextifiedobject-options)

ist has a timeout as well

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 May 2023 10:04 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/5 "2023-05-17T10:04:54Z")

</div>

You are quite right - completely forgot about the vm timeout option.

It would need to be configurable in the node; as it is perfectly valid to have a long running function node, as long is it hasn't tied up the event loop.

---

<div class="post-metadata">

**Author:** ![KilianHertel](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@KilianHertel](https://discourse.nodered.org/u/KilianHertel)\
**Post date:** [17 May 2023 10:11 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/6 "2023-05-17T10:11:11Z")

</div>

So should i create a pull request for this?

I did program this in an individual node, but this is more of a use case in a datamapper tool not a real function node.

I am new to contributing to node-red or open source in general.

If you leave the timeout on 0 the function node would have old behaviour.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [17 May 2023 10:49 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/7 "2023-05-17T10:49:06Z")

</div>

> [@KilianHertel](#):
>
> If you leave the timeout on 0

... or unset/blank of course.

Also note the note on that page - "Using the `timeout` or `breakOnSigint` options will result in new event loops and corresponding threads being started, which have a non-zero performance overhead."

Starting another thread may of course be a benefit in some circumstances... (and not in others)... but we need to ensure the it cleans up properly if there is a re-deploy (stop/start) while this new thread is running.

---

<div class="post-metadata">

**Author:** ![KilianHertel](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@KilianHertel](https://discourse.nodered.org/u/KilianHertel)\
**Post date:** [17 May 2023 11:26 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/8 "2023-05-17T11:26:54Z")

</div>

In my test scenario I just ctached the error, which is thrown after timeout. I don't think that the process would continue after timeout, because this is actually the purpose of it. The timeout is there to kill the process after some time.

If you restart the server the threading of nodejs should be broken or should it not?:

I did run this on my Windows Laptop:

```auto
var vm = require("vm");
var i = 0
while (i < 10000) {
    var skript = 'var i = 0; while(1==1){i++;i--;}';
    const script = new vm.Script(skript);
    const context = {};
    script.runInNewContext(context, {
        lineOffset: 0,
        displayErrors: true,
        timeout: 4294967295,
    });
    result = context.result;
    i++
}

```

It creates one blocked core. When i stop executing node, the core is free again, so no remaining threads i suppose.

Is this what you mean or didn't I understand that right?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [17 May 2023 12:28 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/9 "2023-05-17T12:28:53Z")

</div>

Yes - mostly. and if you do a redeploy (which doesn't fully stop nodes). I think it should be ok - but we don't want orphaned threads lying around leaking memory. I'd expect the GC to clean up anyway but just in case. Timeout option looks useful.

---

<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:** [16 July 2023 12:28 UTC](https://discourse.nodered.org/t/function-node-doesnt-have-timeout-feature/78483/10 "2023-07-16T12:28:59Z")

</div>

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