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


##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -47,29 +49,25 @@ public static void systemInstall() {
     }
 
     public static void systemInstall(Consumer<TerminalBuilder> 
builderConsumer, Consumer<Terminal> terminalConsumer) {
-        // Assign the FastTerminal to the field BEFORE starting the build 
thread so that code
-        // running on that thread (e.g. JLine's FFM provider init, logger 
calls) sees a non-null
-        // reference when it calls MessageUtils.getTerminal().  Thread.start() 
provides the
-        // happens-before edge that makes the assignment visible to the new 
thread.
-        FastTerminal ft = new FastTerminal(
-                () -> {
-                    TerminalBuilder builder =
-                            TerminalBuilder.builder().name("Maven").dumb(true);
-                    if (builderConsumer != null) {
-                        builderConsumer.accept(builder);
-                    }
-                    return builder.build();
-                },
-                terminal -> {
-                    MessageUtils.reader = createReader(terminal);
-                    AnsiConsole.setTerminal(terminal);
-                    AnsiConsole.systemInstall();
-                    if (terminalConsumer != null) {
-                        terminalConsumer.accept(terminal);
-                    }
-                });
-        MessageUtils.terminal = ft;
-        ft.start();
+        TerminalBuilder builder = 
TerminalBuilder.builder().name("Maven").dumb(true);
+        if (builderConsumer != null) {
+            builderConsumer.accept(builder);

Review Comment:
   ⚠️ **`builderConsumer.accept(builder)` runs before `MessageUtils.terminal = 
deferred` is assigned.** The old `FastTerminal` passed `builderConsumer` as a 
`Callable` that ran on the *build thread*, after `MessageUtils.terminal = ft` 
was set. Here it runs on the *calling thread*, before `deferred` is even 
created.
   
   `terminalAssignmentIsVisibleFromBuilderCallback` passes `builder -> 
observed.complete(MessageUtils.getTerminal())` as the consumer — this will now 
complete with `null` and cause `assertNotNull` to fail.



##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -100,11 +98,17 @@ 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;
+                }
             }

Review Comment:
   ⚠️ **Race between `isReady()` and `onComplete` callback:** `isReady()` 
returns `true` as soon as `future.isDone()` — but `DeferredTerminal.complete()` 
calls `future.complete(terminal)` *before* invoking the `onComplete` callback 
(which runs `AnsiConsole.systemInstall()`). If `doSystemUninstall` enters this 
block while the callback is still executing, `AnsiConsole.systemUninstall()` 
races with `AnsiConsole.systemInstall()`. The old `FastTerminal` avoided this 
because `systemInstall()` ran synchronously as part of the consumer, completing 
entirely before `future.complete()` was called.
   
   Consider waiting unconditionally (`deferred.getTerminal()`) rather than 
gating on `isReady()`.



-- 
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