From 59e42e4b09f430dcdb39890e6a710a6151819441 Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Sat, 10 Oct 2015 15:48:39 +0200 Subject: [PATCH] Fix cannot read property 'stdin' of null in some cases Add check for phantom._isDisposed before scheduling a new ping. This prevents a new ping from being scheduled when a pong message is received after phantom.dispose() has been called. --- lib/Phantom.js | 6 ++++++ test/Phantom.test.js | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/Phantom.js b/lib/Phantom.js index 030793e..f1eb75d 100644 --- a/lib/Phantom.js +++ b/lib/Phantom.js @@ -306,6 +306,12 @@ Phantom.prototype._schedulePing = function () { // There is already a ping scheduled. It's unnecessary to schedule another one. return; } + if (this._isDisposed) { + // No need to schedule a ping, this instance is about to be disposed. + // Catches rare edge cases where a pong message is received right after the instance has been disposed. + // @see https://github.com/peerigon/phridge/issues/41 + return; + } this._pingTimeoutId = setTimeout(this._write, pingInterval, { action: "ping" }); }; diff --git a/test/Phantom.test.js b/test/Phantom.test.js index 06993a7..cd4f69e 100644 --- a/test/Phantom.test.js +++ b/test/Phantom.test.js @@ -458,6 +458,17 @@ describe("Phantom", function () { }); }); + // @see https://github.com/peerigon/phridge/issues/41 + it("should not schedule a new ping when a pong message is received right after calling dispose()", function () { + var message = JSON.stringify({ status: "pong" }); + var promise = phantom.dispose(); + + // Simulate a pong message from PhantomJS + phantom._receive(message); + + return promise; + }); + }); });