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


##########
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);
+        }
+        // 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();

Review Comment:
   ⚠️ **`buildDeferred()` throws when the builder has custom streams** — in 
embedded mode, `doCreateTerminal()` calls `builder.streams(in, out)` via this 
consumer, and `TerminalBuilder.buildDeferred()` in JLine 4.7.0 rejects non-null 
streams with `IllegalStateException`. This also breaks all tests in 
`FastTerminalReentrancyTest` (see `installAndAwait` which sets 
`.streams(...)`). The old `FastTerminal` approach worked because the builder 
was created and built inside the `Callable`, not via `buildDeferred()`.
   
   Fix: in embedded mode, skip `systemInstall(builderConsumer, 
terminalConsumer)` entirely and use a pre-built terminal; or fall back to 
synchronous `build()` when `buildDeferred()` is unavailable for the given 
builder:
   
   ```java
           // buildDeferred() is only supported for system terminals (no custom 
streams).
           // Fall back to synchronous build() when streams have been set by 
the consumer.
           Terminal deferred;
           try {
               deferred = builder.buildDeferred();
           } catch (IllegalStateException e) {
               // Consumer set custom streams (e.g. embedded mode) — build 
synchronously
               Terminal built = builder.build();
               MessageUtils.reader = createReader(built);
               AnsiConsole.setTerminal(built);
               AnsiConsole.systemInstall();
               if (terminalConsumer != null) {
                   terminalConsumer.accept(built);
               }
               MessageUtils.terminal = built;
               return;
           }
   ```



##########
impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java:
##########
@@ -64,12 +55,9 @@ class FastTerminalReentrancyTest {
 
     @AfterEach
     void tearDown() {
-        // MessageUtils.terminal is process-global; leaving it set breaks 
every later test.
-        // On a regression the build thread never finishes and systemUninstall 
waits for it (see
-        // #11048), on the test thread and outside any timeout, so the fork 
would hang instead of
-        // reporting the failure. Leave the state dirty in that case; the run 
is lost either way.
-        if (MessageUtils.getTerminal() instanceof FastTerminal ft && 
!ft.isBuilt()) {
-            return;
+        if (MessageUtils.getTerminal() instanceof DeferredTerminal) {
+            // Ensure the deferred terminal is ready before uninstalling
+            ((DeferredTerminal) MessageUtils.getTerminal()).getTerminal();
         }
         MessageUtils.systemUninstall();

Review Comment:
   ⚠️ **Safety guard removed** — the old tearDown returned early when 
`!ft.isBuilt()` to prevent hanging the test fork when the build thread never 
completes. The new code calls `DeferredTerminal.getTerminal()` which calls 
`CompletableFuture.join()` unconditionally — this blocks forever on a 
regression (the fork hangs outside any timeout) and throws 
`CompletionException` on build failure, masking the actual test failure. The 
old comment in this method explicitly called this out: *"On a regression the 
build thread never finishes... so the fork would hang instead of reporting the 
failure."*
   
   Since `doSystemUninstall()` already has the `!deferred.isReady()` fast-path, 
the simplest fix is:
   
   ```suggestion
       @AfterEach
       void tearDown() {
           MessageUtils.systemUninstall();
       }
   ```



##########
impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java:
##########
@@ -175,7 +157,7 @@ private void installAndAwait(Consumer<TerminalBuilder> 
onBuilder, Consumer<Termi
                             .streams(InputStream.nullInputStream(), 
OutputStream.nullOutputStream());

Review Comment:
   ⚠️ **`buildDeferred()` will throw here** — this consumer sets 
`.streams(InputStream.nullInputStream(), OutputStream.nullOutputStream())` on 
the builder, then returns it to `MessageUtils.systemInstall()` which calls 
`builder.buildDeferred()`. In JLine 4.7.0, `buildDeferred()` throws 
`IllegalStateException` when streams are set. Every test that calls 
`installAndAwait` will fail with this exception before reaching any assertion. 
This needs to be reworked once the main `systemInstall` fix is in place 
(whether fallback `build()` or separate embedded path).



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