Kill entire process tree of a child when the timeout is reached. Fixes #232

This commit is contained in:
Matt Godbolt 2017-01-11 15:53:12 -06:00
parent 0ee3cf29c1
commit 25f49f9a80
2 changed files with 11 additions and 3 deletions

View File

@ -31,6 +31,7 @@ var child_process = require('child_process'),
utils = require('./utils'),
quote = require('shell-quote'),
_ = require('underscore-node'),
treeKill = require('tree-kill'),
logger = require('./logger').logger;
function Compile(compiler, env) {
@ -265,12 +266,17 @@ Compile.prototype.exec = function (command, args, options) {
env: env,
detached: process.platform == 'linux'
});
var running = true;
function kill() {
if (running) treeKill(child.pid);
}
var stderr = "";
var stdout = "";
var timeout;
if (timeoutMs) timeout = setTimeout(function () {
logger.warn("Timeout for", command, args, "after", timeoutMs, "ms");
okToCache = false;
child.kill();
kill();
stderr += "\nKilled - processing time exceeded";
}, timeoutMs);
var truncated = false;
@ -279,7 +285,7 @@ Compile.prototype.exec = function (command, args, options) {
if (stdout.length > maxOutput) {
stdout += "\n[Truncated]";
truncated = true;
child.kill();
kill();
return;
}
stdout += data;
@ -289,7 +295,7 @@ Compile.prototype.exec = function (command, args, options) {
if (stderr.length > maxOutput) {
stderr += "\n[Truncated]";
truncated = true;
child.kill();
kill();
return;
}
stderr += data;
@ -297,6 +303,7 @@ Compile.prototype.exec = function (command, args, options) {
child.on('exit', function (code) {
logger.debug({type: 'exited', code: code});
if (timeout !== undefined) clearTimeout(timeout);
running = false;
});
return new Promise(function (resolve, reject) {
child.on('error', function (e) {

View File

@ -30,6 +30,7 @@
"serve-favicon": "2.3.x",
"serve-static": "1.10.x",
"temp": "0.8.x",
"tree-kill": "1.1.x",
"underscore-node": "*",
"shell-quote": "1.6.x",
"winston": "2.2.x"