steelbrain / steelbrain/node-ssh

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

Open
#459 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1k
Forks
94
PR merge metrics
No merged PRs in 30d

Description

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 :)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the TypeScript connect method and trace when this.connection is assigned relative to the SSH2 ready, error, end, and close events. Confirm the change by checking that isConnected() does not report a connection after an unsuccessful reconnect; the issue does not name a file or test to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
networking
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.