question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ssh2-sftp-client crashes Node process with no error and exit code 0

See original GitHub issue

I have an issue which looks exactly like this one https://github.com/theophilusx/ssh2-sftp-client/issues/49

I’m using:

  • Node.js 12
  • ssh2-sftp-client 4.1.0

Here is a minimal reproducible code

const Client = require('ssh2-sftp-client')
const inspect = require('util').inspect

const sftpClient = new Client();

sftpClient.on('error', (err) => {
  console.log('SFTP client emitted ERROR: ' + inspect(err))
})
sftpClient.on('end', () => {
  console.log('SFTP client emitted END')
})
sftpClient.on('finish', () => {
  console.log('SFTP client emitted FINISH')
})
sftpClient.on('close', () => {
  console.log('SFTP client emitted CLOSE')
})

const options = {
  host: '',
  port: '22',
  username: '',
  password: ''
};

// below infinite loop is to simulate large number of 
// connections in my production code
(async () => {
  while (true) {
    try {
      await new Promise(resolve => setTimeout(() => resolve(sftpClient.connect(options)), 1000))
    } catch {
      console.log('boom')
    }
  }
})()

process.on('exit', code => console.log(`Catched!. Exit code: ${code}`))

process.on('uncaughtException', (err, origin) => {
  console.error('Unhandled exception. Please handle!', err.stack || err)
  console.error(`Origin: ${JSON.stringify(origin)}`)
})

process.on('unhandledRejection', (err, promise) => {
  console.error('Unhandled promise rejection. Please handle!', promise, err.stack || err)
})

process.on('warning', (warning) => {
  console.warn(warning.name)
  console.warn(warning.message)
  console.warn(warning.stack)
})

process.on('rejectionHandled', (promise) => {
  console.log('rejectionHandled event triggered')
})

As you can see, it shouldn’t escape the infinite loop unless there is an Error. And it in fact does not for some iterations but eventually it escapes (usually after <10 iterations). And the only log I see is

SFTP client emitted END
Catched!. Exit code: 0

On the SFTP server side I have the following limitations in the sshd_config file

MaxSessions 1
MaxStartups 1

I don’t know how is it possible to exit with code 0 without any error trace without actually calling process.exit(0). This makes investigation even harder, because I don’t know what to look for.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
theophilusxcommented, Sep 23, 2019

The workaround is now in the main branch of the repository. I will probably release an updated version in the next day or so. In the meantime, you can use the repository code directly.

The basic workaround was to add a listener for the end event which will reject the connect promise if an end event is triggered during the connection process. See the repository for details.

Lam Tran notifications@github.com writes:

@theophilusx can you share your workaround for this issue? I’m also experiencing it and it’s quite critical for us. Thank you.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

– Tim Cross

0reactions
lojzatrancommented, Sep 23, 2019

@theophilusx can you share your workaround for this issue? I’m also experiencing it and it’s quite critical for us. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Node.js silently crashes without any information?
ssh2-sftp-client 4.1.0. In brief, question: How can Node crash without any error events/stacktraces and exit code 0? Update #1.
Read more >
ssh2-sftp-client - Bountysource
100% reproducible bug that results in node process crash when using promises (bug in promise reject) $ 0. Created 18 days ago in...
Read more >
Ssh2-sftp-client NPM - npm.io
If the remote file did not exist, the method would return an error, but failed to close any passed in stream supplied as...
Read more >
SFTP node crashes server, the other one is not working
We are using node-red to transfer files to another server via sftp. ... code=exited, status=1/FAILURE nodered.service: Failed with result 'exit-code'.
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Run more than one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found