0

I have a auto-created JS file. And my module call it, some time the auto-created JS file cause infinite loop. And i will call thousand of auto-created JS file, some will run correct but some will cause infinite loop and I will don't know about file content
==> I wonder how I can handle this ?
Example of auto-created JS file. It name "excute.js"

const excuted = (a,b) => {
   let i = 0;
   while(a<b){
     i++;
   }
   console.log(i);
}

module.exports = excuted();

Example My module ==> i want handle infinite loop error here

const {exec} = require("child_process");

const check = () => {
  exec('node excute.js', (error, stdout, stderr) => {
    if(error) {
        console.log(error.message);
        return;
    }
    console.log(stdout);
  })
}

check();

I'm thinking about use setTimeout inside my module file or auto-created JS file. But i really get stuck and confused !!!. Thank every one

  • 1
    `excuted();` <-- so where is a and b? Why are you executing an export? Is that just for an example? – epascarello Sep 29 '21 at 17:37
  • Just wait for some time and then kill the process if it's not done?! – ASDFGerte Sep 29 '21 at 17:38
  • 1
    where inside `executed` do you change the values of `a` and `b`? If you don't, then if `a – Krokodil Sep 29 '21 at 17:40
  • https://stackoverflow.com/questions/56016550/node-js-cannot-kill-process-executed-with-child-process-exec/56016815 – epascarello Sep 29 '21 at 17:41
  • @ASDFGerte that may not always be a solution because the process may take more time than expected, or if it doesn't it will at least be very inefficient. If you can, try and forecast whether an infinite loop will be encountered. – Krokodil Sep 29 '21 at 17:41
  • @AlphaHowl https://en.wikipedia.org/wiki/Halting_problem – ASDFGerte Sep 29 '21 at 17:42
  • 1
    @ASDFGerte That's talking about a general algorithm. In this case, we can just check that a) both inputs are numbers. and b) both inputs are not equal. Done. – Rojo Sep 29 '21 at 17:50
  • @ASDFGerte I know about that, but this is a different case; also note I said **if you can**. In this simplified case with the condition involving `a` and `b`, we can easily forecast an infinite loop. In a more realistic context, we may have an object containing instructions, informing us at which location in the object to go to next. We can skim past the object and check for infinite loops rather easily, before we start following its intructions. If you want an example, I'll write one, just let me know :) – Krokodil Sep 29 '21 at 17:51
  • If you want to spend the time writing an algorithm reasoning about simple snippets, which will be ridiculously time intensive, go ahead. I don't see the big benefit, as anyone wanting to waste your computation time can almost surely engineer one which it can't detect in moments. Which use case are you working for? – ASDFGerte Sep 29 '21 at 18:02
  • @ASDFGerte It does so in `5.00ms` the first time, then averages out:`2ms`. I just rushed though, and this `5 to 2`ms can surely be lowered if I took the time required. As I said earlier, I *"skim" through the object, and any extensive traversing which may take very much time will come later if my skim-check returns false. So I partially agree with the answer given below, in that you cannot detect an infinite loop without having the properties (eg a and b, and the conditions which are used) used in that loop. But, I still think a timeout is not the best method. See my below continuation. – Krokodil Sep 29 '21 at 18:20
  • It is better to look for tell-tale signs that an infinite loop will or is occuring. This is better because as I said earlier, the process may take more time as the computer may be slow due to overheating etc. So there may not necessarily be an infinite loop. Moreover, it will take less time and therefore the thread (or whatever chain of funcitons should occur next) will be inactive and unresponsive for less time. It is better to look for tell-tale signs that an infinite loop will or is occuring. – Krokodil Sep 29 '21 at 18:23

1 Answers1

0

It is impossible to deterministically detect an infinite loop within another process. In your case you may simply want to timeout the process:

const {exec} = require("child_process");

async function check(fileName, timeOut = 1000) {
  return new Promise((resolve, reject) => {
    let _t = null;
    // Execute
    const process = exec(`node ${fileName}`, (error, stdout, stderr) => {
      if (_t) clearTimeout(_t);
      if (error) reject(error);
      else resolve(stdout);
    })
    // Time Out
    _t = setTimeout(() => {
      process.kill(-1);
      reject(undefined);
    }, timeOut);
  })
}

check()
  .then(stdout => console.log(stdout))
  .catch(console.error);

Side Note

Pay attention to exec() as it is executing code on your server. If the code contains errors (as in your case) nothing assures you those errors are not fatal for your system. Furthermore never execute untrusted code like that.

Newbie
  • 4,462
  • 11
  • 23
  • Thank you so much !!! I know that. But in my modules, i have to call thousands file like that each hour. And I can't update the content of the created file. That Why i want to handle it like that. I'm just a beginner. So do you have any solution. Thank you again ! You saved my life :3 – Đình Bình Sep 30 '21 at 08:06
  • @ĐìnhBình The proposed one does not work? I mean timing out the process is not a solution? – Newbie Sep 30 '21 at 10:11
  • It work so good. I mean that whether there is another solution ? – Đình Bình Oct 01 '21 at 11:57
  • You can not determine in a finite amount of time if a program will halt. https://en.m.wikipedia.org/wiki/Halting_problem – Newbie Oct 03 '21 at 12:56