diff --git a/src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java b/src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java index ecac5afcc..eeac5d8ad 100644 --- a/src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java +++ b/src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java @@ -48,6 +48,7 @@ public class SFTPEngine protected final PathHelper pathHelper; + private final Session session; protected final Session.Subsystem sub; protected final PacketReader reader; protected final OutputStream out; @@ -63,7 +64,7 @@ public SFTPEngine(SessionFactory ssh) public SFTPEngine(SessionFactory ssh, String pathSep) throws SSHException { - Session session = ssh.startSession(); + session = ssh.startSession(); loggerFactory = session.getLoggerFactory(); log = loggerFactory.getLogger(getClass()); sub = session.startSubsystem("sftp"); @@ -346,6 +347,7 @@ public void close() throws IOException { sub.close(); reader.interrupt(); + session.close(); } protected LoggerFactory getLoggerFactory() { diff --git a/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java b/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java index 821a1c864..a3a175c46 100644 --- a/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java +++ b/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java @@ -19,6 +19,7 @@ import net.schmizz.sshj.common.LoggerFactory; import net.schmizz.sshj.common.SSHException; import net.schmizz.sshj.common.StreamCopier; +import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.connection.channel.direct.Session.Command; import net.schmizz.sshj.connection.channel.direct.SessionFactory; import net.schmizz.sshj.xfer.TransferListener; @@ -41,6 +42,7 @@ class SCPEngine { private final SessionFactory host; private final TransferListener listener; + private Session session; private Command scp; private int exitStatus; @@ -82,7 +84,8 @@ void cleanSlate() { void execSCPWith(ScpCommandLine commandLine) throws SSHException { - scp = host.startSession().exec(commandLine.toCommandLine()); + session = host.startSession(); + scp = session.exec(commandLine.toCommandLine()); } void exit() { @@ -102,6 +105,10 @@ void exit() { log.warn("SCP exit signal: {}", scp.getExitSignal()); } } + if(session != null) { + IOUtils.closeQuietly(session); + session = null; + } scp = null; }