From e088ecd1bc4d6b98c06f7afccaaf06dbdd3a5023 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 20 Jun 2014 13:18:12 +0100 Subject: [PATCH] Re-order shutdown hook in BootRunTask --- .../boot/gradle/run/BootRunTask.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java index 3980d6ac5b..01f451d24e 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java @@ -30,7 +30,6 @@ import org.gradle.process.ExecResult; import org.gradle.process.internal.DefaultJavaExecAction; import org.gradle.process.internal.ExecHandle; import org.springframework.boot.loader.tools.FileUtils; -import org.springframework.boot.loader.tools.SignalUtils; import org.springframework.util.ReflectionUtils; /** @@ -67,24 +66,29 @@ public class BootRunTask extends JavaExec { } } - private ExecResult executeReflectively() throws Exception { + private ExecResult executeReflectively() throws Exception { Field builder = ReflectionUtils.findField(JavaExec.class, "javaExecHandleBuilder"); builder.setAccessible(true); DefaultJavaExecAction action = (DefaultJavaExecAction) builder.get(this); setMain(getMain()); - final ExecHandle execHandle = action.build(); - ExecResult execResult = execHandle.start().waitForFinish(); - if (!isIgnoreExitValue()) { - execResult.assertNormalExitValue(); - } - SignalUtils.attachSignalHandler(new Runnable() { - @Override - public void run() { - getLogger().info("Aborting java sub-process"); - execHandle.abort(); - } - }); - return execResult; - } + final ExecHandle execHandle = action.build(); + try { + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + + @Override + public void run() { + getLogger().info("Aborting java sub-process"); + execHandle.abort(); + } + })); + } catch (Exception e) { + getLogger().warn("Could not attach shutdown hook (child process may be orphaned)"); + } + ExecResult execResult = execHandle.start().waitForFinish(); + if (!isIgnoreExitValue()) { + execResult.assertNormalExitValue(); + } + return execResult; + } }