ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

end() is broken

Open mcmuffindk opened this issue 8 years ago • 2 comments

I don't think .end() is working as it should. If you create a connection and then close it with end() and repeat this 10 times you will end up getting a connection error.

Reproduce:

var Client = require('ssh2').Client;

var conn = new Client();

function fun() {
  conn.on('ready', function() {
    console.log('Client :: ready');
    conn.sftp(function(err, sftp) {
      if (err) throw err;
      sftp.readdir('/test', function(err, list) {
        if (err) throw err;
        console.dir(list);
        conn.end();
        fun();
      });
    });
  }).connect({
    host: '165.227.137.7',
    port: 22,
    username: 'root',
    password: 'ca1608ti'
  });
}
fun();

The output i get:

Error: (SSH) Channel open failure: open failed
    at SSH2Stream.onFailure (\path\to\node_modules\ssh2\lib\client.js:1203:13)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at SSH2Stream.emit (events.js:211:7)
    at parsePacket (\path\to\node_modules\ssh2-streams\lib\ssh.js:3709:10)
    at SSH2Stream._transform (\path\to\node_modules\ssh2-streams\lib\ssh.js:671:13)
    at SSH2Stream.Transform._read (_stream_transform.js:186:10)
    at SSH2Stream._read (\path\to\node_modules\ssh2-streams\lib\ssh.js:253:15)
    at SSH2Stream.Transform._write (_stream_transform.js:174:12)
    at doWrite (_stream_writable.js:397:12)

mcmuffindk avatar Jul 21 '18 14:07 mcmuffindk

You should really just create a new instance for each new connection. Even without the error, you'd currently be continually adding more and more 'ready' event handlers. This is another reason to not re-use connection objects.

mscdex avatar Jul 21 '18 14:07 mscdex

Changing: conn.on('ready', function() { to: conn.once('ready', function() { should resolve this issue -- it's an EventEmitter subscription leak, not an issue in ssh2.

ddurschlag6river avatar Aug 01 '18 11:08 ddurschlag6river