Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
How to prevent a infinite loop in nodejs child process spwan

I'm building an online ide by running the command to execute the code by spawn... but what should I do if there is an infinite loop in the code...The process is not being stopped

const { childElements } = require("dom-helpers");
const path = require("path");

const executeCpp = (filepath) => {
  const file = path.basename(filepath[1].split(".")[0]);

  const execute = new Promise((resolve, reject) => {
    const run = spawn(
      `cd ${filepath[0]} && g++ ${file}.cpp -o ${file}.out && ./${file}.out < ${file}.txt`,
      {
        shell: true,
      }
      // (error, stdout, stderr) => {
      //   error && reject({ error, stderr });
      //   stderr && reject(stderr);
      //   resolve(stdout);
      // }
    );
    setTimeout(() => {
      run.kill();
    }, 2000);

    run.on("exit", (code) => {
      // clearTimeout(to);
      resolve(`Process Exited with code ${code}`);
    });
    run.stderr.on("data", (data) => {
      resolve(String(data));
      console.log(data)
    });
    run.stdout.on("data", (data) => {
      resolve(data);
      console.log(data)
    });

  });

  return execute;
};
module.exports = {
  executeCpp,
};

This is my workaround, but it is not working. Also, when I use exec, I can reject the promise if there is an error, but it's not working; instead, I need to resolve the promise even if there is an error.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!