Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Session when closing SCPEngine or SFTPEngine #926

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading