Skip to content

Commit

Permalink
Ignore process exit code for exec() if JVM is shutting down
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Nov 4, 2024
1 parent 06d2c6f commit a200ec8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
27 changes: 23 additions & 4 deletions blaze-core/src/main/java/com/fizzed/blaze/local/LocalExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.fizzed.blaze.core.UnexpectedExitValueException;
import com.fizzed.blaze.util.CommandLines;
import com.fizzed.blaze.util.ProcessReaper;
import org.zeroturnaround.exec.InvalidExitValueException;
Expand Down Expand Up @@ -185,10 +186,28 @@ public void stop() {
} finally {
ProcessReaper.INSTANCE.unregister(startedProcess.getProcess());
}
} catch (InvalidExitValueException e) {
throw new com.fizzed.blaze.core.UnexpectedExitValueException("Process exited with unexpected value", this.exitValues, e.getExitValue());
} catch (IOException | InterruptedException | ExecutionException e) {
throw new BlazeException("Unable to cleanly execute process", e);
} catch (Throwable t) {
if (t instanceof ExecutionException) {
ExecutionException ee = (ExecutionException)t;
if (ee.getCause() instanceof InvalidExitValueException) {
// this is actually what we want to process
t = ee.getCause();
}
}

if (t instanceof InvalidExitValueException) {
InvalidExitValueException ievae = (InvalidExitValueException)t;

// this can happen IF we're in the process of being shutdown and we actually don't want to throw an exception
if (ProcessReaper.INSTANCE.isShuttingDown()) {
log.trace("Shutting down, ignoring invalid exit code on exec()");
return new Exec.Result(this, ievae.getExitValue());
}

throw new UnexpectedExitValueException("Process exited with unexpected value", this.exitValues, ievae.getExitValue());
}

throw new BlazeException("Unable to cleanly execute process", t);
} finally {
// close all the output streams (input stream closed above)
Streamables.close(os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class ProcessReaper {
private boolean shutdownHookAdded;
private final Set<Process> processes;
private final ReentrantLock lock;
private volatile boolean shuttingDown;

static public final ProcessReaper INSTANCE = new ProcessReaper();

public ProcessReaper() {
this.processes = new HashSet<>();
this.lock = new ReentrantLock();
this.shuttingDown = false;
}

public void register(Process process) {
Expand All @@ -45,8 +47,14 @@ public void unregister(Process process) {
}
}

public boolean isShuttingDown() {
return shuttingDown;
}

private Thread newShutdownThread() {
return new Thread(() -> {
// flag to indicate we are now shutting down (which can help not throw exceptions in some cases)
this.shuttingDown = true;
if (!this.processes.isEmpty()) {
log.debug("Will destroy {} process(es) along with its children (to properly cleanup what we started)", this.processes.size());
ProcessHelper processHelper = ProcessHelper.get();
Expand Down

0 comments on commit a200ec8

Please sign in to comment.