This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-invoker.git
commit 5c0b1fc6371d24afaf89649693209499cef9c0bb Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Mon Sep 21 14:00:07 2026 +0200 Stop the whole process tree when a build runs into its timeout CommandLineUtils destroys the process it started, which is the launcher shell; the Maven JVM under it and the JVMs Maven forked (Surefire) lived on. The command line now remembers its process and, on timeout, destroys the descendants through ProcessHandle before the launcher goes; on Java 8 the behaviour is unchanged. Closes #135 --- .../maven/shared/invoker/DefaultInvoker.java | 82 +++++++++++++++++----- .../shared/invoker/MavenCommandLineBuilder.java | 13 +++- .../maven/shared/invoker/DefaultInvokerTest.java | 14 ++-- .../org/apache/maven/shared/invoker/AppTest.java | 30 ++++---- 4 files changed, 102 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java b/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java index f2ba03e..3b94c6f 100644 --- a/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java +++ b/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java @@ -23,6 +23,9 @@ import javax.inject.Singleton; import java.io.File; import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.stream.Stream; import org.apache.maven.shared.utils.cli.CommandLineException; import org.apache.maven.shared.utils.cli.CommandLineUtils; @@ -68,7 +71,12 @@ public class DefaultInvoker implements Invoker { /** {@inheritDoc} */ public InvocationResult execute(InvocationRequest request) throws MavenInvocationException { - MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder(); + MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder() { + @Override + protected Commandline createCommandline() { + return new ProcessTrackingCommandline(); + } + }; if (logger != null) { cliBuilder.setLogger(logger); @@ -90,10 +98,10 @@ public class DefaultInvoker implements Invoker { cliBuilder.setBaseDirectory(workingDirectory); } - Commandline cli; + ProcessTrackingCommandline cli; try { - cli = cliBuilder.build(request); + cli = (ProcessTrackingCommandline) cliBuilder.build(request); } catch (CommandLineConfigurationException e) { throw new MavenInvocationException("Error configuring command line", e); } @@ -111,10 +119,8 @@ public class DefaultInvoker implements Invoker { return result; } - private int executeCommandLine(Commandline cli, InvocationRequest request, int timeoutInSeconds) + private int executeCommandLine(ProcessTrackingCommandline cli, InvocationRequest request, int timeoutInSeconds) throws CommandLineException { - int result; - InputStream inputStream = request.getInputStream(this.inputStream); InvocationOutputHandler outputHandler = request.getOutputHandler(this.outputHandler); InvocationOutputHandler errorHandler = request.getErrorHandler(this.errorHandler); @@ -127,22 +133,62 @@ public class DefaultInvoker implements Invoker { if (inputStream != null) { getLogger().info("Executing in batch mode. The configured input stream will be ignored."); } + inputStream = null; + } else if (inputStream == null) { + getLogger() + .warn("Maven will be executed in interactive mode" + + ", but no input stream has been configured for this MavenInvoker instance."); + } + + // the callback runs after a timeout while the Maven process is still alive, before + // CommandLineUtils destroys it; that is the moment to take its descendants with it + return CommandLineUtils.executeCommandLine( + cli, inputStream, outputHandler, errorHandler, timeoutInSeconds, cli::destroyDescendantsIfAlive); + } + + /** + * A command line that remembers the process it starts, so that a build which ran into its timeout can be + * stopped together with the processes it forked (Surefire, for instance), not just the launcher script. + */ + static class ProcessTrackingCommandline extends Commandline { + private volatile Process process; - result = CommandLineUtils.executeCommandLine(cli, outputHandler, errorHandler, timeoutInSeconds); - } else { - if (inputStream == null) { - getLogger() - .warn("Maven will be executed in interactive mode" - + ", but no input stream has been configured for this MavenInvoker instance."); - - result = CommandLineUtils.executeCommandLine(cli, outputHandler, errorHandler, timeoutInSeconds); - } else { - result = CommandLineUtils.executeCommandLine( - cli, inputStream, outputHandler, errorHandler, timeoutInSeconds); + @Override + public Process execute() throws CommandLineException { + process = super.execute(); + return process; + } + + void destroyDescendantsIfAlive() { + Process p = process; + if (p != null && p.isAlive()) { + destroyDescendants(p); } } + } - return result; + /** + * Forcibly destroys every descendant of the process, through {@code ProcessHandle} where the runtime is + * Java 9 or later; on Java 8 only the process itself can be reached and its children are left as they are. + */ + static void destroyDescendants(Process process) { + try { + Method toHandle = Process.class.getMethod("toHandle"); + Object handle = toHandle.invoke(process); + Class<?> processHandle = Class.forName("java.lang.ProcessHandle"); + Method destroyForcibly = processHandle.getMethod("destroyForcibly"); + Stream<?> descendants = + (Stream<?>) processHandle.getMethod("descendants").invoke(handle); + descendants.forEach(descendant -> { + try { + destroyForcibly.invoke(descendant); + } catch (IllegalAccessException | InvocationTargetException e) { + // the process may be gone already; nothing else to do for it + } + }); + } catch (ReflectiveOperationException | RuntimeException e) { + // Java 8: no ProcessHandle + } } /** diff --git a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java index c9043e3..d5780a7 100644 --- a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java +++ b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java @@ -58,7 +58,7 @@ public class MavenCommandLineBuilder { */ public Commandline build(InvocationRequest request) throws CommandLineConfigurationException { - Commandline cli = new Commandline(); + Commandline cli = createCommandline(); setupMavenHome(request); @@ -114,6 +114,17 @@ public class MavenCommandLineBuilder { } } + /** + * Creates the command line that {@link #build(InvocationRequest)} fills in. Subclasses can return a + * {@link Commandline} of their own, for example one that keeps track of the process it starts. + * + * @return a new, empty command line + * @since 3.3.1 + */ + protected Commandline createCommandline() { + return new Commandline(); + } + /** * <p>setSettingsLocation.</p> * diff --git a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java index 7593445..9dfe441 100644 --- a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java +++ b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java @@ -75,18 +75,24 @@ class DefaultInvokerTest { @Test void buildShouldTimeout() throws Exception { File basedir = getBasedirForBuild(); + File heartbeat = new File(basedir, "target/heartbeat.txt"); request.setBaseDirectory(basedir); request.addArgs(Arrays.asList("clean", "package")); - request.setTimeoutInSeconds(4); + request.addArg("-Dheartbeat=" + heartbeat.getAbsolutePath()); + // long enough for the nested build to compile and reach the forked test, short enough for a unit test + request.setTimeoutInSeconds(30); InvocationResult result = invoker.execute(request); // We check the exception to be sure the failure is based on timeout. assertTrue(result.getExecutionException().getMessage().contains("timed out")); - // WARN - Windows issue MSHARED-867 - Maven and child surefire test process stays alive on Windows - // workaround implemented in this test to timeout test after 15 sec - // please also check timeout logic in maven-shared-utils + // the Surefire fork under the timed out Maven must be gone too (MSHARED-867), so its heartbeat stops + assertTrue(heartbeat.isFile(), "the forked test never started: " + heartbeat); + Thread.sleep(1000L); + long length = heartbeat.length(); + Thread.sleep(2000L); + assertEquals(length, heartbeat.length(), "the forked test JVM is still running"); // exitCode can't be used because in case of a timeout it's not correctly // set in DefaultInvoker. Need to think about this. diff --git a/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java b/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java index 5bf7cc6..9933b06 100644 --- a/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java +++ b/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java @@ -1,7 +1,3 @@ -package org.apache.maven.shared.invoker; - -import org.junit.Test; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -21,21 +17,27 @@ import org.junit.Test; * under the License. */ -/** - * Unit test for simple App. - */ +package org.apache.maven.shared.invoker; + +import java.io.File; +import java.io.FileOutputStream; + +import org.junit.Test; + public class AppTest { - /** - * Not ending test - * @throws InterruptedException + * Never ends on its own: the invoker's timeout has to stop this forked JVM. The heartbeat file lets the + * invoker test see whether it did. */ - @Test(timeout = 7000) // should be killed in 4 sec by Invoker - Workaround for Windows MSHARED-867 - public void testApp() throws InterruptedException { + @Test + public void testApp() throws Exception { + File heartbeat = new File( System.getProperty( "heartbeat" ) ); while (true) { - Thread.sleep(1000L); + try ( FileOutputStream out = new FileOutputStream( heartbeat, true ) ) { + out.write( '.' ); + } + Thread.sleep( 200L ); } -// assertTrue( true); } }
