sanketbajoria / ssh2-promise

ssh with promise/async await and typescript support
https://www.npmjs.com/package/ssh2-promise
MIT License
148 stars 24 forks source link

ssh "connect" event is not fired when connected after close() #23

Open jeffrson opened 5 years ago

jeffrson commented 5 years ago

Here is a short test:

let sshconfig = {
  host: '192.168.1.1',
  port: 22,
  username: 'test',
  password: 'test',
  reconnect: false
}

let ssh = new SSH2Promise(sshconfig)
ssh.on('ssh', (...args) => console.log(args[0]))

let sftp = ssh.sftp()

sftp.readdir('/home/test')
  .then((data) => console.dir('data'))
  .then(() => ssh.close())
  .then(() => ssh.connect())
  .then(() => sftp.readdir('/home/test'))
  .then((data) => console.dir('next try'))

it results in

beforeconnect connect connect 'data' beforedisconnect disconnect beforedisconnect 'next try'

As you can see, there's no 'connect' after close. Furthermore, two 'connect' on the first try seem strange as well as the second 'beforedisconnect' after 'disconnect'.

jeffrson commented 5 years ago

BTW, I believe this is somehow related to 'ssh.close()' not really closing when called after connected the second time.

That's why I hesitate to report this fact as a new bug. What do you think?

jeffrson commented 5 years ago

There is a problem when closing the connection - the array deregister is not cleared (or otherwise modified) upon close.

Then, in getSSHConnection, the newly created sshConnection is not registered at all, because isRegistered thinks, the connection had already been registered, because the "unique id" has not changed.

@sanketbajoria Would it be possible to fix it on short-term? I see three alternative solutions:

  1. clear the array in SSH2Promise.close()
  2. remove the element in disconnectCb
  3. forcibly re-register in getSSHConnection, as soon as a new sshConnection is created.

This doesn't fix the strange sequence of "beforedisconnect", though.

sanketbajoria commented 5 years ago

@jeffrson Thanks for reporting this. I will look into this. If you do find a solution. Please feel free to raise a PR

oscarrobinson commented 5 years ago

Is a fix for this coming? I've observed lots of weird behaviour around reconnects that seems to be related to the issues described here.

dobby-tgc commented 3 years ago

Any updates yet?