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

[Bug] Close SSH session after remote shell finish #15348

Merged
merged 2 commits into from
Dec 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.config.keys.loader.KeyPairResourceLoader;
import org.apache.sshd.common.session.SessionHeartbeatController;
import org.apache.sshd.common.util.security.SecurityUtils;

import java.security.KeyPair;
import java.time.Duration;
import java.util.Collection;

public class SSHUtils {
Expand Down Expand Up @@ -57,6 +59,7 @@ public static ClientSession getSession(SshClient client, SSHConnectionParam conn
throw new Exception("Failed to add public key identity", e);
}
}
session.setSessionHeartbeat(SessionHeartbeatController.HeartbeatType.RESERVED, Duration.ofSeconds(3));
return session;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@
import java.util.HashMap;
import java.util.Map;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RemoteExecutor {
public class RemoteExecutor implements AutoCloseable {

static final String REMOTE_SHELL_HOME = "/tmp/dolphinscheduler-remote-shell-%s/";
static final String STATUS_TAG_MESSAGE = "DOLPHINSCHEDULER-REMOTE-SHELL-TASK-STATUS-";
static final int TRACK_INTERVAL = 5000;

protected Map<String, String> taskOutputParams = new HashMap<>();

SshClient sshClient;
ClientSession session;
SSHConnectionParam sshConnectionParam;
private SshClient sshClient;
private ClientSession session;
private SSHConnectionParam sshConnectionParam;

public RemoteExecutor(SSHConnectionParam sshConnectionParam) {

Expand Down Expand Up @@ -205,6 +206,18 @@ private String getRemoteShellHome() {
return String.format(REMOTE_SHELL_HOME, sshConnectionParam.getUser());
}

@SneakyThrows
@Override
public void close() {
Fixed Show fixed Hide fixed
if (session != null && session.isOpen()) {
session.close();
}
if (sshClient != null && sshClient.isStarted()) {
sshClient.close();
}

}

static class COMMAND {

private COMMAND() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void init() {

@Override
public void handle(TaskCallBack taskCallBack) throws TaskException {
try {
// add task close method to release resource
try (RemoteExecutor executor = remoteExecutor) {
// construct process
String localFile = buildCommand();
int exitCode = remoteExecutor.run(taskId, localFile);
Expand Down
Loading