This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ec856f41c756 CAMEL-27004: camel-jbang - camel debug reads the output
of the integration all the time, so it does not block on a full pipe or spin at
100% CPU (#30122)
ec856f41c756 is described below
commit ec856f41c7566990f0d6a1caf73a38bd3edc99c4
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 23:07:19 2026 +0100
CAMEL-27004: camel-jbang - camel debug reads the output of the integration
all the time, so it does not block on a full pipe or spin at 100% CPU (#30122)
…ation all the time, so it does not block on a full pipe or spin at 100% CPU
camel debug read the stdout of the integration it spawned in a loop that
created a new reader on the ended stream again and again, at 100% CPU,
after the integration was gone. It did not read stdout at all with
--log-lines=0, and read stderr only after the integration had exited, so
an integration that wrote more than a pipe holds (64 KB) blocked.
OutputTail reads a stream on a daemon thread until its end and keeps the
last lines. camel debug reads stdout and stderr with it from the start,
and prints the last of both when the integration has ended.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../camel/dsl/jbang/core/commands/Debug.java | 76 ++++++---------
.../camel/dsl/jbang/core/commands/OutputTail.java | 104 +++++++++++++++++++++
.../dsl/jbang/core/commands/OutputTailTest.java | 96 +++++++++++++++++++
3 files changed, 228 insertions(+), 48 deletions(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
index 05f9ce95647d..987bac56d987 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
@@ -16,13 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands;
-import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.reflect.Field;
@@ -82,8 +80,6 @@ import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jline.jansi.Ansi;
import org.jline.jansi.AnsiConsole;
-import static org.apache.camel.util.IOHelper.buffered;
-
@CommandDefinition(name = "debug", description = "Debug local Camel
integration", generateHelp = true,
footer = {
"Examples:",
@@ -155,7 +151,9 @@ public class Debug extends Run {
private Process spawnProcess;
private InputStream spawnOutput;
private InputStream spawnError;
- private final List<String> logBuffer = new ArrayList<>(100);
+ // the output of the spawned process, read all the time, so it never
blocks on a full pipe
+ private OutputTail logTail;
+ private OutputTail errorTail;
private final AtomicBoolean logUpdated = new AtomicBoolean();
private SuspendedRow suspendedRow = new SuspendedRow();
private final AtomicBoolean waitForUser = new AtomicBoolean();
@@ -206,15 +204,20 @@ public class Debug extends Run {
tableHelper.setShowExchangeProperties(showExchangeProperties);
tableHelper.setShowExchangeVariables(showExchangeVariables);
- // read log input
- final AtomicBoolean quit = new AtomicBoolean();
- if (logLines > 0) {
- Thread t = new Thread(() -> {
- doReadLog(quit);
- }, "ReadLog");
- t.start();
+ // read the output of the spawned process; also with --log-lines=0, so
it does not block on a full pipe
+ if (spawnOutput != null) {
+ logTail = new OutputTail(100, () -> {
+ if (logLines > 0) {
+ logUpdated.set(true);
+ }
+ }).start(spawnOutput, "ReadLog");
+ }
+ if (spawnError != null) {
+ errorTail = new OutputTail(1000, null).start(spawnError,
"ReadError");
}
+ final AtomicBoolean quit = new AtomicBoolean();
+
// read CLI input from user
Thread t2 = new Thread(() -> doRead(quit), "ReadCommand");
t2.start();
@@ -225,19 +228,9 @@ public class Debug extends Run {
// wait a little bit before loading again
Thread.sleep(100);
} else if (exit == -1) {
- // maybe failed on startup, so dump log buffer
- for (String line : logBuffer) {
- printer().println(line);
- }
- // and any error
- if (spawnError != null) {
- try {
- String text = IOHelper.loadText(spawnError);
- printer().println(text);
- } catch (Exception e) {
- // ignore
- }
- }
+ // maybe failed on startup, so dump the last of its log and
errors
+ printTail(logTail);
+ printTail(errorTail);
return debugExitCode();
}
} while (exit == 0 && !quit.get());
@@ -282,29 +275,15 @@ public class Debug extends Run {
return 0;
}
- private void doReadLog(AtomicBoolean quit) {
- do {
- if (spawnOutput != null) {
- InputStreamReader isr = new InputStreamReader(spawnOutput);
- try {
- BufferedReader reader = buffered(isr);
- while (true) {
- String line = reader.readLine();
- if (line != null) {
- while (logBuffer.size() >= 100) {
- logBuffer.remove(0);
- }
- logBuffer.add(line);
- logUpdated.set(true);
- } else {
- break;
- }
- }
- } catch (Exception e) {
- // ignore
- }
- }
- } while (!quit.get());
+ // the process has ended: wait a little for the rest of its output, then
print what is kept
+ private void printTail(OutputTail tail) throws InterruptedException {
+ if (tail == null) {
+ return;
+ }
+ tail.awaitEnd(2000);
+ for (String line : tail.lines()) {
+ printer().println(line);
+ }
}
private void doRead(AtomicBoolean quit) {
@@ -680,6 +659,7 @@ public class Debug extends Run {
if (logLines > 0) {
// log last 10 lines
+ List<String> logBuffer = logTail != null ? logTail.lines() :
List.of();
int start = Math.max(logBuffer.size() - logLines, 0);
for (int i = start; i < start + logLines; i++) {
String line = "";
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/OutputTail.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/OutputTail.java
new file mode 100644
index 000000000000..10c93ea123df
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/OutputTail.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+
+/**
+ * Reads the output of a process (its stdout or stderr) on a thread of its own
until the end of the stream, and keeps
+ * the last lines.
+ * <p>
+ * The stream is read all the time, so the process never blocks on a full
pipe, and the thread ends at the end of the
+ * stream instead of reading it again.
+ */
+final class OutputTail {
+
+ private final int maxLines;
+ private final Deque<String> lines;
+ private final Runnable onLine;
+ private Thread thread;
+
+ /**
+ * @param maxLines the number of last lines to keep
+ * @param onLine called after each line that is read, or null
+ */
+ OutputTail(int maxLines, Runnable onLine) {
+ this.maxLines = maxLines;
+ this.lines = new ArrayDeque<>(maxLines);
+ this.onLine = onLine;
+ }
+
+ /**
+ * Starts to read the stream on a daemon thread with the given name. The
stream is closed at its end.
+ */
+ OutputTail start(InputStream in, String threadName) {
+ thread = new Thread(() -> read(in), threadName);
+ thread.setDaemon(true);
+ thread.start();
+ return this;
+ }
+
+ private void read(InputStream in) {
+ try (BufferedReader reader = new BufferedReader(new
InputStreamReader(in, Charset.defaultCharset()))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ add(line);
+ if (onLine != null) {
+ onLine.run();
+ }
+ }
+ } catch (IOException e) {
+ // the process is gone, and its pipe with it
+ }
+ }
+
+ private synchronized void add(String line) {
+ if (lines.size() >= maxLines) {
+ lines.removeFirst();
+ }
+ lines.addLast(line);
+ }
+
+ /**
+ * The lines kept so far, oldest first.
+ */
+ synchronized List<String> lines() {
+ return new ArrayList<>(lines);
+ }
+
+ /**
+ * Waits until the end of the stream is read, so the last lines of a
process that has ended are kept.
+ *
+ * @param millis the time to wait at most
+ * @return true when the end of the stream was read
+ */
+ boolean awaitEnd(long millis) throws InterruptedException {
+ if (thread == null) {
+ return true;
+ }
+ thread.join(millis);
+ return !thread.isAlive();
+ }
+}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/OutputTailTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/OutputTailTest.java
new file mode 100644
index 000000000000..b60ae6a92636
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/OutputTailTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * camel debug reads the output of the integration it spawned with {@link
OutputTail}: all the time, so the integration
+ * does not block on a full pipe, and only until the end of the stream
(CAMEL-27004: it read the ended stream again in a
+ * loop, at 100% CPU).
+ */
+class OutputTailTest {
+
+ @Test
+ void keepsTheLastLines() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 1; i <= 250; i++) {
+ sb.append("line ").append(i).append('\n');
+ }
+ AtomicInteger read = new AtomicInteger();
+ OutputTail tail = new OutputTail(100, read::incrementAndGet)
+ .start(new
ByteArrayInputStream(sb.toString().getBytes(Charset.defaultCharset())), "test");
+
+ assertThat(tail.awaitEnd(10_000)).isTrue();
+ assertThat(read).hasValue(250);
+ assertThat(tail.lines()).hasSize(100).startsWith("line
151").endsWith("line 250");
+ }
+
+ @Test
+ void endsAtTheEndOfTheStream() throws Exception {
+ PipedOutputStream out = new PipedOutputStream();
+ OutputTail tail = new OutputTail(10, null).start(new
PipedInputStream(out), "test");
+
+ out.write("started\n".getBytes(Charset.defaultCharset()));
+ out.flush();
+ // still open: the thread waits for more
+ assertThat(tail.awaitEnd(200)).isFalse();
+
+ out.close();
+ // the end of the stream: the thread ends, instead of reading the
stream again
+ assertThat(tail.awaitEnd(10_000)).isTrue();
+ assertThat(tail.lines()).containsExactly("started");
+ }
+
+ @Test
+ void noStartedThread() throws Exception {
+ OutputTail tail = new OutputTail(10, null);
+ assertThat(tail.awaitEnd(10)).isTrue();
+ assertThat(tail.lines()).isEmpty();
+ }
+
+ @Test
+ @DisabledOnOs(OS.WINDOWS)
+ void processWritingMoreThanAPipeHoldsDoesNotBlock() throws IOException,
InterruptedException {
+ // about 1 MB on stderr and on stdout, many times what the pipe of the
OS holds (64 KB)
+ Process p = new ProcessBuilder(
+ "sh", "-c",
+ "i=0; while [ $i -lt 20000 ]; do echo \"error line $i of the
integration\" >&2;"
+ + " echo \"log line $i of the integration\";
i=$((i+1)); done")
+ .start();
+ OutputTail log = new OutputTail(100, null).start(p.getInputStream(),
"test-log");
+ OutputTail error = new OutputTail(1000,
null).start(p.getErrorStream(), "test-error");
+
+ assertThat(p.waitFor(60, TimeUnit.SECONDS)).as("the process ends, as
both of its pipes are read").isTrue();
+ assertThat(log.awaitEnd(10_000)).isTrue();
+ assertThat(error.awaitEnd(10_000)).isTrue();
+ assertThat(log.lines()).hasSize(100).endsWith("log line 19999 of the
integration");
+ assertThat(error.lines()).hasSize(1000).endsWith("error line 19999 of
the integration");
+ }
+}