From 2fa1b0cb8a884bbc1f6f60899c63f08c47fe2e3b Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 21 Apr 2007 14:27:46 +0000 Subject: [PATCH] make sure to terminate the process when the channel is severed. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3186 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/Launcher.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/Launcher.java b/core/src/main/java/hudson/Launcher.java index aeea696772..1472009575 100644 --- a/core/src/main/java/hudson/Launcher.java +++ b/core/src/main/java/hudson/Launcher.java @@ -102,6 +102,9 @@ public abstract class Launcher { * Launches a specified process and connects its input/output to a {@link Channel}, then * return it. * + *

+ * When the returned channel is terminated, the process will be killed. + * * @param out * Where the stderr from the launched process will be sent. */ @@ -165,15 +168,22 @@ public abstract class Launcher { public Channel launchChannel(String[] cmd, OutputStream out, FilePath workDir) throws IOException { printCommandLine(cmd, workDir); - Process proc = Runtime.getRuntime().exec(cmd, null, toFile(workDir)); - - // TODO: don't we need the equivalent of 'Proc' here? to abort it + final Process proc = Runtime.getRuntime().exec(cmd, null, toFile(workDir)); Thread t2 = new StreamCopyThread(cmd+": stderr copier", proc.getErrorStream(), out); t2.start(); return new Channel("locally launched channel on "+cmd, - Computer.threadPoolForRemoting, proc.getInputStream(), proc.getOutputStream(), out); + Computer.threadPoolForRemoting, proc.getInputStream(), proc.getOutputStream(), out) { + + /** + * Kill the process when the channel is severed. + */ + protected synchronized void terminate(IOException e) { + super.terminate(e); + proc.destroy(); + } + }; } /**