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


##########
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();
+        // Register a callback that runs on the build thread once the real
+        // terminal is ready, before any blocked caller is unblocked.

Review Comment:
   ⚠️ **Misleading comment — callback runs *after* blocked callers are 
unblocked, not before.**
   
   The JLine 4.7.0 `DeferredTerminal.complete()` source is explicit (comment is 
from JLine itself):
   ```java
   // Complete the future first so that if the callback throws, waiters are not 
hung.
   boolean published = future.complete(terminal);  // ← releases blocked callers
   // ... flush buffer ...
   Consumer<Terminal> cb = onComplete.getAndSet(null);
   if (cb != null) {
       cb.accept(terminal);  // ← onComplete callback fires AFTER 
future.complete()
   }
   ```
   Callers blocked on `deferred.getTerminal()` are unblocked by 
`future.complete()`. The `onComplete` callback fires afterward. The comment 
states the opposite.
   
   ```suggestion
           // Register a callback that runs on the build thread once the real
           // terminal is ready. Note: in JLine 4.7.0, blocked callers are
           // unblocked (via future.complete()) before this callback fires.
   ```



##########
impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java:
##########
@@ -35,147 +32,84 @@
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
- * {@link MessageUtils#systemInstall} publishes the terminal before the 
background thread has built
- * it, so anything that thread logs is rendered through a terminal that same 
thread is still
- * producing. See <a 
href="https://github.com/apache/maven/issues/12761";>#12761</a> and
- * <a href="https://github.com/apache/maven/issues/12912";>#12912</a>.
- * <p>
- * A single log statement asks the terminal for two things, its type while 
rendering the message and
- * its writer while emitting the line, so both are exercised from both halves 
of the window: the
- * builder callable, and the consumer that runs before the terminal is 
published.
- * <p>
- * JLine 4.4.0's FFM provider initialization ({@code CLibrary.<clinit>}) can 
reach back through
- * other terminal methods (e.g. {@code getName()}, {@code getColumns()}, 
{@code encoding()}) on the
- * build thread, so the fallback must cover all delegate methods, not just 
{@code writer()} and
- * {@code getType()}.
- * <p>
- * The tests force {@code providers("exec")} so that the builder creates a 
lightweight
- * {@code ExternalTerminal} instead of a {@code PosixPtyTerminal} via FFM. The 
FFM provider calls
- * {@code CLibrary.openpty()} which creates a real PTY pair; the subsequent 
grapheme-cluster probe
- * ({@code ensureModesProbed()}) reads from the PTY master via {@code 
FileInputStream.read0()}, a
- * native blocking read. Although {@code VMIN=0, VTIME=0} attributes are set 
before probing, there
- * is a race where the native read blocks indefinitely instead of respecting 
the timeout, causing
- * the test to hang until the 30-second preemptive timeout fires.
- * <p>
- * The timeouts are preemptive on purpose: a regression parks the build thread 
forever, and only an
- * abandoning timeout turns that into a red test rather than a hung fork.
+ * Verifies that {@link DeferredTerminal}'s non-blocking methods work correctly
+ * when called before the real terminal is ready.
  */
 class FastTerminalReentrancyTest {

Review Comment:
   🔧 **Stale class name — rename to `DeferredTerminalReentrancyTest`.**
   
   The Javadoc at lines 35–36 now says *"Verifies that `DeferredTerminal`'s 
non-blocking methods work correctly when called before the real terminal is 
ready"* — but the class is still named `FastTerminalReentrancyTest`. Rename the 
file and class.



##########
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;
+                }
             }
             AnsiConsole.systemUninstall();

Review Comment:
   ⚠️ **Race condition in `doSystemUninstall()` — `isReady()` fires before 
`onComplete` callback completes.**
   
   `DeferredTerminal.isReady()` delegates to `future.isDone()`. In JLine 4.7.0, 
`complete()` calls `future.complete(terminal)` first (making `isReady()` return 
`true`), then flushes the buffer, then invokes the `onComplete` callback. So 
this code can see `isReady() == true` and call `AnsiConsole.systemUninstall()` 
(line 109) while the `onComplete` callback (line 65) is concurrently calling 
`AnsiConsole.systemInstall()` — leaving the console in a corrupted state.
   
   The old code called `fastTerminal.getTerminal()` which blocked until both 
the future and the callback were done. The equivalent is 
`deferred.getTerminal()`:
   
   ```suggestion
               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;
                   }
                   // isReady() returns true as soon as future.isDone(), but the
                   // onComplete callback (AnsiConsole.systemInstall()) runs 
after
                   // future.complete() — wait for it before uninstalling.
                   deferred.getTerminal();
               }
               AnsiConsole.systemUninstall();
   ```



##########
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();
+        // Register a callback that runs on the build thread once the real
+        // terminal is ready, before any blocked caller is unblocked.
+        deferred.onComplete(terminal -> {
+            MessageUtils.reader = createReader(terminal);
+            AnsiConsole.setTerminal(terminal);
+            AnsiConsole.systemInstall();
+            if (terminalConsumer != null) {
+                terminalConsumer.accept(terminal);
+            }
+        });
+        MessageUtils.terminal = deferred;

Review Comment:
   ⚠️ **Terminal assignment ordering — assign `MessageUtils.terminal = 
deferred` before `onComplete` registration.**
   
   `buildDeferred()` (line 59) starts the background build thread internally. 
The `onComplete` callback (registered at line 62) can fire on that thread 
before line 70 executes on the calling thread. Any code in the 
`terminalConsumer` callback (via line 67: `terminalConsumer.accept(terminal)`) 
that calls `MessageUtils.getTerminal()` will see `null`.
   
   The old `FastTerminal` code set `MessageUtils.terminal = ft` *before* 
`ft.start()` to guarantee visibility — the same guarantee must hold here. 
Assign the field immediately after `buildDeferred()`, before `onComplete`:
   
   ```suggestion
           DeferredTerminal deferred = (DeferredTerminal) 
builder.buildDeferred();
           MessageUtils.terminal = deferred;
           // Register a callback that runs on the build thread once the real
           // terminal is ready. Note: in JLine 4.7.0, blocked callers are
           // unblocked (via future.complete()) before this callback fires.
           deferred.onComplete(terminal -> {
               MessageUtils.reader = createReader(terminal);
               AnsiConsole.setTerminal(terminal);
               AnsiConsole.systemInstall();
               if (terminalConsumer != null) {
                   terminalConsumer.accept(terminal);
               }
           });
   ```



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