gnodet-bot commented on code in PR #13924:
URL: https://github.com/apache/maven/pull/13924#discussion_r4561737424
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -47,29 +49,25 @@ 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();
Review Comment:
⛔ **Critical — `buildDeferred()` rejects custom streams, breaking embedded
mode.**
`builderConsumer.accept(builder)` runs at lines 53–55, potentially calling
`builder.streams(in, out)` (via `doCreateTerminal()` in `LookupInvoker` for
embedded invocations). Then `buildDeferred()` is called here on the builder
that already has non-null streams. JLine 4.7.0 throws:
```
java.lang.IllegalStateException: buildDeferred() is only supported for
system terminals.
Use build() for terminals with custom streams.
```
This breaks Surefire, m2e, and any `MavenCli`-based embedder. The fix is to
split the terminal creation path: build synchronously via `build()` for
embedded, use `buildDeferred()` only for CLI (system-terminal) mode.
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -47,29 +49,25 @@ 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();
+ // Register a callback that runs on the build thread once the real
+ // terminal is ready, before any blocked caller is unblocked.
Review Comment:
⚠️ **Misleading comment — callback runs *after* blocked callers are
unblocked, not before.**
The comment says "before any blocked caller is unblocked", but
`DeferredTerminal.complete()` calls `future.complete(terminal)` first (which
makes `isReady()` return `true` and unblocks waiters on `getTerminal()`), and
*then* invokes the `onComplete` callback. The callback runs after unblocking,
not before.
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -47,29 +49,25 @@ 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();
+ // Register a callback that runs on the build thread once the real
+ // terminal is ready, before any blocked caller is unblocked.
+ deferred.onComplete(terminal -> {
+ MessageUtils.reader = createReader(terminal);
+ AnsiConsole.setTerminal(terminal);
+ AnsiConsole.systemInstall();
+ if (terminalConsumer != null) {
+ terminalConsumer.accept(terminal);
+ }
+ });
+ MessageUtils.terminal = deferred;
Review Comment:
⚠️ **Terminal assignment ordering — assign `MessageUtils.terminal =
deferred` BEFORE `onComplete` registration.**
`buildDeferred()` starts a daemon build thread internally. The `onComplete`
callback (registered at line 62) runs on that thread when the terminal is
ready. If the callback calls `MessageUtils.getTerminal()` — directly or
transitively — it will see `null` because the assignment at this line hasn't
executed yet.
With `FastTerminal`, the field was assigned before `ft.start()`, so the
happens-before edge of `Thread.start()` guaranteed visibility. Apply the same
pattern here: assign `MessageUtils.terminal = deferred` immediately after
`buildDeferred()` (line 59), before registering `onComplete`.
##########
impl/maven-jline/src/main/java/org/apache/maven/jline/MessageUtils.java:
##########
@@ -100,11 +98,17 @@ 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()) {
Review Comment:
⚠️ **Race condition in `doSystemUninstall()` — `isReady()` fires before
`onComplete` callback completes.**
`DeferredTerminal.isReady()` returns `true` as soon as `future.isDone()`,
which happens *before* the `onComplete` callback returns. The callback is what
calls `AnsiConsole.systemInstall()`. If `doSystemUninstall()` runs in this
window (ready=true but callback still running), `AnsiConsole.systemUninstall()`
at line 109 is called without a matching `systemInstall()`.
After the `isReady()` check, add `deferred.getTerminal()` to wait for both
the future *and* the callback before proceeding:
```suggestion
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;
}
// getTerminal() blocks until the future AND the onComplete
callback finish,
// ensuring AnsiConsole.systemInstall() has run before we
uninstall.
deferred.getTerminal();
}
```
##########
impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java:
##########
@@ -35,147 +32,84 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
- * {@link MessageUtils#systemInstall} publishes the terminal before the
background thread has built
- * it, so anything that thread logs is rendered through a terminal that same
thread is still
- * producing. See <a
href="https://github.com/apache/maven/issues/12761">#12761</a> and
- * <a href="https://github.com/apache/maven/issues/12912">#12912</a>.
- * <p>
- * A single log statement asks the terminal for two things, its type while
rendering the message and
- * its writer while emitting the line, so both are exercised from both halves
of the window: the
- * builder callable, and the consumer that runs before the terminal is
published.
- * <p>
- * JLine 4.4.0's FFM provider initialization ({@code CLibrary.<clinit>}) can
reach back through
- * other terminal methods (e.g. {@code getName()}, {@code getColumns()},
{@code encoding()}) on the
- * build thread, so the fallback must cover all delegate methods, not just
{@code writer()} and
- * {@code getType()}.
- * <p>
- * The tests force {@code providers("exec")} so that the builder creates a
lightweight
- * {@code ExternalTerminal} instead of a {@code PosixPtyTerminal} via FFM. The
FFM provider calls
- * {@code CLibrary.openpty()} which creates a real PTY pair; the subsequent
grapheme-cluster probe
- * ({@code ensureModesProbed()}) reads from the PTY master via {@code
FileInputStream.read0()}, a
- * native blocking read. Although {@code VMIN=0, VTIME=0} attributes are set
before probing, there
- * is a race where the native read blocks indefinitely instead of respecting
the timeout, causing
- * the test to hang until the 30-second preemptive timeout fires.
- * <p>
- * The timeouts are preemptive on purpose: a regression parks the build thread
forever, and only an
- * abandoning timeout turns that into a red test rather than a hung fork.
+ * Verifies that {@link DeferredTerminal}'s non-blocking methods work correctly
+ * when called before the real terminal is ready.
Review Comment:
🔧 **Stale class name — `FastTerminalReentrancyTest` now tests
`DeferredTerminal`.**
The Javadoc now references `DeferredTerminal` and all test methods have been
rewritten around it. Please rename the class (and file) to
`DeferredTerminalTest` or `DeferredTerminalReentrancyTest`.
--
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]