mscdex / mscdex/ssh2

Missing callback when SFTP server disconnects during sftp operation

Open
#1,175 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
5.8k
Forks
734
PR merge metrics
No merged PRs in 30d

Description

I have a problem with, maybe, cleanupRequests. Unfortunately I cannot get hold on how it works in the end.

Sometimes, mostly for network reasons, or because it reboots for maintenance, an SFTP server, based on SSH2, "disappears". It could be simulated by

session.on('sftp', (accept, _reject) => {
  const sftpStream = accept()
 ...
  sftpStream.on('READDIR', async (reqId, aPath) => {
     process.exit(0)
...

A client uses ssh2-promises and sometimes the promises are not rejected by disconnection, but stay pending. I tried to debug the situation and I found, that in

function cleanupRequests(sftp) {
  let keys = Object.keys(sftp._requests);
  if (keys.length === 0)
    return;

  let reqs = sftp._requests;
  sftp._requests = {};
  const err = new Error('No response from server');
  for (let i = 0; i < keys.length; ++i) {
    const req = reqs[keys[i]];
    if (typeof req.cb === 'function')
      req.cb(err);
  }
}

the callback I need is added to sftp._requests while another request is processed through req.cb(err);. WIth other words, if cleanupRequests would be executed twice the callback is called correctly and the promises are rejected.

This may sound strange - any idea why this could happen? May this be a problem in ssh2 or ssh2-promise? I couln't reproduce it so far in simple app with ssh2 alone.

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 by tracing cleanupRequests, sftp._requests, and the req.cb(err) callback path during an SFTP disconnect. Reproduce the server-disappearance scenario described in the issue and determine why requests added while cleanupRequests is running remain pending; done means pending callbacks or promises are rejected reliably without duplicate handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.