added default timeout to function node (#1)

* added default timeout to function node

* added unit test to support defaultFunctionTimeout
This commit is contained in:
Vasu Vanka
2024-12-10 01:41:54 +05:30
committed by GitHub
parent bb01f26f06
commit 477e81256e
3 changed files with 98 additions and 5 deletions

View File

@@ -14,6 +14,8 @@
* limitations under the License.
**/
const { log } = require("console");
module.exports = function(RED) {
"use strict";
@@ -399,6 +401,8 @@ module.exports = function(RED) {
if(node.timeout>0){
finOpt.timeout = node.timeout;
finOpt.breakOnSigint = true;
} else if(RED.settings.defaultFunctionTimeout > 0){
finOpt.timeout = RED.settings.defaultFunctionTimeout * 1000
}
}
var promise = Promise.resolve();
@@ -415,8 +419,15 @@ module.exports = function(RED) {
var opts = {};
if (node.timeout>0){
opts = node.timeoutOptions;
} else if(RED.settings.defaultFunctionTimeout > 0){
opts.timeout = RED.settings.defaultFunctionTimeout * 1000
}
try {
node.script.runInContext(context,opts);
} catch (err) {
node.error(err);
return done(err);
}
node.script.runInContext(context,opts);
context.results.then(function(results) {
sendResults(node,send,msg._msgid,results,false);
if (handleNodeDoneCall) {

View File

@@ -473,6 +473,7 @@ module.exports = {
* - fileWorkingDirectory
* - functionGlobalContext
* - functionExternalModules
* - defaultFunctionTimeout
* - functionTimeout
* - nodeMessageBufferMaxLength
* - ui (for use with Node-RED Dashboard)
@@ -499,8 +500,30 @@ module.exports = {
/** Allow the Function node to load additional npm modules directly */
functionExternalModules: true,
/**
* Default function timeout (in seconds) for the Function node.
* A value of 0 indicates no timeout is applied, meaning the function can run indefinitely.
*
* The default function timeout is designed to prevent blocking code in function nodes,
* which could otherwise lead to a stalled or unresponsive main thread. For example,
* the following code would block the event loop indefinitely:
*
* `while(1) {}`
*
* By specifying a `defaultFunctionTimeout`, such scenarios can be mitigated,
* ensuring that long-running or infinite loops are terminated automatically after
* the specified timeout duration.
*
* Note: If both `defaultFunctionTimeout` and `functionTimeout` are defined in the
* settings file, `functionTimeout` takes precedence, providing a more granular
* control for individual function nodes.
*/
defaultFunctionTimeout: 5,
/** Default timeout, in seconds, for the Function node. 0 means no timeout is applied */
functionTimeout: 0,
functionTimeout: 2,
/** The following property can be used to set predefined values in Global Context.
* This allows extra node modules to be made available with in Function node.