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

Uncatchable error upon error-no-response #566

Open
cobasajaxinpho opened this issue Jan 24, 2025 · 5 comments
Open

Uncatchable error upon error-no-response #566

cobasajaxinpho opened this issue Jan 24, 2025 · 5 comments

Comments

@cobasajaxinpho
Copy link

cobasajaxinpho commented Jan 24, 2025

Hi @theophilusx ,

Just wonder. have you encounter this issues?
https://repost.aws/questions/QUsUpFCoYHRMuLd_yfi7tOEQ/lambda-ssh2-sftp-client-throw-uncatchable-error-no-response-from-server-when-run-in-parallel

I have been trying to figure out too. as try catch won't catch the error, will lead to crash the application

here's some snapshot of the code

public uploadFile = async (file: Express.Multer.File): Promise<boolean> => {
    const client = new Client();
    try {
      await client.connect(this.config);

      logger.info('[uploadFile]Read stream: ' + file.path);
      logger.info('[uploadFile]Write stream: ' + file.filename);

      const rs: Readable = client.createReadStream(file.path, {
        autoClose: true,
      });

      const ws: Writable = client.createWriteStream(file.filename, {
        autoClose: true,
      });
      rs.pipe(ws, { end: true });

      // on success of output file being saved
      ws.on('close', async () => {
        logger.error('ssh2 - WebSocket closed with code:');

        try {
          logger.error('ssh2 - Attempting to close SSH client...');
          setTimeout(async () => {
            await client.end();
          }, 1000);
        } catch (e) {
          logger.error('ssh2 - Error closing SSH client:', e);
        }

        logger.info('[uploadFile] Successfully saved file');
        return true;
      });

      ws.on('error', async (err) => {
        logger.error('ssh2 - error - uploadFile');
        try {
          logger.error('ssh2 - error - uploadFile1');
          setTimeout(async () => {
            await client.end();
          }, 1000);
        } catch (e) {
          logger.error('ssh2 - error - uploadFile2', e);
        }
        logger.error('[uploadFile] Failed to save file: ' + err);
        return false;
      });
    } catch (e) {
      logger.error('ssh2 - error - uploadFile2XXX', e);
    } finally {
      setTimeout(async () => {
        await client.end();
      }, 1000);
    }
    return true;
  };

Somehow need to

setTimeout(async () => {
            await client.end();
          }, 1000);

to prevent it from crashing the application

@cobasajaxinpho cobasajaxinpho changed the title Hi hello Uncatchable error upon error-no-response Jan 24, 2025
@theophilusx
Copy link
Owner

theophilusx commented Jan 24, 2025 via email

@cobasajaxinpho
Copy link
Author

cobasajaxinpho commented Jan 27, 2025

Ok. Thanks for clarifying.

public uploadFile = async (file: Express.Multer.File): Promise<boolean> => {
    const client = new Client();
    try {
      await client.connect(this.config);
      logger.info('[uploadFile] Uploading file: ' + file.filename);
      await client.put(file.path, file.filename);
      logger.info('[uploadFile] Successfully saved file: ' + file.filename);
      return true;
    } catch (error) {
      logger.error('ssh2 - error - uploadFile', error);
      return false;
    } finally {
      try {
        await client.end();
        logger.info('[uploadFile] SSH client closed');
      } catch (e) {
        logger.error('ssh2 - error closing SSH client:', e);
      }
    }
  };

got error
[error] ssh2 - error - uploadFile put: Bad path: documents/files/data/ not exist. eventhought i can see the documents uploaded.

any idea why the error happen?

tried "documents/files/data" also same error.

@theophilusx
Copy link
Owner

theophilusx commented Jan 27, 2025 via email

@cobasajaxinpho
Copy link
Author

cobasajaxinpho commented Jan 28, 2025

i have modified my code to use realPath , also result in the same error

public uploadFile = async (file: Express.Multer.File): Promise<boolean> => {
    const client = new Client();
    try {
      await client.connect(this.config);

      const realPath = await client.realPath(file.destination);
      logger.info('[uploadFile] client realPath: ' + realPath);
      logger.info('[uploadFile] Uploading file: ' + file.filename);

      await client.put(realPath, file.filename);

      logger.info('[uploadFile] Successfully saved file: ' + file.filename);
      return true;
    } catch (error) {
      logger.error('ssh2 - error - uploadFile', error);
      return false;
    } finally {
      try {
        await client.end();
        logger.info('[uploadFile] SSH client closed');
      } catch (e) {
        logger.error('ssh2 - error closing SSH client:', e);
      }
    }
  };

the different is real path value : "/documents/files/data"

@theophilusx
Copy link
Owner

There isn't sufficient information for me to tell what is goig on here. Please provide

  • version of ssh2-sftp-client your using
  • Version of nodejs this is running under
  • The exact error message
  • A debug log created by adding a debug logging function to the debug property in the connect config object (see the tests and example scripts for examples on using the debug property).

Some things to consider -

  • The first argument to put should include the target filename, not just the path i.e. realPath + / + filename
  • The second argument needs to be a complete path to the location of the file you want to upload, not just the filename
  • Your 'uploadFile' function should be called with awsait and wrapped inside a try/catch as well (perhaps it is, I cannot tell based on info provided).

Have you tested your uploadFile function in a simple script without any of the web stuff and outside any class or object? To get to the root cause here, it will first be necessary to strip everything down to absolute minimum, verify basic function and then build up bit by bit.

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