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 f4bb678962d0 CAMEL-26930: camel-jbang - camel run with --jvm-args or
--jvm-debug stops the JVM it started when it is killed (#29973)
f4bb678962d0 is described below
commit f4bb678962d073dc79ba2ec040b7c81840294786
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 10:00:03 2026 +0100
CAMEL-26930: camel-jbang - camel run with --jvm-args or --jvm-debug stops
the JVM it started when it is killed (#29973)
The first point of item 7 of
[CAMEL-26930](https://issues.apache.org/jira/browse/CAMEL-26930).
## Problem
`camel run --jvm-args=...` and `--jvm-debug` (and a run of another Camel
version) run the integration in a JVM of its own, in the foreground, and wait
for it. Ctrl+C reaches both JVMs, but a kill of `camel run` (SIGTERM, as a
supervisor or an agent sends it) reaches only the CLI JVM, and nothing stopped
the child: it went on running without its parent.
On the launcher, `camel run r.camel.yaml --jvm-args=-Xmx256m`, then `kill
-TERM` of the CLI JVM:
```
main: cli gone child ALIVE (orphan), still logging "tick"
this branch: cli gone child gone
```
## Change
`Run.waitForChild` waits for the child with a shutdown hook that stops it
(`ChildProcess.stop`: gracefully, then forcibly after 30 seconds), as the
Spring Boot, Quarkus and Camel Main runners and the example run do already, and
removes the hook when the child ended by itself. It is used by the three
foreground paths: `runLauncherJvm` (`--jvm-args`, `--jvm-debug`),
`runCamelVersion` and `runCustomCamelVersion`.
## Tests
- `RunChildStopTest` (new): the hook stops a running child process (not on
Windows, which has no `sleep`).
- End to end with the launcher, before and after, as above.
- camel-jbang-run: 319 pass. camel-jbang-core: 3070 + 264 pass.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/dsl/jbang/core/commands/Run.java | 38 ++++++++++++-----
.../dsl/jbang/core/commands/RunChildStopTest.java | 47 ++++++++++++++++++++++
2 files changed, 76 insertions(+), 9 deletions(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 2ba1369d5658..f1c31a7eaffe 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -68,6 +68,7 @@ import org.apache.camel.dsl.jbang.run.RunProfileProperties;
import org.apache.camel.dsl.jbang.run.RunRequest;
import org.apache.camel.dsl.jbang.run.RunSettings;
import org.apache.camel.dsl.jbang.run.RunningIntegration;
+import org.apache.camel.dsl.jbang.run.common.ChildProcess;
import org.apache.camel.dsl.jbang.run.common.ExportHelper;
import org.apache.camel.dsl.jbang.run.common.Printer;
import org.apache.camel.dsl.jbang.run.common.RuntimeType;
@@ -1311,10 +1312,8 @@ public class Run extends CamelCommand implements
CommandLifecycle {
return runBackgroundProcess(pb, "Camel Main");
} else {
pb.inheritIO(); // run in foreground (with IO so logs are visible)
- Process p = pb.start();
- this.spawnPid = p.pid();
// wait for that process to exit as we run in foreground
- return p.waitFor();
+ return waitForChild(pb.start());
}
}
@@ -1337,10 +1336,8 @@ public class Run extends CamelCommand implements
CommandLifecycle {
return runBackgroundProcess(pb, "Camel Main");
} else {
pb.inheritIO(); // run in foreground (with IO so logs are visible)
- Process p = pb.start();
- this.spawnPid = p.pid();
// wait for that process to exit as we run in foreground
- return p.waitFor();
+ return waitForChild(pb.start());
}
}
@@ -1664,13 +1661,36 @@ public class Run extends CamelCommand implements
CommandLifecycle {
return runBackgroundProcess(pb, "Camel Main");
} else {
pb.inheritIO(); // run in foreground (with IO so logs are visible)
- Process p = pb.start();
- this.spawnPid = p.pid();
// wait for that process to exit as we run in foreground
- return p.waitFor();
+ return waitForChild(pb.start());
+ }
+ }
+
+ /**
+ * Waits for the JVM this run started in the foreground, and stops it when
this JVM is stopped, such as by a kill of
+ * its pid (SIGTERM). Ctrl+C reaches both, but a kill reaches this JVM
only, and the child went on running without
+ * its parent (CAMEL-26930).
+ */
+ private int waitForChild(Process process) throws InterruptedException {
+ this.spawnPid = process.pid();
+ Thread hook = childStopHook(process);
+ Runtime.getRuntime().addShutdownHook(hook);
+ try {
+ return process.waitFor();
+ } finally {
+ try {
+ Runtime.getRuntime().removeShutdownHook(hook);
+ } catch (IllegalStateException e) {
+ // shutting down already
+ }
}
}
+ /** The shutdown hook that stops the JVM this run started: gracefully,
then forcibly after a while. */
+ static Thread childStopHook(Process process) {
+ return new Thread(() -> ChildProcess.stop(process,
ChildProcess.STOP_SECONDS), "CamelChildProcessStop");
+ }
+
/**
* Runs Camel in this JVM, which the run configured, and waits for it to
end.
*/
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunChildStopTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunChildStopTest.java
new file mode 100644
index 000000000000..13e786035e0d
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunChildStopTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.util.concurrent.TimeUnit;
+
+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 run with --jvm-args or --jvm-debug runs the integration in a JVM of
its own, in the foreground. A kill of camel
+ * run (SIGTERM) left that JVM running without its parent (CAMEL-26930); it is
now stopped by a shutdown hook.
+ */
+class RunChildStopTest {
+
+ @Test
+ @DisabledOnOs(OS.WINDOWS)
+ void theHookStopsTheChild() throws Exception {
+ Process child = new ProcessBuilder("sleep", "60").start();
+ try {
+ Thread hook = Run.childStopHook(child);
+ hook.start();
+ hook.join(TimeUnit.SECONDS.toMillis(20));
+
+ assertThat(child.waitFor(10, TimeUnit.SECONDS)).as("the child is
stopped").isTrue();
+ } finally {
+ child.destroyForcibly();
+ }
+ }
+}