Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeSSH Reconnect Issue: isConnected() Returns TRUE Even When Connection Unsuccessful #459

Open
kddige opened this issue Mar 18, 2023 · 0 comments

Comments

@kddige
Copy link

kddige commented Mar 18, 2023

I am encountering an issue which seems to be related to ISSUE-429, where the problem still persists. As im not aware of any pitfalls in the code, i have created a suggestion to resolve the issue.

Description:

The problem is caused by the assignment of the this.connection property before the connection promise resolves. Specifically, the this.connection property is being set before the connection.on('ready') event is triggered.

Proposed Solution:
To resolve this issue, I suggest moving the assignment of this.connection inside the connection.on('ready') event in the connect method:

public async connect(givenConfig: Config): Promise<this> {
  // ... (Rest of the code remains the same)

  const connection = new SSH2.Client();

  await new Promise<void>((resolve, reject) => {
    connection.on('error', reject);
    if (config.onKeyboardInteractive) {
      connection.on('keyboard-interactive', config.onKeyboardInteractive);
    }
    connection.on('ready', () => {
      connection.removeListener('error', reject);
      this.connection = connection; // Move this line inside the 'ready' event
      resolve();
    });
    connection.on('end', () => {
      if (this.connection === connection) {
        this.connection = null;
      }
    });
    connection.on('close', () => {
      if (this.connection === connection) {
        this.connection = null;
      }
      reject(new SSHError('No response from server', 'ETIMEDOUT'));
    });
    connection.connect(config);
  });

  return this;
}

Will i help?
Yeah, let us discuss and i can create a PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant