Skip to content

Commit

Permalink
Close Session when closing SCPEngine or SFTPEngine (hierynomus#926)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen van Erp <[email protected]>
  • Loading branch information
evigeant and hierynomus authored Apr 15, 2024
1 parent 624fe83 commit 586a664
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -346,6 +347,7 @@ public void close()
throws IOException {
sub.close();
reader.interrupt();
session.close();
}

protected LoggerFactory getLoggerFactory() {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,7 @@ class SCPEngine {
private final SessionFactory host;
private final TransferListener listener;

private Session session;
private Command scp;
private int exitStatus;

Expand Down Expand Up @@ -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() {
Expand All @@ -102,6 +105,10 @@ void exit() {
log.warn("SCP exit signal: {}", scp.getExitSignal());
}
}
if(session != null) {
IOUtils.closeQuietly(session);
session = null;
}

scp = null;
}
Expand Down

0 comments on commit 586a664

Please sign in to comment.