'Kill() doesn't kill the child process in node.js

const kill = require('tree-kill')
.
.
.
function killWorkload() {
    Logger.log("within kill");
    if (global.currentChild) {
        Logger.log(global.currentChild.pid);
    try {
        kill(global.currentChild.pid, 'SIGKILL', (error) => {
        Logger.log(error);
        });
    }
    catch (e) {
        Logger.error("Unable to Kill"+ e);
    }
  }
  Logger.log("Child killed");
}

process.on('SIGINT', (exitCode) => {
    killWorkload();
    Logger.log("Aborted");
});
.
.
.
global.currentChild.on('exit', async(exitCode) => {
      global.currentChild = "";
      //Non normal exit, Something went wrong
      if (exitCode !== 0) {
        await Logger.logMsg(exitCode, resultPath);  //Writes the exitCode to Logger.txt
        process.exit();
      }
      //Normal exit
      else {
        process.exit();
      }
}

global.currentchild is a spawned 'child-process' which is a cpp executable.

within kill
12380
Child killed
Aborted
Error: Command failed: taskkill /pid 12380 /T /F
ERROR: The process "12380" not found.

    at ChildProcess.exithandler (child_process.js:390:12)
    at ChildProcess.emit (events.js:400:28)
    at maybeClose (internal/child_process.js:1055:16)
    at Socket.<anonymous> (internal/child_process.js:441:11)
    at Socket.emit (events.js:400:28)
    at Pipe.<anonymous> (net.js:675:12) {
  killed: false,
  code: 128,
  signal: null,
  cmd: 'taskkill /pid 12380 /T /F'
}

this is the result that I get. Kill() takes time to execute. Even tried putting the kill block within a new Promise() with await which also doesn't work.

The flow that I want is to raise a sigint and it should kill the childprocess and in turn the global.currentChild.on('exit') must execute and call process.exit() within.

Used this line just for debugging

Logger.log("Aborted");

Could someone help me with this, what Am I doing wrong here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source