ssh2
ssh2 copied to clipboard
end() is broken
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)
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.
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.