gnodet-bot commented on code in PR #13924:
URL: https://github.com/apache/maven/pull/13924#discussion_r4552902442


##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -100,11 +102,22 @@ public static void systemUninstall() {
 
     private static void doSystemUninstall() {
         try {
-            if (terminal instanceof FastTerminal fastTerminal) {
-                // wait for the asynchronous systemInstall call before we 
uninstall to ensure a consistent state
-                fastTerminal.getTerminal();
+            if (terminal instanceof DeferredTerminal deferred) {
+                if (!deferred.isReady()) {
+                    // Don't wait — drain buffered output to stdout and let
+                    // the daemon build thread die with the JVM.
+                    deferred.close();
+                    return;
+                }
+                // Wait for the onComplete callback to finish before 
uninstalling.
+                // isReady() returns true as soon as future.isDone(), but the 
onComplete callback
+                // (which runs AnsiConsole.systemInstall()) may not have 
finished yet.
+                // getTerminal() blocks until both the future and the callback 
complete.
+                deferred.getTerminal();

Review Comment:
   ⚠️ **Incorrect claim:** The comment says `getTerminal()` blocks until both 
the future *and* the callback complete — but `DeferredTerminal.complete()` 
calls `future.complete(terminal)` first (which unblocks `future.join()` waiters 
immediately), then runs `cb.accept(terminal)` afterward. So `getTerminal()` can 
return while `AnsiConsole.systemInstall()` is still executing on the build 
thread, creating a race with `AnsiConsole.systemUninstall()` on the calling 
thread.
   
   With `FastTerminal` this was safe because `consumer.accept(term)` ran 
*before* `terminal.complete(term)`. `DeferredTerminal` reverses that order. You 
need an additional synchronisation barrier to close this window.



##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java:
##########
@@ -340,13 +340,36 @@ protected final void createTerminal(C context) {
                     new 
ProjectBuildLogAppender(determineBuildEventListener(context));
             context.closeables.add(projectBuildLogAppender);
 
-            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));
+            if (context.invokerRequest.embedded()) {
+                // Embedded mode: build the terminal synchronously so it is 
available immediately.
+                // DeferredTerminal.buildDeferred() rejects custom streams, so 
we build eagerly.
+                InputStream in = 
context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
+                OutputStream out = 
context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
+                context.coloredOutput = context.coloredOutput != null ? 
context.coloredOutput : false;
+                context.closeables.add(out::flush);
+                try {
+                    Terminal terminal = TerminalBuilder.builder()
+                            .name("Maven")
+                            .dumb(true)
+                            .streams(in, out)
+                            .provider(TerminalBuilder.PROP_PROVIDER_EXEC)
+                            .build();
+                    MessageUtils.systemInstall(terminal);

Review Comment:
   ⚠️ **`AnsiConsole` install/uninstall asymmetry.** `systemInstall(terminal)` 
(single-arg) does not call `AnsiConsole.setTerminal()` or 
`AnsiConsole.systemInstall()`. But `doSystemUninstall()` calls 
`AnsiConsole.systemUninstall()` unconditionally for any terminal, including 
those installed via this path. Calling `systemUninstall()` without a prior 
`systemInstall()` will decrement Jansi's install counter below zero — 
potentially corrupting ANSI state in embedded scenarios.



##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java:
##########
@@ -170,12 +170,12 @@ private static void restoreRootLoggerHandlers(Handler[] 
handlers) {
     }
 
     protected int doInvoke(C context) throws Exception {
+        createTerminal(context);

Review Comment:
   ⚠️ **Data race on `context.coloredOutput`.** `createTerminal` now runs 
before `configureLogging`. `configureLogging` sets `context.coloredOutput` 
(from `-Dstyle.color`). The `onComplete` callback reads `context.coloredOutput` 
via `doConfigureWithTerminal`. If the background terminal build completes 
before `configureLogging` runs, `coloredOutput` is still `null` and the colour 
preference is silently ignored.
   
   In the old ordering (`configureLogging` → `createTerminal`), `coloredOutput` 
was always set before the terminal callback ran. This reordering reintroduces 
the race.



##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java:
##########
@@ -340,13 +340,36 @@ protected final void createTerminal(C context) {
                     new 
ProjectBuildLogAppender(determineBuildEventListener(context));
             context.closeables.add(projectBuildLogAppender);
 
-            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));
+            if (context.invokerRequest.embedded()) {
+                // Embedded mode: build the terminal synchronously so it is 
available immediately.
+                // DeferredTerminal.buildDeferred() rejects custom streams, so 
we build eagerly.
+                InputStream in = 
context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
+                OutputStream out = 
context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
+                context.coloredOutput = context.coloredOutput != null ? 
context.coloredOutput : false;
+                context.closeables.add(out::flush);
+                try {
+                    Terminal terminal = TerminalBuilder.builder()
+                            .name("Maven")
+                            .dumb(true)
+                            .streams(in, out)
+                            .provider(TerminalBuilder.PROP_PROVIDER_EXEC)
+                            .build();
+                    MessageUtils.systemInstall(terminal);
+                    doConfigureWithTerminal(context, terminal);
+                } catch (IOException e) {
+                    throw new RuntimeException("Failed to create embedded 
terminal", e);
                 }
-                doConfigureWithTerminal(context, terminal);
-            });
+            } else {
+                // System terminal: use DeferredTerminal so the FFM/JNI 
provider starts
+                // building in the background while Maven continues 
initializing.
+                MessageUtils.systemInstall(null, terminal -> {

Review Comment:
   ⚠️ **`doCreateTerminal()` override hook silently dropped.** The old code 
passed `builder -> doCreateTerminal(context, builder)` as the builder consumer, 
allowing subclasses to customise the terminal builder. Here `null` is passed, 
so any `doCreateTerminal` override is never called for the system-terminal 
path. The old base implementation also set 
`builder.systemOutput(TerminalBuilder.SystemOutput.SysOut)` and 
`builder.color(context.coloredOutput)` — both are now gone.
   
   Fix: pass `builder -> doCreateTerminal(context, builder)` here, or 
explicitly deprecate `doCreateTerminal` and migrate all existing overrides.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to