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

Fix shutdown hooks in native-image #520

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
3 changes: 3 additions & 0 deletions etc/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<suppress checks="FileLength"
files="helidon-cli/codegen/src/main/java/io/helidon/build/cli/codegen/AST.java"
lines="1"/>
<suppress checks="IllegalImport"
files="helidon-cli/impl/src/main/java/io/helidon/build/cli/impl/Helidon.java"
lines="22"/>
<suppress checks="LineLength"
files="helidon-archetype/engine/src/test/resources/META-INF/test.properties" />
<suppress checks="LineLength"
Expand Down
1 change: 1 addition & 0 deletions helidon-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- dev-loop incremental recompilation fails with maven 3.8.2 [499](https://github.com/oracle/helidon-build-tools/issues/499) [500](https://github.com/oracle/helidon-build-tools/pull/500)
- dev-loop does not re-build for resource files on Windows [357](https://github.com/oracle/helidon-build-tools/issues/357) [483](https://github.com/oracle/helidon-build-tools/issues/483) [498](https://github.com/oracle/helidon-build-tools/pull/498)
- dev-loop ctrl+c on Windows [511](https://github.com/oracle/helidon-build-tools/pull/511)
- fix shutdown hooks with native image [520](https://github.com/oracle/helidon-build-tools/pull/520)

### Changes

Expand Down
1 change: 1 addition & 0 deletions helidon-cli/impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
<argument>-H:Path=${build.dir}</argument>
<argument>-H:Name=${finalName}</argument>
<argument>-H:+PrintAnalysisCallTree</argument>
<argument>--install-exit-handlers</argument>
<argument>--no-fallback</argument>
<argument>-cp</argument>
<argument>${build.dir}${file.separator}${finalName}.jar${path.separator}${build.dir}${file.separator}libs${file.separator}*${path.separator}${build.dir}${file.separator}native</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public void print(String line) {
STDOUT.println();
insertLineIfError = false;
}
STDOUT.println(errorMessage);
STDOUT.print(errorMessage);
}
} else {
STDOUT.print(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import io.helidon.build.cli.harness.CommandLineInterface;
import io.helidon.build.cli.harness.CommandRunner;

import org.graalvm.nativeimage.ImageInfo;
import sun.misc.Signal;

/**
* Helidon CLI definition and entry-point.
*/
Expand All @@ -42,6 +45,13 @@ private Helidon() {
* @param args raw command line arguments
*/
public static void main(String[] args) {

if (ImageInfo.inImageRuntimeCode()) {
// Register a signal handler for Ctrl-C that calls System.exit in order to trigger
// the shutdown hooks
Signal.handle(new Signal("INT"), sig -> System.exit(0));
}

CommandRunner.builder()
.args(args)
.optionLookup(Config.userConfig()::property)
Expand Down
1 change: 1 addition & 0 deletions helidon-cli/impl/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
requires io.helidon.build.util;
requires maven.model;
requires org.graalvm.sdk;
requires jdk.unsupported;
provides io.helidon.build.cli.harness.CommandRegistry
with io.helidon.build.cli.impl.HelidonRegistry;
}
14 changes: 10 additions & 4 deletions utils/src/main/java/io/helidon/build/util/ProcessMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -50,6 +51,7 @@ public final class ProcessMonitor {
private final ConsoleRecorder recorder;
private final boolean capturing;
private final CompletableFuture<Void> exitFuture;
private final AtomicBoolean shutdown;
private final Runnable beforeShutdown;
private final Runnable afterShutdown;
private volatile Process process;
Expand Down Expand Up @@ -250,6 +252,7 @@ private ProcessMonitor(Builder builder) {
builder.filter,
builder.transform,
builder.capture);
this.shutdown = new AtomicBoolean();
this.beforeShutdown = builder.beforeShutdown;
this.afterShutdown = builder.afterShutdown;
this.exitFuture = new CompletableFuture<>();
Expand Down Expand Up @@ -352,7 +355,8 @@ public ProcessMonitor waitForCompletion(long timeout, TimeUnit unit)
try {
exitFuture.get(timeout, unit);
try {
if (process.exitValue() != 0) {
// ignore exit code if this is a shutdown
if (process.exitValue() != 0 && !shutdown.get()) {
throw new ProcessFailedException();
}
} catch (IllegalThreadStateException ex) {
Expand Down Expand Up @@ -483,6 +487,7 @@ private void shutdown() {
.map(p -> {
p.recorder.stop();
p.beforeShutdown.run();
p.shutdown.set(true);
p.process.destroy();
return p.exitFuture.thenRun(p.afterShutdown);
})
Expand Down Expand Up @@ -523,9 +528,10 @@ private ProcessException(String reason) {

@Override
public String getMessage() {
final StringBuilder message = new StringBuilder();
message.append(requireNonNullElseGet(description, () -> String.join(" ", builder.command())));
message.append(reason);
final StringBuilder message = new StringBuilder()
.append(requireNonNullElseGet(description, () -> String.join(" ", builder.command())))
.append(" ")
.append(reason);
if (capturing) {
message.append(Constants.EOL);
for (String line : output().split("\\R")) {
Expand Down