Lambda + ssh2-sftp-client throw uncatchable error "No response from server", when run in parallel

0
  1. This is my code: a. State machine: Perform "Download Lambda" in parallel to download multiple CSV file from SFTP server. Concurrency = 20 b. Download Lambda a. use ssh2-sftp-client library to connect b. this is my code of lambda:
try {
    await sftp.connect({
      host: serverInfo.host,
      port: serverInfo.port,
      username: serverInfo.username,
      privateKey: serverInfo.private_key,
    });
    //buffer size =10MB
    const readStream = sftp.createReadStream(fileFullPath, {
      highWaterMark: 10 * 1024 * 1024,
    });
    const rl = readline.createInterface({
      input: readStream,
      crlfDelay: Infinity,
    });
    for await (const line of rl) {
	//do something
    }
    rl.close();
    readStream.destroy();
  } catch (err) {
    console.error(`processing failed`);
    throw err;
  } finally {
    sftp.end();
  }
  1. I got below error, and I couldn't cacth it by catch()

1707216848757,"2024-02-06T10:54:08.757Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Inbound: CHANNEL_DATA (r:0, 6829) 1707216848758,"2024-02-06T10:54:08.758Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO SFTP: Inbound: Received DATA (id:2, 6816) 1707216848758,"2024-02-06T10:54:08.758Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Outbound: Sending CHANNEL_DATA (r:0, 29) 1707216848758,"2024-02-06T10:54:08.758Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO SFTP: Outbound: Buffered READ 1707216848801,"2024-02-06T10:54:08.801Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Inbound: CHANNEL_DATA (r:0, 32) 1707216848801,"2024-02-06T10:54:08.801Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO SFTP: Inbound: Received STATUS (id:3, 1, ""End of file"") 1707216848802,"2024-02-06T10:54:08.802Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Outbound: Sending CHANNEL_DATA (r:0, 17) 1707216848802,"2024-02-06T10:54:08.802Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO SFTP: Outbound: Buffered CLOSE 1707216848843,"2024-02-06T10:54:08.843Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Outbound: Sending DISCONNECT (11) 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Socket ended 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO CLIENT[sftp]: end endListener - ignoring handled error 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO CLIENT[sftp]: Global end event: Ignoring expected and handled event 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO Socket closed 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO CLIENT[sftp]: end: Connection closed 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO CLIENT[sftp]: end closeListener - ignoring handled error 1707216848852,"2024-02-06T10:54:08.852Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 INFO CLIENT[sftp]: Global close event: Ignoring expected and handled event 1707216848854,"2024-02-06T10:54:08.854Z fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 ERROR Uncaught Exception {""errorType"":""Error"",""errorMessage"":""No response from server"",""stack"":[""Error: No response from server"","" at cleanupRequests (/opt/nodejs/node_modules/ssh2/lib/protocol/SFTP.js:2730:15)"","" at SFTP.push (/opt/nodejs/node_modules/ssh2/lib/protocol/SFTP.js:191:7)"","" at onCHANNEL_CLOSE (/opt/nodejs/node_modules/ssh2/lib/utils.js:50:13)"","" at ChannelManager.cleanup (/opt/nodejs/node_modules/ssh2/lib/utils.js:200:7)"","" at Socket.<anonymous> (/opt/nodejs/node_modules/ssh2/lib/client.js:831:21)"","" at Socket.emit (node:events:517:28)"","" at TCP.<anonymous> (node:net:350:12)""]} 1707216848866,"Unknown application error occurred Runtime.Unknown 1707216848866,"END RequestId: fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 1707216848866,"REPORT RequestId: fc6cd4fc-a488-4da7-a570-1cb4802c8ef6 Duration: 819.19 ms Billed Duration: 820 ms Memory Size: 5120 MB Max Memory Used: 159 MB

  1. If I put sftp.end() in to timeout, it work normally. anyone can please help me to explain why error occur? And how to fix correctly?
Do Khoi
asked 2 months ago131 views
1 Answer
1
Accepted Answer

Based on the logs provided, it seems that the error occurs when attempting to clean up the SFTP requests. This suggests that the error might be happening when the client is trying to close the SFTP connection or perform other cleanup actions after completing the file transfer.

Here are a few steps you can take to diagnose and potentially fix the issue:

  1. Increase Timeout: Try increasing the timeout value for the SFTP connection to see if it helps in receiving a response from the server. You can adjust the timeout value in the options passed to the connect method.
2)Check Server Status: Ensure that the SFTP server is running and responsive. Check the server logs for any errors or warnings that might indicate issues with the server's operation.
  1. Network Connectivity: Verify that there are no network connectivity issues between your Lambda function and the SFTP server. You can use tools like ping or telnet to test the connectivity.
4)Server Load: Check the server's load and resource usage to see if it is overloaded or experiencing high traffic. If the server is overloaded, it may not be able to respond to requests in a timely manner.
5)Client Configuration: Double-check the client configuration to ensure that it is correctly configured to connect to the SFTP server. Verify the host, port, username, and private key values.

6) Error Handling: Implement more robust error handling in your code to handle cases where no response is received from the server. This could involve retrying the connection, logging the error for further investigation, or taking other appropriate actions.

  1. Library Updates: Ensure that you are using the latest version of the ssh2-sftp-client library, as newer versions may contain bug fixes or improvements that address issues like this.

By following these steps and carefully analyzing the logs and error messages, you should be able to diagnose and resolve the issue with your Lambda function's SFTP connection.

profile picture
answered 2 months ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions