gnodet-bot commented on code in PR #13990:
URL: https://github.com/apache/maven/pull/13990#discussion_r4570245391
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -47,29 +58,34 @@ 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();
+ // Track when the full onComplete callback has finished, not just when
the
+ // JLine future resolves. The JLine future completes *before* the
callback
+ // runs, so awaitTerminalInitialization() must wait on this separate
future.
+ CompletableFuture<Void> ready = new CompletableFuture<>();
+ terminalReady = ready;
+ // Register a callback that runs on the build thread once the real
+ // terminal is ready, before any blocked caller is unblocked.
+ deferred.onComplete(terminal -> {
+ try {
+ MessageUtils.reader = createReader(terminal);
+ AnsiConsole.setTerminal(terminal);
+ AnsiConsole.systemInstall();
+ if (terminalConsumer != null) {
+ terminalConsumer.accept(terminal);
+ }
+ } finally {
+ ready.complete(null);
+ }
Review Comment:
⚠️ **Previous finding NOT addressed.** The `try/finally` still fires
`ready.complete(null)` unconditionally. If `createReader()`,
`AnsiConsole.systemInstall()`, or `terminalConsumer.accept()` throws, the
exception propagates out of the lambda but `ready.complete(null)` has already
fired — `awaitTerminalInitialization()` returns normally while `System.out` was
never wrapped. Any subsequent terminal operation then throws an unexpected
`RuntimeException`.
```suggestion
deferred.onComplete(terminal -> {
try {
MessageUtils.reader = createReader(terminal);
AnsiConsole.setTerminal(terminal);
AnsiConsole.systemInstall();
if (terminalConsumer != null) {
terminalConsumer.accept(terminal);
}
ready.complete(null);
} catch (Exception | LinkageError ex) {
ready.completeExceptionally(ex);
throw ex;
}
});
```
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -100,27 +116,43 @@ 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;
Review Comment:
⚠️ **TOCTOU race on the shutdown path.** Between `!deferred.isReady()`
returning `false` and `deferred.close()` executing, the background build can
complete (including `onComplete` → `AnsiConsole.systemInstall()`). The `return`
then exits before `AnsiConsole.systemUninstall()` runs, leaving JAnsi state
installed permanently. Low risk in practice since this is a JVM-exit path, but
it can cause issues in test environments where
`systemInstall`/`systemUninstall` are called repeatedly (as in the new tests).
Consider removing the early return and always falling through to
`AnsiConsole.systemUninstall()`.
--
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]