Ssh connection reset crash

I have a network monitoring module, which opens large number of ssh connections, about 200 or so. This module crashes the application at times (note that this does not happen all the time) with following error,

events.js:183
throw er; // Unhandled ‘error’ event
^

Error: read ECONNRESET
at _errnoException (util.js:992:11)
at TCP.onread (net.js:618:25)

Iam using the npm package node-ssh.

Is it possible to catch this error? Simple try/catch block doesn’t seem to work. People have asked this question in forums, but I did find specific to ssh. Any help would be appreciated.

A quick look at the node-ssh package shows that:

Node-SSH is an extremely lightweight Promise wrapper for ssh2.

Which means that to catch errors you will need to add a .catch( handler to the returned promise, instead of try/catch.

try/catch will only work with promises if you are awaiting the promise in the try block.

If this doesn’t help, you will need to post some code where the methods are called so we can look up the correct error handling pattern

2 Likes

Yes Iam awaiting the promise in try/catch block. Here is my code,

async getSshConnection() {

const options = {
  host: this.ip,
  username: 'admin',
  readyTimeout: this.sshConnectionTimeout,
};

let sshConn;
const sshConnection = new NodeSsh();
for (let i = 0; i < this.passwords.length; i += 1) {
  options.password = this.passwords[i];
  try {
    sshConn = await sshConnection.connect(options);
    break;
  } catch (error) {
    sshConnection.dispose();
  }
}
if (sshConn) {
  return sshConn;
}
throw (new Error('ssh failed for ' + this.ip));

}

I have 3 different passwords for ssh. I try each of them and if ssh connection is successful I return. Do you see some issue with this code?

This code works, except I get uncaught error, ECONNRESET once in 2-3 days and the application restarts. Iam not sure ECONNRESET is in this code block or later when I use sshConn object to execute terminal commands again via await sshConn.execCommand. Thanks.