You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/08/30 02:30:12 UTC
(maven) branch fix/12912-guard-close created (now 0e60db7eaa)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a change to branch fix/12912-guard-close
in repository https://gitbox.apache.org/repos/asf/maven.git
at 0e60db7eaa Fix #12912: use a dumb fallback terminal for all build-thread reentrant calls
This branch includes the following new commits:
new 0e60db7eaa Fix #12912: use a dumb fallback terminal for all build-thread reentrant calls
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(maven) 01/01: Fix #12912: use a dumb fallback terminal for all build-thread reentrant calls
Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch fix/12912-guard-close
in repository https://gitbox.apache.org/repos/asf/maven.git
commit 0e60db7eaafacbde68601504215d869951b8fc05
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Sun Aug 30 04:29:59 2026 +0200
Fix #12912: use a dumb fallback terminal for all build-thread reentrant calls
Guard getTerminal() instead of individual methods so every delegate is
covered — JLine 4.4.0's FFM provider initialization reaches back through
arbitrary terminal methods on the build thread. The DumbTerminal fallback
output is wrapped with FilterOutputStream to prevent close() from closing
the captured System.err.
Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
.../java/org/apache/maven/jline/FastTerminal.java | 64 +++++++++++++---------
.../maven/jline/FastTerminalReentrancyTest.java | 47 +++++++++++++++-
2 files changed, 83 insertions(+), 28 deletions(-)
diff --git a/impl/maven-jline/src/main/java/org/apache/maven/jline/FastTerminal.java b/impl/maven-jline/src/main/java/org/apache/maven/jline/FastTerminal.java
index 2cd81f9289..0fbf5f59ee 100644
--- a/impl/maven-jline/src/main/java/org/apache/maven/jline/FastTerminal.java
+++ b/impl/maven-jline/src/main/java/org/apache/maven/jline/FastTerminal.java
@@ -18,10 +18,10 @@
*/
package org.apache.maven.jline;
+import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.concurrent.Callable;
@@ -37,6 +37,7 @@
import org.jline.terminal.Size;
import org.jline.terminal.Sized;
import org.jline.terminal.Terminal;
+import org.jline.terminal.impl.DumbTerminal;
import org.jline.terminal.spi.SystemStream;
import org.jline.terminal.spi.TerminalExt;
import org.jline.terminal.spi.TerminalProvider;
@@ -56,21 +57,37 @@ public class FastTerminal implements TerminalExt {
private final Thread buildThread;
/**
- * Writer handed to {@link #buildThread} while the real terminal is still being built. Created on
- * demand, and only ever by {@link #buildThread}.
+ * A dumb terminal constructed <em>before</em> the build thread starts, used as a stand-in for
+ * every delegate method when the build thread would otherwise wait on its own future. Guarding
+ * only individual methods was sufficient for JLine 4.3.x, but JLine 4.4.0's FFM provider
+ * initialization ({@code CLibrary.<clinit>}) reaches back through arbitrary terminal methods, so
+ * the fallback must cover all of them. Its output stream is wrapped so that closing the fallback
+ * terminal does not close the captured {@code System.err}. See
+ * <a href="https://github.com/apache/maven/issues/12912">#12912</a>.
*/
- private PrintWriter fallbackWriter;
-
- /**
- * Captured before {@link #buildThread} starts, hence before the consumer swaps the system streams
- * for logging-backed ones. Writing to the live {@code System.err} instead would feed the fallback
- * output back into the logger it came from.
- */
- private final OutputStream fallbackOutput;
+ private final TerminalExt fallbackTerminal;
public FastTerminal(Callable<Terminal> builder, Consumer<Terminal> consumer) {
this.terminal = new CompletableFuture<>();
- this.fallbackOutput = System.err;
+ // Capture System.err before the build thread starts — the consumer swaps the system streams
+ // for logging-backed ones, and writing to the live System.err would feed the fallback output
+ // back into the logger it came from.
+ OutputStream fallbackOutput = System.err;
+ TerminalExt dumbFallback;
+ try {
+ // Construct a DumbTerminal directly instead of going through TerminalBuilder, which
+ // probes for grapheme-cluster support and can block on a null input stream in JLine 4.4.0.
+ // Wrap the output so closing the fallback does not close the underlying System.err.
+ dumbFallback = new DumbTerminal(InputStream.nullInputStream(), new FilterOutputStream(fallbackOutput) {
+ @Override
+ public void close() throws IOException {
+ flush();
+ }
+ });
+ } catch (IOException e) {
+ dumbFallback = null;
+ }
+ this.fallbackTerminal = dumbFallback;
this.buildThread = new Thread(
() -> {
try {
@@ -88,6 +105,12 @@ public FastTerminal(Callable<Terminal> builder, Consumer<Terminal> consumer) {
}
public TerminalExt getTerminal() {
+ if (isBuildThreadWaitingOnItself()) {
+ if (fallbackTerminal != null) {
+ return fallbackTerminal;
+ }
+ throw new IllegalStateException("Terminal not yet available (build in progress on this thread)");
+ }
try {
return (TerminalExt) terminal.get();
} catch (Exception e) {
@@ -111,14 +134,6 @@ boolean isBuilt() {
return terminal.isDone();
}
- private PrintWriter fallbackWriter() {
- // only buildThread reaches this, so no synchronization is needed
- if (fallbackWriter == null) {
- fallbackWriter = new PrintWriter(new OutputStreamWriter(fallbackOutput, Charset.defaultCharset()), true);
- }
- return fallbackWriter;
- }
-
@Override
public String getName() {
return getTerminal().getName();
@@ -141,8 +156,7 @@ public NonBlockingReader reader() {
@Override
public PrintWriter writer() {
- // the log sink writes here, and must not wait for the terminal this thread is building
- return isBuildThreadWaitingOnItself() ? fallbackWriter() : getTerminal().writer();
+ return getTerminal().writer();
}
@Override
@@ -247,11 +261,7 @@ public void flush() {
@Override
public String getType() {
- // AttributedCharSequence.toAnsi asks for the type first and renders plain for a dumb one,
- // so answering here keeps message rendering off the terminal this thread is building
- return isBuildThreadWaitingOnItself()
- ? Terminal.TYPE_DUMB
- : getTerminal().getType();
+ return getTerminal().getType();
}
@Override
diff --git a/impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java b/impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java
index 0fa35e8815..ff668adf75 100644
--- a/impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java
+++ b/impl/maven-jline/src/test/java/org/apache/maven/jline/FastTerminalReentrancyTest.java
@@ -32,16 +32,23 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+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>.
+ * 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
+ * arbitrary terminal methods (e.g. {@code getName()}, {@code getSize()}, {@code encoding()}) on the
+ * build thread, so the fallback must cover all delegate methods, not just {@code writer()} and
+ * {@code getType()}.
+ * <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.
*/
@@ -77,6 +84,44 @@ void usingTheTerminalFromTheConsumerDoesNotDeadlock() {
});
}
+ /**
+ * Exercises terminal methods beyond {@code writer()} and {@code getType()} that JLine's FFM
+ * provider initialization can reach on the build thread. Before the fallback terminal was added,
+ * these would deadlock. See <a href="https://github.com/apache/maven/issues/12912">#12912</a>.
+ */
+ @Test
+ void arbitraryTerminalMethodsFromTheBuilderDoNotDeadlock() {
+ assertTimeoutPreemptively(Duration.ofSeconds(30), () -> {
+ CompletableFuture<int[]> probed = new CompletableFuture<>();
+ installAndAwait(
+ builder -> {
+ Terminal t = MessageUtils.getTerminal();
+ probed.complete(
+ new int[] {t.getSize().getColumns(), t.getSize().getRows()});
+ },
+ terminal -> {});
+ int[] dims = probed.get();
+ assertTrue(dims[0] >= 0, "width should be non-negative");
+ assertTrue(dims[1] >= 0, "height should be non-negative");
+ });
+ }
+
+ /**
+ * Exercises terminal methods beyond {@code writer()} and {@code getType()} from the consumer
+ * callback. See <a href="https://github.com/apache/maven/issues/12912">#12912</a>.
+ */
+ @Test
+ void arbitraryTerminalMethodsFromTheConsumerDoNotDeadlock() {
+ assertTimeoutPreemptively(Duration.ofSeconds(30), () -> {
+ CompletableFuture<String> probed = new CompletableFuture<>();
+ installAndAwait(builder -> {}, terminal -> {
+ Terminal t = MessageUtils.getTerminal();
+ probed.complete(t.getName());
+ });
+ assertNotNull(probed.get());
+ });
+ }
+
/**
* Both terminal calls a single log statement makes, run on the terminal building thread.
*/