Skip to content

Commit 586a664

Browse files
evigeanthierynomus
andauthored
Close Session when closing SCPEngine or SFTPEngine (#926)
Co-authored-by: Jeroen van Erp <[email protected]>
1 parent 624fe83 commit 586a664

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class SFTPEngine
4848

4949
protected final PathHelper pathHelper;
5050

51+
private final Session session;
5152
protected final Session.Subsystem sub;
5253
protected final PacketReader reader;
5354
protected final OutputStream out;
@@ -63,7 +64,7 @@ public SFTPEngine(SessionFactory ssh)
6364

6465
public SFTPEngine(SessionFactory ssh, String pathSep)
6566
throws SSHException {
66-
Session session = ssh.startSession();
67+
session = ssh.startSession();
6768
loggerFactory = session.getLoggerFactory();
6869
log = loggerFactory.getLogger(getClass());
6970
sub = session.startSubsystem("sftp");
@@ -346,6 +347,7 @@ public void close()
346347
throws IOException {
347348
sub.close();
348349
reader.interrupt();
350+
session.close();
349351
}
350352

351353
protected LoggerFactory getLoggerFactory() {

src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.schmizz.sshj.common.LoggerFactory;
2020
import net.schmizz.sshj.common.SSHException;
2121
import net.schmizz.sshj.common.StreamCopier;
22+
import net.schmizz.sshj.connection.channel.direct.Session;
2223
import net.schmizz.sshj.connection.channel.direct.Session.Command;
2324
import net.schmizz.sshj.connection.channel.direct.SessionFactory;
2425
import net.schmizz.sshj.xfer.TransferListener;
@@ -41,6 +42,7 @@ class SCPEngine {
4142
private final SessionFactory host;
4243
private final TransferListener listener;
4344

45+
private Session session;
4446
private Command scp;
4547
private int exitStatus;
4648

@@ -82,7 +84,8 @@ void cleanSlate() {
8284

8385
void execSCPWith(ScpCommandLine commandLine)
8486
throws SSHException {
85-
scp = host.startSession().exec(commandLine.toCommandLine());
87+
session = host.startSession();
88+
scp = session.exec(commandLine.toCommandLine());
8689
}
8790

8891
void exit() {
@@ -102,6 +105,10 @@ void exit() {
102105
log.warn("SCP exit signal: {}", scp.getExitSignal());
103106
}
104107
}
108+
if(session != null) {
109+
IOUtils.closeQuietly(session);
110+
session = null;
111+
}
105112

106113
scp = null;
107114
}

0 commit comments

Comments
 (0)