Skip to content

Commit

Permalink
Merge pull request #7007 from rundeck/issue/1518-openssh-executor-kil…
Browse files Browse the repository at this point in the history
…l-job

Kill process when thread for local command is interrupted
  • Loading branch information
gschueler committed May 6, 2021
2 parents a4a77f2 + 2504c37 commit 54885b8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,28 @@ public static int runLocalCommand(
final Streams.StreamCopyThread outthread = Streams.copyStreamThread(exec.getInputStream(), outputStream);
errthread.start();
outthread.start();
final int result = exec.waitFor();

outputStream.flush();
errorStream.flush();
errthread.join();
outthread.join();
exec.getInputStream().close();
exec.getErrorStream().close();
if (null != outthread.getException()) {
throw outthread.getException();
}
if (null != errthread.getException()) {
throw errthread.getException();
try {
final int result = exec.waitFor();
outputStream.flush();
errorStream.flush();
errthread.join();
outthread.join();
exec.getInputStream().close();
exec.getErrorStream().close();
if (null != outthread.getException()) {
throw outthread.getException();
}
if (null != errthread.getException()) {
throw errthread.getException();
}
return result;
} catch (InterruptedException e) {
exec.destroy();
if(exec.isAlive()){
exec.waitFor();
}
throw new InterruptedException("Execution interrupted with code: " + exec.exitValue());
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.dtolabs.rundeck.core.utils

import spock.lang.Specification

import java.util.concurrent.CountDownLatch

/**
* @author greg
* @since 4/19/17
Expand Down Expand Up @@ -48,4 +50,39 @@ class ScriptExecUtilSpec extends Specification {
null | ["a", "arg"] | '${globals.sudo}' | false | ['/bin/sudo', '/a/file', 'a', 'arg']
null | ["a", "arg"] | '${globals.sudo} myexec' | false | ['/bin/sudo', 'myexec', '/a/file', 'a', 'arg']
}

def "interrupt local command should kill subprocess"() {

when:
Map<String, String> envMap = [:]
String[] command = ["sleep", "10"]
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ByteArrayOutputStream errStream = new ByteArrayOutputStream();
def started = new CountDownLatch(1)
def main = new CountDownLatch(1)
GroovyMock(Runtime, global: true)
String interruptedMessageExpected = "Execution interrupted with code: 143"
String interruptedMessage = ""
def t = new Thread(
{
started.countDown()
try {
ScriptExecUtil.runLocalCommand(command, envMap, null, outStream, errStream)
}catch(InterruptedException e){
interruptedMessage = e.getMessage()
Thread.currentThread().interrupt()
} finally {
main.countDown()
}
}
)
t.start()
started.await()
Thread.sleep(2000)
t.interrupt()
main.await()

then:
interruptedMessageExpected == interruptedMessage
}
}

0 comments on commit 54885b8

Please sign in to comment.