You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/07/27 18:19:24 UTC

(maven) 01/01: Fix #12536: Handle Ctrl+C on Windows terminals (#12538)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch backport-12538-to-maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git

commit cbe0a96063c2a136ca8fbd3354fdf6daf0cdbc40
Author: Jack Myers <jm...@jackmyers.one>
AuthorDate: Tue Jul 28 02:18:04 2026 +0800

    Fix #12536: Handle Ctrl+C on Windows terminals (#12538)
    
    On Windows, JLine handles Ctrl+C as the terminal's `INT` signal. Without
    an explicit handler, the standalone Maven JVM does not follow its normal
    interrupt termination path and Maven's shutdown hooks do not run, which
    can leave long-running child processes alive.
    
    This change registers an `INT` handler for Windows terminals that exits
    with status `130`. This triggers normal JVM shutdown and its registered
    cleanup hooks.
---
 .../java/org/apache/maven/cling/invoker/LookupInvoker.java  | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java
index 56c7fb83d7..5ef24ad725 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java
@@ -89,6 +89,7 @@
 import org.jline.terminal.TerminalBuilder;
 import org.jline.terminal.impl.AbstractPosixTerminal;
 import org.jline.terminal.spi.TerminalExt;
+import org.jline.utils.OSUtils;
 import org.slf4j.LoggerFactory;
 import org.slf4j.bridge.SLF4JBridgeHandler;
 import org.slf4j.spi.LocationAwareLogger;
@@ -103,6 +104,8 @@
  * @param <C> The context type.
  */
 public abstract class LookupInvoker<C extends LookupContext> implements Invoker {
+    private static final int SIGINT_EXIT_CODE = 130;
+
     protected final Lookup protoLookup;
 
     @Nullable
@@ -317,9 +320,13 @@ protected final void createTerminal(C context) {
                     new ProjectBuildLogAppender(determineBuildEventListener(context));
             context.closeables.add(projectBuildLogAppender);
 
-            MessageUtils.systemInstall(
-                    builder -> doCreateTerminal(context, builder),
-                    terminal -> doConfigureWithTerminal(context, terminal));
+            MessageUtils.systemInstall(builder -> doCreateTerminal(context, builder), terminal -> {
+                if (OSUtils.IS_WINDOWS && !context.invokerRequest.embedded()) {
+                    // JLine may consume Ctrl+C as terminal input, bypassing the JVM's shutdown hooks.
+                    terminal.handle(Terminal.Signal.INT, signal -> System.exit(SIGINT_EXIT_CODE));
+                }
+                doConfigureWithTerminal(context, terminal);
+            });
 
             context.terminal = MessageUtils.getTerminal();
             context.closeables.add(MessageUtils::systemUninstall);