diff --git a/examples/kill_mem.js b/examples/kill_mem.js new file mode 100644 index 0000000..c9d765c --- /dev/null +++ b/examples/kill_mem.js @@ -0,0 +1,26 @@ +var + sys = require("sys"), + jefe = new require("../lib/jefe"), + elJefe = new jefe.Jefe(), + scriptName = "script that creates too much memory should be killed"; + +var sourceCode = "A = []; while (true) A.push('a')"; + +elJefe.compile(scriptName, sourceCode, { maxMem: 10240, maxTime: 0 } ); // KB + +sys.error("this will kill the script for using too much memory regardless of how long it takes ..."); + +elJefe.run(scriptName, {}, function (error, sandboxIn, sandboxOut) { + if (error) { + sys.puts("error = " + error); + } else { + sys.puts("completed without error"); + sys.puts("sandboxOut = " + JSON.stringify(sandboxOut)); + } + + var stats = elJefe.getScriptStats(scriptName); + sys.p(stats); + + process.exit(0); +}); + diff --git a/examples/while_true.js b/examples/while_true.js index b6ab279..afbf773 100644 --- a/examples/while_true.js +++ b/examples/while_true.js @@ -6,7 +6,7 @@ var elJefe.compile(scriptName, "while (true) {}", { maxTime: 5000 } ); -sys.error("this will wait up to 5s ..."); +sys.error("this will wait up to 5s before killing the script ..."); elJefe.run(scriptName, {}, function (error, sandboxIn, sandboxOut) { if (error) { diff --git a/lib/jefe.js b/lib/jefe.js index 8631b26..45bd41d 100644 --- a/lib/jefe.js +++ b/lib/jefe.js @@ -229,12 +229,27 @@ Jefe.prototype._runRequestOnChild = function (request, childHandler) { }; Jefe.prototype._watchMem = function (request, childHandler, initialMem, initialMemPercent) { - var self = this; + var + self = this, + script = request.script; function checkMem() { childHandler.peakMemoryUsage(function (err, mem, memPercent) { if ((script.maxMem > 0 && mem - initialMem > script.maxMem) || (script.maxMemPercent > 0 && memPercent - initialMemPercent > script.maxMemPercent)) { + + if (common.debugMode) { + sys.debug("pid " + childHandler.pid + + " mem overage! maxMem=" + script.maxMem + + " initialMem=" + initialMem + + " currPeak=" + mem + + " deltaMem=" + (mem - initialMem) + + " maxMemPercent=" + script.maxMemPercent + + " initialMemPercent=" + initialMemPercent + + " currPeakPercent=" + memPercent + + " deltaPercent=" + (memPercent - initialMemPercent)); + } + childHandler.kill(); self.scripts[script.name].wasKilled("memory"); request.callback(exports.ERR_TOO_MUCH_MEMORY, null); diff --git a/lib/memory.js b/lib/memory.js index d751898..f4e30d4 100644 --- a/lib/memory.js +++ b/lib/memory.js @@ -1,3 +1,5 @@ +var fs = require('fs'); + if (process.platform.match(/linux/i)) { /** @@ -28,7 +30,8 @@ if (process.platform.match(/linux/i)) { var match = contents.match(/^VmHWM:\s+(\d+)\s+kB$/m), peakRSS = match ? parseInt(match[1], 10) : 0, - peakRSSPercent = (peakRSS > 0 && systemMemoryTotal > 0) ? peakRSS / systemMemoryTotal : 0; + peakRSSPercent = (peakRSS > 0 && exports.systemMemoryTotal > 0) + ? peakRSS / exports.systemMemoryTotal : 0; callback(null, peakRSS, peakRSSPercent); } diff --git a/lib/userscript.js b/lib/userscript.js index ef48e1c..b532421 100644 --- a/lib/userscript.js +++ b/lib/userscript.js @@ -28,7 +28,7 @@ function UserScript(name, sourceCode, options) { if (this.maxMemPercent < 0 || this.maxMemPercent > 1) throw new Error("maxMemPercent"); - this.maxTime = Math.max(0, opts.maxTime || 250); + this.maxTime = Math.max(0, opts.maxTime === undefined ? 250 : opts.maxTime); this.name = name || ''; this.sourceCode = sourceCode;