diff --git a/lib/spawn.js b/lib/spawn.js index 9e75b84..50e83f5 100644 --- a/lib/spawn.js +++ b/lib/spawn.js @@ -104,6 +104,14 @@ function spawn(phantomJsConfig) { prepareChildProcess(child); + child.stdin.on('error', function (err) { + // Ignore errors if the child has died. + // child.on('exit') will still be fired. + // @see https://github.com/peerigon/phridge/issues/34 + if (err.code === 'EPIPE') return; + if (err.code === 'ECONNRESET') return; + throw err; + }); child.stdout.on("data", onStdout); child.stderr.on("data", onStderr); @@ -156,4 +164,4 @@ function disposeChildProcess() { childProcess.cleanStdout = null; } -module.exports = spawn; \ No newline at end of file +module.exports = spawn; diff --git a/package.json b/package.json index 359fded..7e7ca97 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "when": "^3.4.2" }, "devDependencies": { - "chai": "^1.9.1", + "chai": "^2.3.0", "chai-as-promised": "^4.1.1", "getport": "^0.1.0", "istanbul": "^0.3.0", diff --git a/test/Phantom.test.js b/test/Phantom.test.js index b595854..44314b3 100644 --- a/test/Phantom.test.js +++ b/test/Phantom.test.js @@ -404,7 +404,7 @@ describe("Phantom", function () { }); }); - it("should be save to call .dispose() multiple times", slow(function () { + it("should be safe to call .dispose() multiple times", slow(function () { return when.all([ phantom.dispose(), phantom.dispose(), @@ -412,8 +412,10 @@ describe("Phantom", function () { ]); })); + it("should be safe to call .dispose() after the process has died", slow(function () { + phantom.childProcess.kill('SIGTERM'); + return phantom.dispose(); + })); }); - }); - -}); \ No newline at end of file +}); diff --git a/test/disposeAll.test.js b/test/disposeAll.test.js index 90d293d..bb3ec08 100644 --- a/test/disposeAll.test.js +++ b/test/disposeAll.test.js @@ -32,4 +32,29 @@ describe("disposeAll()", function () { }); })); -}); \ No newline at end of file + it("should ignore EPIPE errors", slow(function () { + var exited = []; + + return when.all([ + phridge.spawn(), + phridge.spawn(), + phridge.spawn() + ]) + .then(function (p) { + p[0].childProcess.kill('SIGINT'); + return p; + }) + .then(function (p) { + p[0].childProcess.on("exit", function () { exited.push(0); }); + p[1].childProcess.on("exit", function () { exited.push(1); }); + p[2].childProcess.on("exit", function () { exited.push(2); }); + + return phridge.disposeAll(); + }) + .then(function () { + exited.sort(); + expect(exited).to.eql([0, 1, 2]); + }); + })); + +});