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

Kill process when thread for local command is interrupted #7007

Merged
merged 1 commit into from
May 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}