From f05edd6969bd7cb4d30d130dadb90f1f710557ce Mon Sep 17 00:00:00 2001 From: samueleaton Date: Sat, 7 May 2016 17:16:54 -0600 Subject: [PATCH] will prevent events from firing if autossh has already been killed --- index.js | 5 +++++ src/index.js | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/index.js b/index.js index b48bf0c..b5a3132 100644 --- a/index.js +++ b/index.js @@ -80,6 +80,7 @@ var AutoSSH = function (_EventEmitter) { var port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort; _portfinder2.default.getPort({ port: port }, function (portfinderErr, freePort) { + if (_this2.killed) return; if (portfinderErr) _this2.emit('error', 'Port error: ' + portfinderErr); if (_this2.localPort !== 'auto' && _this2.localPort !== freePort) _this2.emit('error', 'Port ' + _this2.localPort + ' is not available');else { _this2.localPort = freePort; @@ -120,6 +121,8 @@ var AutoSSH = function (_EventEmitter) { value: function pollConnection() { var _this4 = this; + if (this.killed) return; + if (this.maxPollCount && this.pollCount >= this.maxPollCount) { this.emit('error', 'Max poll count reached. Aborting...'); this.kill(); @@ -248,6 +251,8 @@ var AutoSSH = function (_EventEmitter) { this.execString = this.generateExecString(); this.currentProcess = (0, _child_process.exec)(this.execString, function (execErr, stdout, stderr) { + if (_this6.killed) return; + if (/Address already in use/i.test(stderr)) { _this6.kill(); _this6.emit('error', stderr); diff --git a/src/index.js b/src/index.js index d426404..859cdbe 100644 --- a/src/index.js +++ b/src/index.js @@ -48,6 +48,8 @@ class AutoSSH extends EventEmitter { const port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort; portfinder.getPort({ port }, (portfinderErr, freePort) => { + if (this.killed) + return; if (portfinderErr) this.emit('error', 'Port error: ' + portfinderErr); if (this.localPort !== 'auto' && this.localPort !== freePort) @@ -79,6 +81,9 @@ class AutoSSH extends EventEmitter { /* starts polling the port to see if connection established */ pollConnection() { + if (this.killed) + return; + if (this.maxPollCount && this.pollCount >= this.maxPollCount) { this.emit('error', 'Max poll count reached. Aborting...'); this.kill(); @@ -203,6 +208,9 @@ class AutoSSH extends EventEmitter { execTunnel(execTunnelCb) { this.execString = this.generateExecString(); this.currentProcess = exec(this.execString, (execErr, stdout, stderr) => { + if (this.killed) + return; + if (/Address already in use/i.test(stderr)) { this.kill(); this.emit('error', stderr);