ssh2-sftp-client icon indicating copy to clipboard operation
ssh2-sftp-client copied to clipboard

Issue Regards Memory Leak

Open abhishekglb opened this issue 1 year ago • 2 comments

Hi Theophilus,

As we talked about before, I am also sharing a more detailed description of the package & nodejs version.

I have multiple users who each configure their own SFTP in my dashboard. When a user attempts to upload a new file, I establish a new SFTP connection for that user based on the credentials they have configured.

For example, if a user with user_id 1 has a server at 192.168.10.10 and a similar user with user_id 2 has a server at 192.168.10.20 when user 1 uploads a new file, I create a new SFTP connection for that user. This can be implemented as follows:

let sftpConnectionObj = {};

sftpConnectionObj[user_id || 1] = new SFTP();
sftpConnectionObj[user_id || 2] = new SFTP();

After, based on the user ID, I check for the existence of a directory, create it if necessary, upload the file, close the connection, and remove it from the object.

sftpConnectionObj[user_id || 1 ].mkdir(remoteDir, true);

The versions of the packages and Node.js that I am using are:

  • ssh2-sftp-client: "^9.1.0"
  • Node.js: v14.17.6

abhishekglb avatar Mar 04 '24 13:03 abhishekglb

I have multiple users who each configure their own SFTP in my dashboard. When a user attempts to upload a new file, I establish a new SFTP connection for that user based on the credentials they have configured.

Are yuou generating the sftp client object just before you create the conneciton with connect() or are you creating the object at app initi8laisation time and just using connect/end on the same object multiple times?

The key thing to note is that the count used for max listeners is done per emitter, which means per sftp object. In your case, per user. So, the number of users you have at the same time should not alter this because each user has their own sftp object and each object has its own emitter.

This means the max count must be getting reach in a specific user sftp object. Two common ways this could happen is if you are re-using the sftp object for multiple connections, especially if some connections are not closed correctly or there is some concurrency conflict where your app is recieving multiple requests for the same user at the same time and tying to process them using the same sftp object.

Point is, your issue is unlikely to be due to multiple different users at the same time. Therefore, focus testing on single user and multiple requests etc.

The versions of the packages and Node.js that I am using are:

  • ssh2-sftp-client: "^9.1.0"
  • Node.js: v14.17.6

That node version is not supported. Node v14 has been out of maintenance for nearly two and half years! Even if I was to add the changes you requested, it wouldn't be supported on that version of node.

You also need to be using node v10.0.3. There are known issues in v9.1.0 (though these are primarily with the donwloadDir/uploadDir methods).

theophilusx avatar Mar 04 '24 21:03 theophilusx

The string argument passed in when calling new to create the sftp object is completely optional. It really just helps with debugging by giving each object a specific name in the logs.

In your case, it may be useful to add a unique id value for each user, especially if you have unified logging where all logs go to the same location. THis will let you trace which user each log record belongs to, so may help with diagnostic issues.

theophilusx avatar Mar 04 '24 21:03 theophilusx