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 499b983cb1a2 CAMEL-27334: camel-netty - an exception on a channel 
without a request does not keep the next request on it from completing (#30627)
499b983cb1a2 is described below

commit 499b983cb1a243895abb0bc2cda3a71628d4d9ff
Author: allthingssecurity <[email protected]>
AuthorDate: Mon Nov 23 14:01:20 2026 +0530

    CAMEL-27334: camel-netty - an exception on a channel without a request does 
not keep the next request on it from completing (#30627)
    
    ClientChannelHandler.exceptionCaught set exceptionHandled before it looked 
for
    the state of a request. On a channel with no state (an unused channel in the
    producer pool, or one just borrowed) it returned with the flag set and the
    channel open, and the next request on it was never completed: its read
    timeout and the close of the channel both skip the callback when the flag is
    set. The flag is now set only when the state of a request was found.
    
    Found with a TLA+ model.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../netty/handlers/ClientChannelHandler.java       |  17 +-
 .../netty/NettyProducerStrayExceptionTest.java     | 172 +++++++++++++++++++++
 2 files changed, 183 insertions(+), 6 deletions(-)

diff --git 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
index f084826cb926..b6147e3dbc70 100644
--- 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
+++ 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
@@ -71,12 +71,19 @@ public class ClientChannelHandler extends 
SimpleChannelInboundHandler<Object> {
             // ignore subsequent exceptions being thrown
             return;
         }
-        exceptionHandled = true;
 
         Exchange exchange = null;
 
         NettyCamelState state = getState(ctx, cause);
-        if (state != null && state.isDone()) {
+        if (state == null) {
+            // no request on this channel yet (an idle channel in the producer 
pool, or one just borrowed): the next
+            // request on it must still be completed by its exception, its 
read timeout or the channel being closed,
+            // so this exception does not set exceptionHandled
+            LOG.debug("Exception caught at Channel: {} while no request has a 
state on it", ctx.channel(), cause);
+            return;
+        }
+        exceptionHandled = true;
+        if (state.isDone()) {
             // no request on this channel waits for a reply: the channel is 
idle in the producer pool (the server reset
             // it, for example), and the exchange of its last request is 
completed, and may still be routing, so it
             // must not get this exception
@@ -85,10 +92,8 @@ public class ClientChannelHandler extends 
SimpleChannelInboundHandler<Object> {
             NettyHelper.close(ctx.channel());
             return;
         }
-        if (state != null) {
-            state.onExceptionCaught();
-            exchange = state.getExchange();
-        }
+        state.onExceptionCaught();
+        exchange = state.getExchange();
 
         // the state may not be set
         if (exchange != null) {
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerStrayExceptionTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerStrayExceptionTest.java
new file mode 100644
index 000000000000..e44d1dee29dc
--- /dev/null
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerStrayExceptionTest.java
@@ -0,0 +1,172 @@
+/*
+ * 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.netty;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.handler.codec.TooLongFrameException;
+import io.netty.handler.timeout.ReadTimeoutException;
+import org.apache.camel.AsyncProducer;
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * An exception on a pooled channel that no request uses yet (here the server 
sends a line longer than
+ * decoderMaxLineLength to the channel that lazyChannelCreation=false opened 
at start) must not keep the next request on
+ * that channel from completing: by its request timeout, or when the server 
closes the connection.
+ */
+class NettyProducerStrayExceptionTest extends CamelTestSupport {
+
+    private final CountDownLatch sendGreeting = new CountDownLatch(1);
+    private final CountDownLatch requestReceived = new CountDownLatch(1);
+    private final CountDownLatch exceptionSeen = new CountDownLatch(1);
+    private ServerSocket server;
+    private Socket accepted;
+    private AsyncProducer producer;
+
+    @AfterEach
+    void cleanup() throws Exception {
+        ServiceHelper.stopService(producer);
+        if (accepted != null) {
+            accepted.close();
+        }
+        if (server != null) {
+            server.close();
+        }
+    }
+
+    @Test
+    void testRequestTimeoutAfterExceptionOnUnusedChannel() throws Exception {
+        startServer("x".repeat(200), false);
+        startProducerAndAwaitException("&requestTimeout=1000");
+
+        Exchange exchange = sendRequest();
+
+        assertInstanceOf(ReadTimeoutException.class, exchange.getException());
+    }
+
+    @Test
+    void testServerCloseAfterExceptionOnUnusedChannel() throws Exception {
+        startServer("x".repeat(200), true);
+        startProducerAndAwaitException("");
+
+        Exchange exchange = sendRequest();
+
+        assertInstanceOf(CamelExchangeException.class, 
exchange.getException());
+        assertTrue(exchange.getException().getMessage().startsWith("No 
response received from remote server"),
+                exchange.getException().getMessage());
+    }
+
+    @Test
+    void testRequestTimeoutWithoutException() throws Exception {
+        // the server sends nothing before the request, so there is no 
exception on the unused channel
+        startServer(null, false);
+        startProducer("&requestTimeout=1000");
+
+        Exchange exchange = sendRequest();
+
+        assertInstanceOf(ReadTimeoutException.class, exchange.getException());
+    }
+
+    private Exchange sendRequest() throws InterruptedException {
+        Exchange exchange = new DefaultExchange(context, 
ExchangePattern.InOut);
+        exchange.getMessage().setBody("hello");
+        CountDownLatch done = new CountDownLatch(1);
+        producer.process(exchange, doneSync -> done.countDown());
+        assertTrue(requestReceived.await(5, TimeUnit.SECONDS), "the server 
should get the request");
+        assertTrue(done.await(10, TimeUnit.SECONDS), "the exchange should be 
completed");
+        return exchange;
+    }
+
+    private Channel startProducer(String options) throws Exception {
+        producer = context.getEndpoint("netty:tcp://localhost:" + 
server.getLocalPort()
+                                       + 
"?textline=true&sync=true&lazyChannelCreation=false&decoderMaxLineLength=64"
+                                       + options)
+                .createAsyncProducer();
+        ServiceHelper.startService(producer);
+        // the channel the producer opened at start, unused in its pool (a 
listener of the connect future adds it to
+        // the group on the event loop, which may run just after the start 
returned)
+        NettyProducer nettyProducer = (NettyProducer) producer;
+        await().atMost(5, TimeUnit.SECONDS).until(() -> 
!nettyProducer.getAllChannels().isEmpty());
+        return nettyProducer.getAllChannels().iterator().next();
+    }
+
+    private void startProducerAndAwaitException(String options) throws 
Exception {
+        Channel channel = startProducer(options);
+        // see the exception before the producer's handler gets it, and count 
down once that handler is done with it
+        channel.pipeline().addBefore("handler", "exceptionProbe", new 
ChannelInboundHandlerAdapter() {
+            @Override
+            public void exceptionCaught(ChannelHandlerContext ctx, Throwable 
cause) {
+                ctx.fireExceptionCaught(cause);
+                if (cause instanceof TooLongFrameException) {
+                    exceptionSeen.countDown();
+                }
+            }
+        });
+        sendGreeting.countDown();
+        assertTrue(exceptionSeen.await(5, TimeUnit.SECONDS), "the long 
greeting should fail in the decoder");
+        assertTrue(channel.isActive(), "the channel stays open in the pool");
+    }
+
+    private void startServer(String greeting, boolean closeAfterRequest) 
throws Exception {
+        server = new ServerSocket(0);
+        Thread thread = new Thread(() -> {
+            try {
+                accepted = server.accept();
+                if (greeting != null) {
+                    sendGreeting.await(10, TimeUnit.SECONDS);
+                    OutputStream out = accepted.getOutputStream();
+                    out.write((greeting + 
"\n").getBytes(StandardCharsets.UTF_8));
+                    out.flush();
+                }
+                BufferedReader in = new BufferedReader(
+                        new InputStreamReader(accepted.getInputStream(), 
StandardCharsets.UTF_8));
+                if (in.readLine() != null) {
+                    requestReceived.countDown();
+                }
+                if (closeAfterRequest) {
+                    accepted.close();
+                }
+                // otherwise there is no reply
+            } catch (Exception e) {
+                // the test fails on its own latches
+            }
+        }, "NettyProducerStrayExceptionTestServer");
+        thread.setDaemon(true);
+        thread.start();
+    }
+}

Reply via email to