This code should write a file, run it in a spawn process, and then kill the spawn process if the timeout is reached. However, anytime it triggers the timeout, I get an [ERR_HTTP_HEADERS_SENT] error.
$.get('/path', function(req, res){
// data = contents of a file
fs.writeFile(filename, data, (err) => {
const child_process = spawn(//run file, {detached: true});
const wait = setTimeout(function() {
try {
process.kill(-runner.pid);
return res.send("Timeout Message");
}
}, 3000);
runner.on('exit', (code, signal) => {
clearTimeout(wait);
fs.unlink(filename, (err) => {
if (code === 0) return res.send("Good");
else return res.send('Fail');
});
});
runner.stdout.on('data', function(data) {
console.log('stdout: ' + data);
});
runner.stderr.on('data', function(data) {
console.log('stderr: ' + data);
});
runner.on('error', (err) => {
clearTimeout(wait);
fs.unlink(filename, (error) => {
return res.send("Error");
});
});
});
}
Your help is appreciated!