will prevent events from firing if autossh has already been killed

This commit is contained in:
samueleaton 2016-05-07 17:16:54 -06:00
parent 70d82c62ea
commit f05edd6969
2 changed files with 13 additions and 0 deletions

View file

@ -80,6 +80,7 @@ var AutoSSH = function (_EventEmitter) {
var port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort; var port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort;
_portfinder2.default.getPort({ port: port }, function (portfinderErr, freePort) { _portfinder2.default.getPort({ port: port }, function (portfinderErr, freePort) {
if (_this2.killed) return;
if (portfinderErr) _this2.emit('error', 'Port error: ' + portfinderErr); 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 { if (_this2.localPort !== 'auto' && _this2.localPort !== freePort) _this2.emit('error', 'Port ' + _this2.localPort + ' is not available');else {
_this2.localPort = freePort; _this2.localPort = freePort;
@ -120,6 +121,8 @@ var AutoSSH = function (_EventEmitter) {
value: function pollConnection() { value: function pollConnection() {
var _this4 = this; var _this4 = this;
if (this.killed) return;
if (this.maxPollCount && this.pollCount >= this.maxPollCount) { if (this.maxPollCount && this.pollCount >= this.maxPollCount) {
this.emit('error', 'Max poll count reached. Aborting...'); this.emit('error', 'Max poll count reached. Aborting...');
this.kill(); this.kill();
@ -248,6 +251,8 @@ var AutoSSH = function (_EventEmitter) {
this.execString = this.generateExecString(); this.execString = this.generateExecString();
this.currentProcess = (0, _child_process.exec)(this.execString, function (execErr, stdout, stderr) { this.currentProcess = (0, _child_process.exec)(this.execString, function (execErr, stdout, stderr) {
if (_this6.killed) return;
if (/Address already in use/i.test(stderr)) { if (/Address already in use/i.test(stderr)) {
_this6.kill(); _this6.kill();
_this6.emit('error', stderr); _this6.emit('error', stderr);

View file

@ -48,6 +48,8 @@ class AutoSSH extends EventEmitter {
const port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort; const port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort;
portfinder.getPort({ port }, (portfinderErr, freePort) => { portfinder.getPort({ port }, (portfinderErr, freePort) => {
if (this.killed)
return;
if (portfinderErr) if (portfinderErr)
this.emit('error', 'Port error: ' + portfinderErr); this.emit('error', 'Port error: ' + portfinderErr);
if (this.localPort !== 'auto' && this.localPort !== freePort) if (this.localPort !== 'auto' && this.localPort !== freePort)
@ -79,6 +81,9 @@ class AutoSSH extends EventEmitter {
/* starts polling the port to see if connection established /* starts polling the port to see if connection established
*/ */
pollConnection() { pollConnection() {
if (this.killed)
return;
if (this.maxPollCount && this.pollCount >= this.maxPollCount) { if (this.maxPollCount && this.pollCount >= this.maxPollCount) {
this.emit('error', 'Max poll count reached. Aborting...'); this.emit('error', 'Max poll count reached. Aborting...');
this.kill(); this.kill();
@ -203,6 +208,9 @@ class AutoSSH extends EventEmitter {
execTunnel(execTunnelCb) { execTunnel(execTunnelCb) {
this.execString = this.generateExecString(); this.execString = this.generateExecString();
this.currentProcess = exec(this.execString, (execErr, stdout, stderr) => { this.currentProcess = exec(this.execString, (execErr, stdout, stderr) => {
if (this.killed)
return;
if (/Address already in use/i.test(stderr)) { if (/Address already in use/i.test(stderr)) {
this.kill(); this.kill();
this.emit('error', stderr); this.emit('error', stderr);