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

Seeing unhandled error and nodejs crash #562

Open
devgk882 opened this issue Nov 24, 2024 · 5 comments
Open

Seeing unhandled error and nodejs crash #562

devgk882 opened this issue Nov 24, 2024 · 5 comments

Comments

@devgk882
Copy link

devgk882 commented Nov 24, 2024

Hi, I am using the latest ssh2-sftp-client library

And this is what I am trying to do

const Client = require('ssh2-sftp-client');

function connectServer(config) {
    return new Promise((resolve, reject) => {
        const sftp = new Client();
        sftp.on('error', (err) => {
            console.error('SFTP connection error:', err.message);
            reject(err);
        });

        sftp.connect(
            config
        )
        .then(() => resolve(sftp))
        .catch((error) => reject(error));
    });
}

// Usage Example

    const config = {
        host: 'example.com',
        port: 22,
        username: 'yourUsername',
        password: 'yourPassword',
    };

    try {
        const sftpConnection = await connectServer(config);
        console.log('Connected successfully');

        // Perform operations with the SFTP connection
       await sftpConnection.get('/remote/path', '/local/path');

        // Close the connection
        await sftpConnection.end();
        console.log('Connection closed');
    } catch (error) {
        console.error('Error during SFTP connection:', error.message);
    }

I got uncaught error handler err with error readtimeout with CONNRESET message no response from server i saw the log SFTP connection error: which is from the .on event listener and Error during SFTP connection: from catch part and it crashed the nodejs application itself not sure what I should do here

And also before the code was like this:

const Client = require('ssh2-sftp-client');

function connectServer(config) {
    return new Promise((resolve, reject) => {
        const sftp = new Client();
      

        sftp.connect(
            config
        )
        .then(() => resolve(sftp))
        .catch((error) => reject(error));
    });
}

// Usage Example

    const config = {
        host: 'example.com',
        port: 22,
        username: 'yourUsername',
        password: 'yourPassword',
    };

    try {
        const sftpConnection = await connectServer(config);
        console.log('Connected successfully');

        // Perform operations with the SFTP connection
       await sftpConnection.get('/remote/path', '/local/path');

        // Close the connection
        await sftpConnection.end();
        console.log('Connection closed');
    } catch (error) {
        console.error('Error during SFTP connection:', error.message);
    }

and the ssh2-sftp-client version 9.x.x and during the connection reset i didnt saw crash and it just logged properly the error

@devgk882 devgk882 changed the title Seeing unhandled error and crash Seeing unhandled error and nodejs crash Nov 24, 2024
@theophilusx
Copy link
Owner

theophilusx commented Nov 24, 2024 via email

@devgk882
Copy link
Author

Thanks for the help

@devgk882
Copy link
Author

devgk882 commented Nov 30, 2024

I have another doubt @theophilusx it looks like for this below code

const Client = require('ssh2-sftp-client');

function connectServer(config) {
    return new Promise((resolve, reject) => {
        const sftp = new Client();
      

        sftp.connect(
            config
        )
        .then(() => resolve(sftp))
        .catch((error) => reject(error));
    });
}

// Usage Example

    const config = {
        host: 'example.com',
        port: 22,
        username: 'yourUsername',
        password: 'yourPassword',
    };

    try {
        const sftpConnection = await connectServer(config);
        console.log('Connected successfully');

        // Perform operations with the SFTP connection
       await sftpConnection.get('/remote/path', '/local/path');

        // Close the connection
        await sftpConnection.end();
        console.log('Connection closed');
    } catch (error) {
        console.error('Error during SFTP connection:', error.message);
    }

I am still seeing the hanging issue which causes the partial file download issue is that issue expected because i am using the connect in promise? but this happens randomly I thought this was fixed with latest release. Adding another question with this I am using ssh-rsa based auth and even the download speed is to slow for this can authentication can impact network speed i am reading the rsa key using fs.readFileSync

@devgk882
Copy link
Author

devgk882 commented Nov 30, 2024

@theophilusx also, I tried your example code but looks like for sftp.get() with passing stream we are seeing unhandled error event nodejs crash for 11.0.0 and nodejs v18.20.4 i downgraded the version to 9.1.0 and now i dont see the crash anymore. But sftp.fastget() seems to be working

async function main() {
  let sftp = new Client();

  try {
   
   
    await sftp.connect(config);
    const ws = createWriteStream('./large-file.txt');
    ws.on('error', (err) => {
      console.error(`ws error: ${err.message}`);
    });
    ws.on('close', () => {
      console.log('ws close event raiswed');
    });
    ws.on('end', () => {
      console.log('ws end event raised');
    });
    await sftp.get('/upload/large-file.txt', ws);
    console.log('File downloaded');
  } catch (err) {
    console.error(`main: Error ${err.message}`);
  } finally {
    await sftp.end();
  }
}

main();

This also throws Emitted 'error' event on ReadStream instance at:
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

when the file doesnt exist in server it just crash the node process

@devgk882
Copy link
Author

devgk882 commented Nov 30, 2024

I tried to switch from v11.0.0 to 9.1.0 and looks like unhandled error is gone now node version 18.20.4 and this unhandled error is seeing only for .get() not for fastGet()

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

2 participants