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

Allow disabling process killing on interruption #3293

Merged
merged 3 commits into from
Mar 10, 2018
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
43 changes: 35 additions & 8 deletions core/src/main/java/hudson/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public final class ProcStarter {
*/
protected boolean reverseStdin, reverseStdout, reverseStderr;

/**
* True to prevent killing the launched process when it is interrupted
* @since TODO
*/
protected boolean dontKillWhenInterrupted;

/**
* Passes a white-space separated single-string command (like "cat abc def") and parse them
* as a command argument. This method also handles quotes.
Expand Down Expand Up @@ -441,6 +447,22 @@ public ProcStarter writeStdin() {
return this;
}

/**
* Indicates that the launched process should not be killed when interrupted.
* It allows detecting the interruption on caller's side and do custom (cleanup) action while
* the launched process is still running.
*
* <p>
* Note that the process can (and should) be killed
* via {@link Proc#kill()} when custom action is done.
*
* @return {@code this}
* @since TODO
*/
public ProcStarter dontKillWhenInterrupted() {
this.dontKillWhenInterrupted = true;
return this;
}

/**
* Starts the new process as configured.
Expand Down Expand Up @@ -926,7 +948,7 @@ public Proc launch(ProcStarter ps) throws IOException {
ps.reverseStdin ?LocalProc.SELFPUMP_INPUT:ps.stdin,
ps.reverseStdout?LocalProc.SELFPUMP_OUTPUT:ps.stdout,
ps.reverseStderr?LocalProc.SELFPUMP_OUTPUT:ps.stderr,
toFile(ps.pwd));
toFile(ps.pwd), ps.dontKillWhenInterrupted);
}

private File toFile(FilePath f) {
Expand Down Expand Up @@ -1049,7 +1071,8 @@ public Proc launch(ProcStarter ps) throws IOException {
final String workDir = psPwd==null ? null : psPwd.getRemote();

try {
return new ProcImpl(getChannel().call(new RemoteLaunchCallable(ps.commands, ps.masks, ps.envs, in, ps.reverseStdin, out, ps.reverseStdout, err, ps.reverseStderr, ps.quiet, workDir, listener)));
return new ProcImpl(getChannel().call(new RemoteLaunchCallable(ps.commands, ps.masks, ps.envs, in, ps.reverseStdin,
out, ps.reverseStdout, err, ps.reverseStderr, ps.quiet, workDir, listener, ps.dontKillWhenInterrupted)));
} catch (InterruptedException e) {
throw (IOException)new InterruptedIOException().initCause(e);
}
Expand Down Expand Up @@ -1267,12 +1290,14 @@ private static class RemoteLaunchCallable extends MasterToSlaveCallable<RemotePr
private final @Nonnull TaskListener listener;
private final boolean reverseStdin, reverseStdout, reverseStderr;
private final boolean quiet;

RemoteLaunchCallable(@Nonnull List<String> cmd, @CheckForNull boolean[] masks, @CheckForNull String[] env,
@CheckForNull InputStream in, boolean reverseStdin,
@CheckForNull OutputStream out, boolean reverseStdout,
@CheckForNull OutputStream err, boolean reverseStderr,
boolean quiet, @CheckForNull String workDir, @Nonnull TaskListener listener) {
private final boolean dontKillWhenInterrupted;

RemoteLaunchCallable(@Nonnull List<String> cmd, @CheckForNull boolean[] masks, @CheckForNull String[] env,
@CheckForNull InputStream in, boolean reverseStdin,
@CheckForNull OutputStream out, boolean reverseStdout,
@CheckForNull OutputStream err, boolean reverseStderr,
boolean quiet, @CheckForNull String workDir,
@Nonnull TaskListener listener, boolean dontKillWhenInterrupted) {
this.cmd = new ArrayList<>(cmd);
this.masks = masks;
this.env = env;
Expand All @@ -1285,6 +1310,7 @@ private static class RemoteLaunchCallable extends MasterToSlaveCallable<RemotePr
this.reverseStdout = reverseStdout;
this.reverseStderr = reverseStderr;
this.quiet = quiet;
this.dontKillWhenInterrupted = dontKillWhenInterrupted;
}

public RemoteProcess call() throws IOException {
Expand All @@ -1295,6 +1321,7 @@ public RemoteProcess call() throws IOException {
if (reverseStdin) ps.writeStdin();
if (reverseStdout) ps.readStdout();
if (reverseStderr) ps.readStderr();
if (dontKillWhenInterrupted) ps.dontKillWhenInterrupted();

final Proc p = ps.start();

Expand Down
23 changes: 17 additions & 6 deletions core/src/main/java/hudson/Proc.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
public abstract class Proc {
protected Proc() {}

/**
* Indicates that the process should not be killed on interruption.
* @since TODO
*/
protected boolean dontKillWhenInterrupted;

/**
* Checks if the process is still alive.
*/
Expand Down Expand Up @@ -207,17 +213,17 @@ public LocalProc(String[] cmd,String[] env,InputStream in,OutputStream out) thro
}

public LocalProc(String[] cmd,String[] env,InputStream in,OutputStream out, File workDir) throws IOException {
this(cmd,env,in,out,null,workDir);
this(cmd,env,in,out,null,workDir, true);
}

/**
* @param err
* null to redirect stderr to stdout.
*/
public LocalProc(String[] cmd,String[] env,InputStream in,OutputStream out,OutputStream err,File workDir) throws IOException {
public LocalProc(String[] cmd,String[] env,InputStream in,OutputStream out,OutputStream err,File workDir, boolean dontKillWhenInterrupted) throws IOException {
this( calcName(cmd),
stderr(environment(new ProcessBuilder(cmd),env).directory(workDir), err==null || err== SELFPUMP_OUTPUT),
in, out, err );
in, out, err, dontKillWhenInterrupted);
}

private static ProcessBuilder stderr(ProcessBuilder pb, boolean redirectError) {
Expand All @@ -237,10 +243,11 @@ private static ProcessBuilder environment(ProcessBuilder pb, String[] env) {
return pb;
}

private LocalProc( String name, ProcessBuilder procBuilder, InputStream in, OutputStream out, OutputStream err ) throws IOException {
private LocalProc( String name, ProcessBuilder procBuilder, InputStream in, OutputStream out, OutputStream err, boolean dontKillWhenInterrupted ) throws IOException {
Logger.getLogger(Proc.class.getName()).log(Level.FINE, "Running: {0}", name);
this.name = name;
this.out = out;
this.dontKillWhenInterrupted = dontKillWhenInterrupted;
this.cookie = EnvVars.createCookie();
procBuilder.environment().putAll(cookie);
if (procBuilder.directory() != null && !procBuilder.directory().exists()) {
Expand Down Expand Up @@ -354,7 +361,9 @@ public int join() throws InterruptedException, IOException {
return r;
} catch (InterruptedException e) {
// aborting. kill the process
destroy();
if (!dontKillWhenInterrupted) {
destroy();
}
throw e;
} finally {
t.setName(oldName);
Expand Down Expand Up @@ -462,7 +471,9 @@ public int join() throws IOException, InterruptedException {
return process.get();
} catch (InterruptedException e) {
LOGGER.log(Level.FINE, String.format("Join operation has been interrupted for the process %s. Killing the process", this), e);
kill();
if (!dontKillWhenInterrupted) {
kill();
}
throw e;
} catch (ExecutionException e) {
if(e.getCause() instanceof IOException)
Expand Down