From 129d659628b08ccad668a7ba6f558e7028ebebb5 Mon Sep 17 00:00:00 2001 From: Martin Hoecker Date: Wed, 1 Nov 2023 23:15:54 +0100 Subject: [PATCH] Count the number of jumps to break out of infinite loops. This is especially useful for the bru cli, to make sure that test-runners that are accidentally stuck in an infinite loop still terminate in a reasonable amount of time and don't hog up resources. --- packages/bruno-cli/src/commands/run.js | 6 ++++++ packages/bruno-electron/src/ipc/network/index.js | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/bruno-cli/src/commands/run.js b/packages/bruno-cli/src/commands/run.js index 51bb35704..5e7b8e554 100644 --- a/packages/bruno-cli/src/commands/run.js +++ b/packages/bruno-cli/src/commands/run.js @@ -356,6 +356,7 @@ const handler = async function (argv) { } let currentRequestIndex = 0; + let nJumps = 0; // count the number of jumps to avoid infinite loops while (currentRequestIndex < bruJsons.length) { const iter = bruJsons[currentRequestIndex]; const { bruFilepath, bruJson } = iter; @@ -372,6 +373,11 @@ const handler = async function (argv) { const nextRequestName = result?.nextRequestName; if (nextRequestName) { + nJumps++; + if (nJumps > 10000) { + console.error(chalk.red(`Too many jumps, possible infinite loop`)); + process.exit(1); + } const nextRequestIdx = bruJsons.findIndex((iter) => iter.bruJson.name === nextRequestName); if (nextRequestIdx >= 0) { currentRequestIndex = nextRequestIdx; diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 00ce034e8..3a6170abe 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -673,9 +673,10 @@ const registerNetworkIpc = (mainWindow) => { } let currentRequestIndex = 0; + let nJumps = 0; // count the number of jumps to avoid infinite loops while (currentRequestIndex < folderRequests.length) { - item = folderRequests[currentRequestIndex]; - let nextRequestName = undefined; + const item = folderRequests[currentRequestIndex]; + let nextRequestName; const itemUid = item.uid; const eventData = { collectionUid, @@ -866,6 +867,10 @@ const registerNetworkIpc = (mainWindow) => { }); } if (nextRequestName) { + nJumps++; + if (nJumps > 10000) { + throw new Error('Too many jumps, possible infinite loop'); + } const nextRequestIdx = folderRequests.findIndex((request) => request.name === nextRequestName); if (nextRequestIdx >= 0) { currentRequestIndex = nextRequestIdx;