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


##########
compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliLoggingTest.java:
##########
@@ -165,11 +166,108 @@ void withColorForcedLoggerCachesEarlyStream() throws 
Exception {
         }
     }
 
+    /**
+     * When {@code -l <logfile>} is used, {@code logging()} must call
+     * {@link MessageUtils#awaitTerminalInitialization()} before redirecting 
{@code System.out}
+     * to the log file.  Without the await, the terminal's installed consumer 
(which may call
+     * {@code System.setOut}) could race with the log-file redirect, causing 
logs to leak to
+     * the console instead of the log file (see
+     * <a href="https://github.com/apache/maven/issues/12188";>#12188</a>).
+     * <p>
+     * This test verifies that the terminal consumer fires <em>before</em> 
{@code System.out} is
+     * redirected to the log file, so the consumer cannot overwrite the 
log-file redirect.
+     */
+    @Test
+    void logFileBranchAwaitsTerminalBeforeRedirect(@TempDir Path tempDir) 
throws Exception {
+        PrintStream originalOut = System.out;
+
+        Path logFile = tempDir.resolve("maven.log");
+        Files.writeString(logFile, "");
+
+        CountDownLatch buildingTerminal = new CountDownLatch(1);
+        CountDownLatch releaseTerminal = new CountDownLatch(1);
+        AtomicBoolean consumerFiredBeforeRedirect = new AtomicBoolean(false);
+        Thread loggingThread = null;
+        try (Terminal dumb = new DumbTerminal(
+                "Maven",
+                Terminal.TYPE_DUMB_COLOR,
+                InputStream.nullInputStream(),
+                OutputStream.nullOutputStream(),
+                null)) {
+            DeferredTerminal terminal = new DeferredTerminal();
+            terminal.onComplete(installed -> {
+                consumerFiredBeforeRedirect.set(System.out == originalOut);
+            });

Review Comment:
   ⚠️ **Race condition — `terminalReady` not injected**: 
`awaitTerminalInitialization()` falls back to `deferred.getTerminal()` = 
`future.join()`, which returns **before** the `onComplete` callback runs 
(`DeferredTerminal.complete()` completes the future first, runs the callback 
second). The logging thread can proceed and redirect `System.out` while 
`consumerFiredBeforeRedirect` is still being set by the build thread.
   
   The fix is the same pattern used in `cachesOutputAfterTerminalInstallation`:
   ```suggestion
               DeferredTerminal terminal = new DeferredTerminal();
               CompletableFuture<Void> terminalReady = new 
CompletableFuture<>();
               terminal.onComplete(installed -> {
                   consumerFiredBeforeRedirect.set(System.out == originalOut);
                   terminalReady.complete(null);
               });
   ```



##########
compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliLoggingTest.java:
##########
@@ -165,11 +166,108 @@ void withColorForcedLoggerCachesEarlyStream() throws 
Exception {
         }
     }
 
+    /**
+     * When {@code -l <logfile>} is used, {@code logging()} must call
+     * {@link MessageUtils#awaitTerminalInitialization()} before redirecting 
{@code System.out}
+     * to the log file.  Without the await, the terminal's installed consumer 
(which may call
+     * {@code System.setOut}) could race with the log-file redirect, causing 
logs to leak to
+     * the console instead of the log file (see
+     * <a href="https://github.com/apache/maven/issues/12188";>#12188</a>).
+     * <p>
+     * This test verifies that the terminal consumer fires <em>before</em> 
{@code System.out} is
+     * redirected to the log file, so the consumer cannot overwrite the 
log-file redirect.
+     */
+    @Test
+    void logFileBranchAwaitsTerminalBeforeRedirect(@TempDir Path tempDir) 
throws Exception {
+        PrintStream originalOut = System.out;
+
+        Path logFile = tempDir.resolve("maven.log");
+        Files.writeString(logFile, "");
+
+        CountDownLatch buildingTerminal = new CountDownLatch(1);
+        CountDownLatch releaseTerminal = new CountDownLatch(1);
+        AtomicBoolean consumerFiredBeforeRedirect = new AtomicBoolean(false);
+        Thread loggingThread = null;
+        try (Terminal dumb = new DumbTerminal(
+                "Maven",
+                Terminal.TYPE_DUMB_COLOR,
+                InputStream.nullInputStream(),
+                OutputStream.nullOutputStream(),
+                null)) {
+            DeferredTerminal terminal = new DeferredTerminal();
+            terminal.onComplete(installed -> {
+                consumerFiredBeforeRedirect.set(System.out == originalOut);
+            });
+            Thread buildThread = new Thread(
+                    () -> {
+                        try {
+                            buildingTerminal.countDown();
+                            if (!releaseTerminal.await(10, SECONDS)) {
+                                terminal.completeExceptionally(
+                                        new IllegalStateException("Terminal 
installation was not released"));
+                                return;
+                            }
+                            terminal.complete(dumb);
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                            terminal.completeExceptionally(e);
+                        }
+                    },
+                    "deferred-terminal-build");
+            buildThread.setDaemon(true);
+            injectTerminal(terminal);

Review Comment:
   Also update the injection to include `terminalReady`, so 
`awaitTerminalInitialization()` takes the strong path (`ready.join()`) instead 
of the `deferred.getTerminal()` fallback:
   ```suggestion
               injectTerminal(terminal, terminalReady);
   ```



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