Skip to content

Commit

Permalink
fix(ssh): Join thread when the output streams are closed (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu authored Apr 3, 2023
1 parent 886ba21 commit 3cfe66f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/java/io/kestra/plugin/fs/ssh/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class Command extends Task implements AbstractVfsInterface, RunnableTask<
public VoidOutput run(RunContext runContext) throws Exception {
Session session = null;
ChannelExec channel = null;
AbstractLogThread stdOut = null;
AbstractLogThread stdErr = null;

try(
var outStream = new PipedOutputStream();
Expand All @@ -97,17 +99,14 @@ public VoidOutput run(RunContext runContext) throws Exception {
channel.setCommand(String.join("\n", renderedCommands));
channel.setOutputStream(new BufferedOutputStream(outStream), true);
channel.setErrStream(new BufferedOutputStream(errStream), true);
var stdOut = threadLogSupplier(runContext).call(inStream, false);
var stdErr = threadLogSupplier(runContext).call(inErrStream, true);
stdOut = threadLogSupplier(runContext).call(inStream, false);
stdErr = threadLogSupplier(runContext).call(inErrStream, true);

channel.connect();
while (channel.isConnected()) {
Thread.sleep(SLEEP_DELAY_MS);
}

stdOut.join();
stdErr.join();

if(channel.getExitStatus() != 0) {
throw new Exception("SSH command fails with exit status " + channel.getExitStatus());
}
Expand All @@ -120,6 +119,12 @@ public VoidOutput run(RunContext runContext) throws Exception {
if (session != null) {
session.disconnect();
}
if (stdOut != null) {
stdOut.join();
}
if (stdErr != null) {
stdErr.join();
}
}
}

Expand Down

0 comments on commit 3cfe66f

Please sign in to comment.