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 2044622f0c78 chore: camel-core - Routing Slip and Dynamic Router do
not copy the exchange of a step that continues asynchronously (#30541)
2044622f0c78 is described below
commit 2044622f0c785bbeb7708e9b7bd5ee3cf0665c0d
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 21 09:14:30 2026 +0100
chore: camel-core - Routing Slip and Dynamic Router do not copy the
exchange of a step that continues asynchronously (#30541)
The exchange was copied for the next step before checking whether the step
continued asynchronously. In that case the copy was thrown away, while
another
thread may already be processing the exchange: the copy read its message and
reset the stream cache of its body. The copy is now made only when the step
completed synchronously.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../org/apache/camel/processor/RoutingSlip.java | 7 +-
...AsyncEndpointRoutingSlipNoCopyInFlightTest.java | 130 +++++++++++++++++++++
2 files changed, 135 insertions(+), 2 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
index bfb2e86beb50..c7a860230108 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
@@ -285,7 +285,6 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
//process and prepare the routing slip
boolean sync = processExchange(endpoint, current, exchange,
originalCallback, iter, prototype);
- current = prepareExchangeForRoutingSlip(current, endpoint);
if (!sync) {
if (LOG.isTraceEnabled()) {
@@ -294,9 +293,12 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
}
// the remainder of the routing slip will be completed async
// so we break out now, then the callback will be invoked
which then continue routing from where we left here
+ // (do not copy the exchange, as another thread may be
processing it)
return false;
}
+ current = prepareExchangeForRoutingSlip(current, endpoint);
+
if (LOG.isTraceEnabled()) {
LOG.trace("Processing exchangeId: {} is continued being
processed synchronously", exchange.getExchangeId());
}
@@ -489,16 +491,17 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
// prepare and process the routing slip
final AsyncCallback cbNext = getNextCallback(prototype,
nextEndpoint, originalCallback);
boolean sync = processExchange(nextEndpoint, current,
original, cbNext, iter, prototype);
- current = prepareExchangeForRoutingSlip(current, nextEndpoint);
if (!sync) {
if (LOG.isTraceEnabled()) {
LOG.trace("Processing exchangeId: {} is continued
being processed asynchronously",
original.getExchangeId());
}
+ // do not copy the exchange, as another thread may be
processing it
return;
}
+ current = prepareExchangeForRoutingSlip(current, nextEndpoint);
ignoreInvalidEndpoint(current, nextEndpoint);
more = continueProcessing(current, "so breaking out of the
routing slip", LOG);
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRoutingSlipNoCopyInFlightTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRoutingSlipNoCopyInFlightTest.java
new file mode 100644
index 000000000000..f6ea40fcc888
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRoutingSlipNoCopyInFlightTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.processor.async;
+
+import java.io.ByteArrayInputStream;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProcessor;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.converter.stream.ByteArrayInputStreamCache;
+import org.apache.camel.support.DefaultAsyncProducer;
+import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.DefaultEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The routing slip must not copy the exchange of a step that continues
asynchronously, as another thread may be
+ * processing it (the copy resets the stream cache of the body that the step
may be reading).
+ */
+public class AsyncEndpointRoutingSlipNoCopyInFlightTest extends
ContextTestSupport {
+
+ private final AtomicBoolean inFlight = new AtomicBoolean();
+ private final AtomicInteger resetsInFlight = new AtomicInteger();
+ private final AtomicReference<Runnable> completion = new
AtomicReference<>();
+
+ @Test
+ public void testNoCopyWhileStepInFlight() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ ByteArrayInputStreamCache body = new ByteArrayInputStreamCache(new
ByteArrayInputStream("Hello".getBytes())) {
+ @Override
+ public void reset() {
+ if (inFlight.get()) {
+ resetsInFlight.incrementAndGet();
+ }
+ super.reset();
+ }
+ };
+
+ Exchange exchange =
context.getEndpoint("direct:start").createExchange();
+ exchange.getIn().setBody(body);
+ CountDownLatch done = new CountDownLatch(1);
+
+ // when process returns the step continues asynchronously, and the
routing slip has handed it over
+ AsyncProcessor processor = (AsyncProcessor)
context.getRoute("start").getConsumer().getProcessor();
+ boolean sync = processor.process(exchange, doneSync ->
done.countDown());
+ assertFalse(sync, "The step should continue asynchronously");
+ assertNotNull(completion.get(), "The step should be in flight");
+
+ completion.get().run();
+ assertTrue(done.await(10, TimeUnit.SECONDS), "The exchange should
complete");
+
+ assertMockEndpointsSatisfied();
+ assertEquals(0, resetsInFlight.get(), "The exchange of the step in
flight should not be copied");
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ context.addComponent("hold", new HoldComponent());
+
+ // no error handler, so the step is called directly and
continues on another thread
+ errorHandler(noErrorHandler());
+
+
from("direct:start").routeId("start").routingSlip(constant("hold:step,mock:result"));
+ }
+ };
+ }
+
+ private final class HoldComponent extends DefaultComponent {
+
+ @Override
+ protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) {
+ return new DefaultEndpoint(uri, this) {
+ @Override
+ public Producer createProducer() {
+ return new DefaultAsyncProducer(this) {
+ @Override
+ public boolean process(Exchange exchange,
AsyncCallback callback) {
+ inFlight.set(true);
+ completion.set(() -> new Thread(() -> {
+ inFlight.set(false);
+ callback.done(false);
+ }, "HoldStep").start());
+ return false;
+ }
+ };
+ }
+
+ @Override
+ public Consumer createConsumer(Processor processor) {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ }
+}