This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new 53a0ef8593b8 [backport camel-4.22.x] CAMEL-27470: camel-timer - a 
VirtualMachineError in a timer route stops every timer route with the same 
timer name (#30751)
53a0ef8593b8 is described below

commit 53a0ef8593b8ed72cb57f23977ba00003a8a439a
Author: Guillaume Nodet <[email protected]>
AuthorDate: Tue Nov 24 21:15:53 2026 +0100

    [backport camel-4.22.x] CAMEL-27470: camel-timer - a VirtualMachineError in 
a timer route stops every timer route with the same timer name (#30751)
    
    Backport of #30713.
    
    TimerConsumer.TimerTask.run() caught only Exception. When a 
VirtualMachineError
    (such as StackOverflowError) propagates out of run(), the java.util.Timer 
contract
    states that any unchecked Throwable from run() terminates the timer thread 
and
    silently cancels ALL tasks sharing that timer name — stopping every route 
that shares
    it, with no visible error. This is a long-standing issue in camel-timer, 
not a
    regression from CAMEL-26741. CAMEL-26741 made it visible in practice by 
re-throwing
    VirtualMachineError through the pipeline rather than swallowing it, but the 
same
    failure mode exists independently of that change.
    
    Fix: catch Throwable (after the existing catch Exception) in run(). Call 
cancel() on
    the failing task (cancels this route only, leaves the shared Timer thread 
alive) and
    log at ERROR with a hint to restart the route. Never re-throw from run(): 
any re-throw
    would still kill the shared Timer thread, defeating the fix.
    
    Also add catch Throwable in the executor path (negative delay), which had 
no catch at
    all. The executor is per-route so there is no shared Timer to protect, but 
logging
    the error is still valuable.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../camel/component/timer/TimerConsumer.java       | 40 ++++++++--
 .../TimerNegativeDelayVirtualMachineErrorTest.java | 63 ++++++++++++++++
 .../timer/TimerVirtualMachineErrorTest.java        | 86 ++++++++++++++++++++++
 3 files changed, 182 insertions(+), 7 deletions(-)

diff --git 
a/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
 
b/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
index 396909eb1ad5..4b55a1c29916 100644
--- 
a/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
+++ 
b/components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
@@ -138,6 +138,21 @@ public class TimerConsumer extends DefaultConsumer 
implements StartupListener, S
                         LOG.warn(
                                 "Error processing exchange. This exception 
will be ignored, to let the timer be able to trigger again.",
                                 e);
+                    } catch (Throwable t) {
+                        // An Error (such as StackOverflowError or 
OutOfMemoryError) must not propagate out
+                        // of run(): the java.util.Timer contract states that 
any unchecked Throwable from
+                        // run() terminates the timer thread and silently 
cancels ALL tasks sharing that
+                        // timer — including tasks from other routes with the 
same timer name. We therefore
+                        // catch Throwable rather than just Error: cancelling 
this task and logging is the
+                        // least-bad option in every case. For 
OutOfMemoryError the JVM may already be in
+                        // trouble, but allowing the error to propagate would 
additionally kill sibling
+                        // routes, which is strictly worse.
+                        cancel();
+                        LOG.error(
+                                "Fatal error processing exchange on timer: {}."
+                                  + " The timer task for this route has been 
cancelled."
+                                  + " The route no longer fires until it is 
restarted.",
+                                endpoint, t);
                     } finally {
                         polling = false;
                     }
@@ -198,6 +213,14 @@ public class TimerConsumer extends DefaultConsumer 
implements StartupListener, S
                         sendTimerExchange(count);
                         count = counter.incrementAndGet();
                     }
+                } catch (Throwable t) {
+                    // An Error (such as StackOverflowError) must not be 
silently swallowed in the executor
+                    // path. Log at ERROR so the operator sees it.
+                    LOG.error(
+                            "Fatal error processing exchange on timer: {}."
+                              + " The timer task for this route has been 
terminated."
+                              + " The route no longer fires until it is 
restarted.",
+                            endpoint, t);
                 } finally {
                     polling = false;
                 }
@@ -292,14 +315,17 @@ public class TimerConsumer extends DefaultConsumer 
implements StartupListener, S
             AsyncCallback cb = defaultConsumerCallback(exchange, false);
             getAsyncProcessor().process(exchange, cb);
         } else {
+            // Use an outer try-finally so that releaseExchange is always 
called,
+            // even if an Error (e.g. StackOverflowError) escapes 
getProcessor().process().
+            // Without this, an Error would leave the exchange inflight and 
cause
+            // the context shutdown to wait for the full shutdown timeout.
             try {
-                getProcessor().process(exchange);
-            } catch (Exception e) {
-                exchange.setException(e);
-            }
-
-            // handle any thrown exception
-            try {
+                try {
+                    getProcessor().process(exchange);
+                } catch (Exception e) {
+                    exchange.setException(e);
+                }
+                // handle any thrown exception
                 if (exchange.getException() != null) {
                     getExceptionHandler().handleException("Error processing 
exchange", exchange, exchange.getException());
                 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/timer/TimerNegativeDelayVirtualMachineErrorTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/timer/TimerNegativeDelayVirtualMachineErrorTest.java
new file mode 100644
index 000000000000..b5bb7d4ef1f5
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/timer/TimerNegativeDelayVirtualMachineErrorTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.component.timer;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Verifies that a VirtualMachineError (StackOverflowError) thrown in a timer 
route with a negative delay (executor
+ * path) is caught and logged, and that the route's fire loop terminates 
rather than propagating the error.
+ *
+ * @see <a 
href="https://issues.apache.org/jira/browse/CAMEL-27470";>CAMEL-27470</a>
+ */
+class TimerNegativeDelayVirtualMachineErrorTest extends ContextTestSupport {
+
+    /** Open once route A has thrown its Error, confirming the error was 
processed, not silently lost. */
+    private final CountDownLatch errorThrown = new CountDownLatch(1);
+
+    @Test
+    void negativeDelayRouteLoopTerminatesOnVirtualMachineError() throws 
InterruptedException {
+        // The executor path catches the Throwable: the loop must terminate 
and the
+        // latch must open. If the error propagated uncaught and killed the 
thread,
+        // the latch still opens because the lambda completes — so the main 
assertion
+        // is that this finishes in time (not that it hangs).
+        boolean thrown = errorThrown.await(5, TimeUnit.SECONDS);
+        assertTrue(thrown, "executor path must catch the StackOverflowError 
and not hang");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("timer:negDelay?delay=-1&period=0&synchronous=true")
+                        .routeId("route-neg-delay")
+                        .process(exchange -> {
+                            errorThrown.countDown();
+                            throw new StackOverflowError("simulated 
StackOverflowError in negative-delay route");
+                        });
+            }
+        };
+    }
+}
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/timer/TimerVirtualMachineErrorTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/timer/TimerVirtualMachineErrorTest.java
new file mode 100644
index 000000000000..61e57b2bbe89
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/timer/TimerVirtualMachineErrorTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.component.timer;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Verifies that a VirtualMachineError (StackOverflowError) thrown in one 
timer route does not kill the shared
+ * java.util.Timer thread and stop other routes sharing the same timer name.
+ *
+ * @see <a 
href="https://issues.apache.org/jira/browse/CAMEL-27470";>CAMEL-27470</a>
+ */
+public class TimerVirtualMachineErrorTest extends ContextTestSupport {
+
+    private static final String TIMER_NAME = "shared";
+
+    /** Counts how many times route A has fired (used to trigger the error on 
the third). */
+    private final AtomicInteger routeAFireCount = new AtomicInteger();
+    /** Set to true once route A has thrown its Error. */
+    private volatile boolean routeAHasThrown;
+    /** Open once route B fires at least once after route A has already thrown 
its Error. */
+    private final CountDownLatch routeBFiredAfterError = new CountDownLatch(1);
+
+    @Test
+    public void sharedTimerSurvivesStackOverflowErrorInSiblingRoute() throws 
InterruptedException {
+        // Wait up to 5 seconds for route B to fire at least once after route 
A has thrown.
+        // Before the fix this never happens: the StackOverflowError escapes 
run() and kills
+        // the shared java.util.Timer thread, which silently cancels both 
timer tasks.
+        boolean firedAfterError = routeBFiredAfterError.await(5, 
TimeUnit.SECONDS);
+        assertTrue(firedAfterError,
+                "route B must keep firing after a StackOverflowError in route 
A (shared timer must survive)");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // Route A: throws a StackOverflowError on its third exchange.
+                // Uses synchronous=true so the error propagates synchronously 
through the timer task.
+                from("timer:" + TIMER_NAME + "?period=50&synchronous=true")
+                        .routeId("route-a")
+                        .process(exchange -> {
+                            int count = routeAFireCount.incrementAndGet();
+                            if (count >= 3) {
+                                routeAHasThrown = true;
+                                throw new StackOverflowError("simulated 
StackOverflowError in route A");
+                            }
+                        });
+
+                // Route B: a bystander on the same timer. It must keep firing 
even after route A
+                // throws its StackOverflowError, because the fix prevents the 
error from killing
+                // the shared java.util.Timer thread.
+                from("timer:" + TIMER_NAME + "?period=50&synchronous=true")
+                        .routeId("route-b")
+                        .process(exchange -> {
+                            if (routeAHasThrown) {
+                                routeBFiredAfterError.countDown();
+                            }
+                        });
+            }
+        };
+    }
+}

Reply via email to