SFTPClient.close() causing parent SSHClient to disconnect
- Dominant language
- Java
- Stars
- 2.7k
- Forks
- 620
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 11
Description
I have a situation where I am trying to abort an active SFTP file transfer by calling SFTPClient.close() in another thread. I'm seeing that this is also killing the SSHClient connection that was used to create the SFTPClient. So questions are: (1) is this the expected behavior, and (2) is this the correct approach for aborting an active SFTP file transfer?
I'm using sshj 0.19.0.
Here is a modified SFTPDownload class that illustrates the problem:
`package net.schmizz.sshj.examples;
// mvn exec:java -Dexec.mainClass="net.schmizz.sshj.examples.SFTPDownload" -Dexec.args="useranme abcdabcd999999"
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.FileAttributes;
import net.schmizz.sshj.sftp.RemoteResourceFilter;
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.xfer.FileSystemFile;
import java.io.IOException;
import java.util.List;
/** This example demonstrates downloading of a file over SFTP from the SSH server. */
public class SFTPDownload {
public static void main(String[] args)
throws Exception
{
Thread thread = null;
try {
String userName = args[0];
String password = args[1];
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("someremotehost");
try {
ssh.authPassword(userName, password);
final SFTPClient sftp = ssh.newSFTPClient();
try {
thread = new Thread("sftp get thread"){
public void run() {
try {
System.out.println("(1) start transfer");
sftp.get(
"/home/kermit/giga_4.txt", // 4GB test file on remote host
"giga_4.txt");
}
catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();
}
finally {
try {
System.out.println("(2) sleep 2 sec");
Thread.currentThread().sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("(3) ssh.isConnected = " + ssh.isConnected());
//
// Close SFTP connection while transfer is active in other thread.
///
sftp.close();
System.out.println("(4) ssh.isConnected = " + ssh.isConnected());
try {
System.out.println("(5) sleep 2 sec");
Thread.currentThread().sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("(6) ssh.isConnected = " + ssh.isConnected());
thread.join();
}
}
finally {
ssh.disconnect();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
`
When run this, I am seeing the following output:
`(2) sleep 2 sec
(1) start transfer
(3) ssh.isConnected = true
[WARNING]
java.lang.ArrayIndexOutOfBoundsException: -5
at net.schmizz.sshj.common.Buffer.putUInt32(Buffer.java:315)
at net.schmizz.sshj.transport.Encoder.encode(Encoder.java:96)
at net.schmizz.sshj.transport.TransportImpl.write(TransportImpl.java:438)
at net.schmizz.sshj.connection.channel.ChannelOutputStream$DataBuffer.flush(ChannelOutputStream.java:107)
at net.schmizz.sshj.connection.channel.ChannelOutputStream$DataBuffer.flush(ChannelOutputStream.java:74)
at net.schmizz.sshj.connection.channel.ChannelOutputStream.flush(ChannelOutputStream.java:188)
at net.schmizz.sshj.sftp.SFTPEngine.transmit(SFTPEngine.java:283)
at net.schmizz.sshj.sftp.SFTPEngine.request(SFTPEngine.java:128)
at net.schmizz.sshj.sftp.RemoteResource.close(RemoteResource.java:54)
at net.schmizz.sshj.sftp.SFTPFileTransfer$Downloader.downloadFile(SFTPFileTransfer.java:152)
at net.schmizz.sshj.sftp.SFTPFileTransfer$Downloader.download(SFTPFileTransfer.java:107)
at net.schmizz.sshj.sftp.SFTPFileTransfer$Downloader.access$300(SFTPFileTransfer.java:92)
at net.schmizz.sshj.sftp.SFTPFileTransfer.download(SFTPFileTransfer.java:73)
at net.schmizz.sshj.sftp.SFTPFileTransfer.download(SFTPFileTransfer.java:59)
at net.schmizz.sshj.sftp.SFTPClient.get(SFTPClient.java:230)
at net.schmizz.sshj.examples.SFTPDownload$1.run(SFTPDownload.java:37)
(4) ssh.isConnected = true
(5) sleep 2 sec
(6) ssh.isConnected = false
`
Thanks,
--Neil
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.