kajtzu / ganymed-ssh-2

Automatically exported from code.google.com/p/ganymed-ssh-2
Other
0 stars 0 forks source link

Sessions created by SCPClient can remain open #46

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Ganymed release: 2.6.2
Client platform: Java 7

What steps will reproduce the problem?

The ganymed ssh library version 2.6.2 is used as part of our product for 
establishing SSH connections. While testing, sometimes the following exception 
occurred when the library was used for file transfer:

---
java.io.IOException: Remote scp sent illegal error code.
    at ch.ethz.ssh2.SCPClient.readResponse(SCPClient.java:89)
    at ch.ethz.ssh2.SCPOutputStream.<init>(SCPOutputStream.java:32)
    at ch.ethz.ssh2.SCPClient.put(SCPClient.java:203)

---

This reason for the exception boiled down to a problem with the credential 
used. However, the exception did produce a side effect of the session remaining 
open.

Explanation:
Within the SCPClient.put() function, a Session was created to be passed to the 
SCPOutputStream. The only time this Session would ever subsequently get closed 
is when SCPOutputStream.close() was called. However since the SCPOutputStream's 
constructor threw an IOException the created Session is never closed.

Here is the problematic code:

---
public SCPOutputStream put(final String remoteFile, long length, String 
remoteTargetDirectory, String mode) throws IOException {
...
    sess = conn.openSession();
    sess.execCommand(cmd, charsetName);

    return new SCPOutputStream(this, sess, remoteFile, length, mode);
}

---

Repeated execution attempts and IOExceptions caused many Session instances to 
remain open and never get closed. The same problem can occur for the 
SCPClient.get() call.

Attached is a proposed patch for the SCPClient, which ensures that sessions are 
closed if there is a problem either executing the scp command or creating the 
SCP IO stream.

Original issue reported on code.google.com by derek.pi...@gmail.com on 29 Jul 2014 at 8:43

Attachments: