Close Session when closing SCPEngine or SFTPEngine (#926)

Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
Eric Vigeant
2024-04-15 14:31:54 -04:00
committed by GitHub
parent 624fe839cb
commit 586a66420e
2 changed files with 11 additions and 2 deletions

View File

@@ -48,6 +48,7 @@ public class SFTPEngine
protected final PathHelper pathHelper; protected final PathHelper pathHelper;
private final Session session;
protected final Session.Subsystem sub; protected final Session.Subsystem sub;
protected final PacketReader reader; protected final PacketReader reader;
protected final OutputStream out; protected final OutputStream out;
@@ -63,7 +64,7 @@ public class SFTPEngine
public SFTPEngine(SessionFactory ssh, String pathSep) public SFTPEngine(SessionFactory ssh, String pathSep)
throws SSHException { throws SSHException {
Session session = ssh.startSession(); session = ssh.startSession();
loggerFactory = session.getLoggerFactory(); loggerFactory = session.getLoggerFactory();
log = loggerFactory.getLogger(getClass()); log = loggerFactory.getLogger(getClass());
sub = session.startSubsystem("sftp"); sub = session.startSubsystem("sftp");
@@ -346,6 +347,7 @@ public class SFTPEngine
throws IOException { throws IOException {
sub.close(); sub.close();
reader.interrupt(); reader.interrupt();
session.close();
} }
protected LoggerFactory getLoggerFactory() { protected LoggerFactory getLoggerFactory() {

View File

@@ -19,6 +19,7 @@ import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.LoggerFactory; import net.schmizz.sshj.common.LoggerFactory;
import net.schmizz.sshj.common.SSHException; import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.common.StreamCopier; 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.Session.Command;
import net.schmizz.sshj.connection.channel.direct.SessionFactory; import net.schmizz.sshj.connection.channel.direct.SessionFactory;
import net.schmizz.sshj.xfer.TransferListener; import net.schmizz.sshj.xfer.TransferListener;
@@ -41,6 +42,7 @@ class SCPEngine {
private final SessionFactory host; private final SessionFactory host;
private final TransferListener listener; private final TransferListener listener;
private Session session;
private Command scp; private Command scp;
private int exitStatus; private int exitStatus;
@@ -82,7 +84,8 @@ class SCPEngine {
void execSCPWith(ScpCommandLine commandLine) void execSCPWith(ScpCommandLine commandLine)
throws SSHException { throws SSHException {
scp = host.startSession().exec(commandLine.toCommandLine()); session = host.startSession();
scp = session.exec(commandLine.toCommandLine());
} }
void exit() { void exit() {
@@ -102,6 +105,10 @@ class SCPEngine {
log.warn("SCP exit signal: {}", scp.getExitSignal()); log.warn("SCP exit signal: {}", scp.getExitSignal());
} }
} }
if(session != null) {
IOUtils.closeQuietly(session);
session = null;
}
scp = null; scp = null;
} }