theophilusx / ssh2-sftp-client

a client for SSH2 SFTP
Apache License 2.0
797 stars 195 forks source link

Help! Weird behavior calling end() #526

Closed ahm0 closed 1 month ago

ahm0 commented 3 months ago

Hi all!

First of all...this is more a request for help than an issue.

Client OS: AmazonLinux:2 SFTP Server OS: Windows (something) Versions:

Description: in brief, SOMETIMES my code get stuck when it try to disconnect from sftp server. This is very weird because, sometimes it works fine, disconnects and finish, but sometimes it get stuck. It doesn't throw errors and I can't define a common pattern, so I don't know where or what to debug.

Sample of code


const client = new SshSftpClient();
client.on("error", (err: Error) => {
      console.debug(`sftp connection error: ${err}`);
});
client.on("close", () => {
      console.debug("sftp connection closed");
});
client.on("end", () => {
      console.debug("sftp connection end");
});
try {
   await client.connect({<common_params_for_connection>});
   // some operations, like put
   await client.end();
}
catch(error) {
    // handle error
}

The logs for when it works ok are like this:

SFTP: Inbound: Received STATUS (id:191, 0, ""Success"")
Outbound: Sending DISCONNECT (11) 
Socket ended 
CLIENT[sftp]: end endListener - ignoring handled error 
CLIENT[sftp]: Global end event: Ignoring expected and handled event 
Socket closed 
CLIENT[sftp]: end: Connection closed 
CLIENT[sftp]: end closeListener - ignoring handled error 
CLIENT[sftp]: Global close event: Ignoring expected and handled event 

And when it get stuck are just this:

SFTP: Inbound: Received STATUS (id:74, 0, ""Success"")
Outbound: Sending DISCONNECT (11) 

I know that probably the information is not much, but I don't have any additional info to add.

So any ideas? Something that I can try/change?

TIA

theophilusx commented 3 months ago

You need to update to v10.0.3.

The version you are running had known problems with handling unexpected close of connection by remote server. This was fixed in later versions.

ahm0 commented 3 months ago

Hi @theophilusx , and thanks for your response. I'll try updating the version, but just in case, is there any other way to force close the connection to the server, but end() method? Thanks

theophilusx commented 3 months ago

No, the only way to end a connencttion is by calling end().

ahm0 commented 3 months ago

Ok. Thank you again. I'll give a try to the new version and let you know about results.

Best Regards