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; + }); + }); });