gnodet-bot commented on code in PR #13924:
URL: https://github.com/apache/maven/pull/13924#discussion_r4515523712
##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java:
##########
@@ -175,10 +175,11 @@ protected int doInvoke(C context) throws Exception {
pushUserProperties(context);
setupGuiceClassLoading(context);
configureLogging(context);
- createTerminal(context);
+ startTerminalBuild(context);
activateLogging(context);
helpOrVersionAndMayExit(context);
preCommands(context);
+ awaitTerminal(context);
Review Comment:
⚠️ **Race condition — NPE window:** `helpOrVersionAndMayExit` (line 180) and
`preCommands` (line 181) are called while `context.terminal` is **null**.
`startTerminalBuild` starts the background FFM build but does not set
`context.terminal`; that only happens in `awaitTerminal` (line 182).
`doDetermineWriter` creates a lambda that dereferences
`context.terminal.writer()` at call time — if `helpOrVersionAndMayExit` invokes
the writer (e.g. for `--help`, `--version`, or `-X --show-version`), you get a
**NullPointerException**.
Additionally, if `helpOrVersionAndMayExit` or `preCommands` throws
`ExitException`, `awaitTerminal` is never reached and
`MessageUtils::systemUninstall` is never registered — leaking
`MessageUtils.terminal` in embedded mode.
Consider moving `awaitTerminal(context)` above
`helpOrVersionAndMayExit(context)`:
```suggestion
startTerminalBuild(context);
activateLogging(context);
awaitTerminal(context);
helpOrVersionAndMayExit(context);
preCommands(context);
```
##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java:
##########
@@ -342,7 +355,16 @@ protected final void createTerminal(C context) {
}
doConfigureWithTerminal(context, terminal);
});
+ }
+ }
+ /**
+ * 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:
🔍 **Guard note:** This guard (`context.terminal == null`) may not hold when
`doConfigureWithTerminal` on the background thread has already set
`context.terminal` to the raw terminal. In that case the block is skipped,
`context.terminal = MessageUtils.getTerminal()` is never called, and
`closeables.add(MessageUtils::systemUninstall)` is never registered.
This is a secondary concern subsumed by the NPE fix above, but worth noting
for correctness.
--
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]