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


##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -47,29 +58,34 @@ 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);
+        }
+        // buildDeferred() returns a DeferredTerminal proxy immediately 
(~10ms).
+        // The real terminal is built on a background daemon thread; all proxy
+        // methods block transparently until it is ready.
+        DeferredTerminal deferred = (DeferredTerminal) builder.buildDeferred();
+        // Track when the full onComplete callback has finished, not just when 
the
+        // JLine future resolves.  The JLine future completes *before* the 
callback
+        // runs, so awaitTerminalInitialization() must wait on this separate 
future.
+        CompletableFuture<Void> ready = new CompletableFuture<>();
+        terminalReady = ready;
+        // Register a callback that runs on the build thread once the real
+        // terminal is ready, before any blocked caller is unblocked.
+        deferred.onComplete(terminal -> {
+            try {
+                MessageUtils.reader = createReader(terminal);
+                AnsiConsole.setTerminal(terminal);
+                AnsiConsole.systemInstall();
+                if (terminalConsumer != null) {
+                    terminalConsumer.accept(terminal);
+                }
+            } finally {
+                ready.complete(null);
+            }
+        });
+        MessageUtils.terminal = deferred;

Review Comment:
   ⚠️ **Missing `catch` block — exception silently swallowed**
   
   If `createReader()`, `AnsiConsole.systemInstall()`, or the 
`terminalConsumer` throws, the `finally` block still calls 
`ready.complete(null)`. `awaitTerminalInitialization()` then returns 
successfully to callers, who proceed thinking initialization succeeded when it 
actually failed (the exception was silently discarded by JLine's callback 
dispatcher).
   
   Fix: add a `catch` block that calls `ready.completeExceptionally(t)`, and 
move `ready.complete(null)` to the end of the `try` body (not `finally`):
   
   ```suggestion
           deferred.onComplete(terminal -> {
               try {
                   MessageUtils.reader = createReader(terminal);
                   AnsiConsole.setTerminal(terminal);
                   AnsiConsole.systemInstall();
                   if (terminalConsumer != null) {
                       terminalConsumer.accept(terminal);
                   }
                   ready.complete(null);
               } catch (Throwable t) {
                   ready.completeExceptionally(t);
               }
           });
   ```



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