gnodet-bot commented on code in PR #14024:
URL: https://github.com/apache/maven/pull/14024#discussion_r4585887806
##########
api/maven-api-core/src/main/java/org/apache/maven/api/Constants.java:
##########
@@ -935,5 +935,18 @@ public final class Constants {
readOnly = true)
public static final String MAVEN_SITE_LIFECYCLE_DISABLED =
"maven.site.lifecycle.disabled";
+ /**
+ * Whether to build the terminal on a background thread. When {@code
false},
+ * the terminal is built synchronously during startup.
+ *
Review Comment:
⚠️ **Still wrong `@since` tag** — `4.0.0` has already shipped without
`MAVEN_TERMINAL_DEFERRED`. This constant is introduced by this PR, so the tag
must reflect the actual release it ships in. Compare with
`MAVEN_LIFECYCLE_FILTER` which correctly uses `@since 4.2.0`.
```suggestion
* @since 4.2.0
```
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -30,66 +31,67 @@
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.impl.DeferredTerminal;
+import org.jline.utils.OSUtils;
public class MessageUtils {
- static Terminal terminal;
- static LineReader reader;
+ private static final int SIGINT_EXIT_CODE = 130;
+
+ static volatile Terminal terminal;
+ static volatile LineReader reader;
static MessageBuilderFactory messageBuilderFactory = new
JLineMessageBuilderFactory();
- static boolean colorEnabled = true;
+ static volatile Boolean colorEnabled;
static Thread shutdownHook;
static final Object STARTUP_SHUTDOWN_MONITOR = new Object();
- /**
- * Tracks completion of the terminal's {@code onComplete} callback
(including
- * {@link AnsiConsole#systemInstall()}). Callers of {@link
#awaitTerminalInitialization()}
- * must wait on this future, not just on {@link
DeferredTerminal#getTerminal()}, because
- * the JLine future is resolved <em>before</em> the {@code onComplete}
callback runs and
- * {@code System.out} is wrapped.
- */
- static volatile CompletableFuture<Void> terminalReady;
public static void systemInstall(Terminal terminal) {
MessageUtils.terminal = terminal;
- MessageUtils.reader = terminal != null ? createReader(terminal) : null;
}
public static void systemInstall() {
- systemInstall(null, null);
+ systemInstall((Consumer<TerminalBuilder>) null);
+ }
+
+ public static void systemInstall(Consumer<TerminalBuilder>
builderConsumer) {
+ systemInstall(builderConsumer, false);
}
- public static void systemInstall(Consumer<TerminalBuilder>
builderConsumer, Consumer<Terminal> terminalConsumer) {
+ public static void systemInstall(Consumer<TerminalBuilder>
builderConsumer, boolean embedded) {
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 -> {
+ boolean deferred =
+ !embedded &&
Boolean.parseBoolean(System.getProperty(Constants.MAVEN_TERMINAL_DEFERRED,
"true"));
+ if (!deferred) {
try {
- MessageUtils.reader = createReader(terminal);
- AnsiConsole.setTerminal(terminal);
- AnsiConsole.systemInstall();
- if (terminalConsumer != null) {
- terminalConsumer.accept(terminal);
+ MessageUtils.terminal = builder.build();
+ AnsiConsole.setTerminal(MessageUtils.terminal);
+ if (OSUtils.IS_WINDOWS) {
+ MessageUtils.terminal.handle(Terminal.Signal.INT, signal
-> System.exit(SIGINT_EXIT_CODE));
}
- } finally {
- ready.complete(null);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
}
- });
- MessageUtils.terminal = deferred;
+ } else {
+ DeferredTerminal deferredTerminal = (DeferredTerminal)
builder.buildDeferred();
+ deferredTerminal.onComplete(terminal -> {
+ if (OSUtils.IS_WINDOWS) {
+ terminal.handle(Terminal.Signal.INT, signal ->
System.exit(SIGINT_EXIT_CODE));
+ }
+ AnsiConsole.setTerminal(terminal);
+ });
+ MessageUtils.terminal = deferredTerminal;
+ }
}
- private static LineReader createReader(Terminal terminal) {
- return LineReaderBuilder.builder().terminal(terminal).build();
+ static LineReader getReader() {
+ LineReader r = reader;
+ if (r == null) {
+ r = LineReaderBuilder.builder().terminal(terminal).build();
+ reader = r;
+ }
+ return r;
}
Review Comment:
⚠️ **`getReader()` has a check-then-act race** — `reader` is `volatile` but
the lazy initialization is not synchronized. Two threads can both observe `r ==
null`, both call `LineReaderBuilder.builder().terminal(terminal).build()`, and
each write their own `LineReader` to the field. After that race, the two
threads hold **different** `LineReader` instances against the same terminal —
which is not safe (each `LineReader` maintains its own history, signal
handlers, and input state).
Fix with a synchronized block (consistent with `STARTUP_SHUTDOWN_MONITOR`
already present in this class):
```suggestion
static LineReader getReader() {
LineReader r = reader;
if (r == null) {
synchronized (STARTUP_SHUTDOWN_MONITOR) {
r = reader;
if (r == null) {
r =
LineReaderBuilder.builder().terminal(terminal).build();
reader = r;
}
}
}
return r;
}
```
--
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]