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


##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java:
##########
@@ -342,9 +357,20 @@ protected final void createTerminal(C context) {
                 }
                 doConfigureWithTerminal(context, terminal);
             });
+            // Register cleanup immediately so it runs even if awaitTerminal() 
is never reached
+            // (e.g. ExitException from helpOrVersionAndMayExit before 
awaitTerminal is called).
+            context.closeables.add(MessageUtils::systemUninstall);
+        }
+    }
 
+    /**
+     * Blocks until the terminal build started by {@link 
#startTerminalBuild(C)} has completed,
+     * then registers the terminal and log appender on the context.
+     * No-op if the terminal was already set before {@link 
#startTerminalBuild(C)} was called.
+     */
+    protected final void awaitTerminal(C context) {
+        if (context.terminal == null) {

Review Comment:
   ⚠️ **Race condition — `else` branch misses `ProjectBuildLogAppender`:** The 
`else` branch (taken when `context.terminal` was already set) conflates two 
callers:
   
   1. Terminal pre-set externally (embedded mode) — intentional skip of the 
appender.
   2. Background thread's `doConfigureWithTerminal` already set 
`context.terminal` before this method was called — the appender is wrongly 
skipped.
   
   In case 2: `ProjectBuildLogAppender` is never created → 
`MavenSimpleLogger.setLogSink` is never called → build log routing is broken 
for the entire build. Additionally, `doConfigureWithTerminal` is called a 
second time, double-wrapping `System.out`/`System.err` via 
`LoggingOutputStream`.
   
   `context.terminal` is not `volatile`, making this a data race (undefined 
behaviour on the JMM), but practically observable on the exec-provider path 
where terminal build is fast.
   
   **Suggested fix:** add a boolean flag (e.g. `context.terminalBuildStarted`) 
set in `startTerminalBuild`, and check it in `awaitTerminal` to distinguish the 
two `else` cases. `ProjectBuildLogAppender` and `registerShutdownHook` must run 
unconditionally when a background build was started.



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