You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/10/07 19:37:16 UTC

(pulsar) 02/03: [fix][proxy] Fix direct memory leaks from dropped reference-counted messages (#26811)

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

lhotari pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 81c2b8d99313968b1c8a38769d49ec0fe0c091a1
Author: Radiance' 波波 <39...@users.noreply.github.com>
AuthorDate: Thu Oct 8 00:32:05 2026 +0800

    [fix][proxy] Fix direct memory leaks from dropped reference-counted messages (#26811)
    
    (cherry picked from commit 11e5e002fdd51d6f17d62a3bf9fd1477dc7c5810)
---
 .../protocol/OptionalProxyProtocolDecoder.java     |  13 ++-
 .../protocol/OptionalProxyProtocolDecoderTest.java |  46 +++++++++
 .../pulsar/proxy/server/ProxyConnection.java       |  11 ++
 .../pulsar/proxy/server/ProxyConnectionTest.java   | 111 +++++++++++++++++++++
 4 files changed, 177 insertions(+), 4 deletions(-)

diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoder.java b/pulsar-common/src/main/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoder.java
index b4e15f8cd1d7..fcd8c5f21554 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoder.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoder.java
@@ -26,6 +26,7 @@ import io.netty.handler.codec.ProtocolDetectionResult;
 import io.netty.handler.codec.ProtocolDetectionState;
 import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
 import io.netty.handler.codec.haproxy.HAProxyProtocolVersion;
+import io.netty.util.ReferenceCountUtil;
 import lombok.extern.slf4j.Slf4j;
 
 /**
@@ -72,10 +73,14 @@ public class OptionalProxyProtocolDecoder extends ChannelInboundHandlerAdapter {
 
     @Override
     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
-        super.channelInactive(ctx);
-        if (cumulatedByteBuf != null) {
-            log.info("Release cumulated byte buffer when channel inactive.");
-            cumulatedByteBuf = null;
+        try {
+            super.channelInactive(ctx);
+        } finally {
+            if (cumulatedByteBuf != null) {
+                log.info("Release cumulated byte buffer when channel inactive.");
+                ReferenceCountUtil.safeRelease(cumulatedByteBuf);
+                cumulatedByteBuf = null;
+            }
         }
     }
 }
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoderTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoderTest.java
new file mode 100644
index 000000000000..38e50677f926
--- /dev/null
+++ b/pulsar-common/src/test/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoderTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.pulsar.common.protocol;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.embedded.EmbeddedChannel;
+import io.netty.util.ReferenceCountUtil;
+import org.testng.annotations.Test;
+
+public class OptionalProxyProtocolDecoderTest {
+
+    @Test
+    public void incompleteProxyProtocolBufferIsReleasedWhenChannelCloses() {
+        EmbeddedChannel channel = new EmbeddedChannel(new OptionalProxyProtocolDecoder());
+        ByteBuf incompleteHeader = Unpooled.directBuffer(
+                OptionalProxyProtocolDecoder.MIN_BYTES_SIZE_TO_DETECT_PROTOCOL - 1).writeZero(1);
+        try {
+            channel.writeInbound(incompleteHeader);
+            channel.close();
+            assertThat(incompleteHeader.refCnt())
+                    .as("the accumulated HAProxy detection buffer must be released when the channel closes")
+                    .isZero();
+        } finally {
+            ReferenceCountUtil.safeRelease(incompleteHeader);
+            channel.finishAndReleaseAll();
+        }
+    }
+}
diff --git a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java
index 9271319fe136..aee873eb6dcf 100644
--- a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java
+++ b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java
@@ -19,6 +19,7 @@
 package org.apache.pulsar.proxy.server;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import com.google.common.annotations.VisibleForTesting;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFutureListener;
@@ -29,6 +30,7 @@ import io.netty.channel.epoll.EpollSocketChannel;
 import io.netty.handler.codec.haproxy.HAProxyMessage;
 import io.netty.handler.ssl.SslHandler;
 import io.netty.resolver.dns.DnsAddressResolverGroup;
+import io.netty.util.ReferenceCountUtil;
 import io.netty.util.concurrent.ScheduledFuture;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
@@ -161,6 +163,11 @@ public class ProxyConnection extends PulsarHandler {
         return connectionPool;
     }
 
+    @VisibleForTesting
+    void setStateForTesting(State state) {
+        this.state = state;
+    }
+
     public ProxyConnection(ProxyService proxyService, DnsAddressResolverGroup dnsAddressResolverGroup) {
         super(proxyService.getConfiguration().getKeepAliveIntervalSeconds(), TimeUnit.SECONDS);
         this.service = proxyService;
@@ -278,6 +285,7 @@ public class ProxyConnection extends PulsarHandler {
     public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
         if (msg instanceof HAProxyMessage) {
             haProxyMessage = (HAProxyMessage) msg;
+            haProxyMessage.release();
             return;
         }
         switch (state) {
@@ -317,14 +325,17 @@ public class ProxyConnection extends PulsarHandler {
                 LOG.warn("Received message of type {} while connection to broker is missing in state {}. "
                                 + "Dropping the input message (readable bytes={}).", msg.getClass(), state,
                         msg instanceof ByteBuf ? ((ByteBuf) msg).readableBytes() : -1);
+                ReferenceCountUtil.safeRelease(msg);
             }
             break;
         case ProxyConnectingToBroker:
             LOG.warn("Received message of type {} while connecting to broker. "
                             + "Dropping the input message (readable bytes={}).", msg.getClass(),
                     msg instanceof ByteBuf ? ((ByteBuf) msg).readableBytes() : -1);
+            ReferenceCountUtil.safeRelease(msg);
             break;
         default:
+            ReferenceCountUtil.safeRelease(msg);
             break;
         }
     }
diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java
index 1abba6d6d8c7..9046798cd951 100644
--- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java
+++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java
@@ -18,11 +18,25 @@
  */
 package org.apache.pulsar.proxy.server;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.haproxy.HAProxyCommand;
+import io.netty.handler.codec.haproxy.HAProxyMessage;
+import io.netty.handler.codec.haproxy.HAProxyProtocolVersion;
+import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol;
+import io.netty.handler.codec.haproxy.HAProxyTLV;
+import io.netty.util.ReferenceCountUtil;
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import org.apache.pulsar.client.impl.conf.ClientConfigurationData;
 import org.testng.annotations.Test;
 
@@ -59,4 +73,101 @@ public class ProxyConnectionTest {
         clientConfiguration = proxyConnection.createClientConfiguration();
         assertEquals(clientConfiguration.getServiceUrl(), proxyUrl);
     }
+
+    @Test
+    public void droppedInboundMessageIsReleasedWhenConnectionIsClosing() throws Exception {
+        ProxyService proxyService = mock(ProxyService.class);
+        doReturn(new ProxyConfiguration()).when(proxyService).getConfiguration();
+        ProxyConnection proxyConnection = new ProxyConnection(proxyService, null);
+        ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+        Channel channel = mock(Channel.class);
+        doReturn(channel).when(context).channel();
+        doReturn(false).when(channel).isOpen();
+
+        proxyConnection.exceptionCaught(context, new IllegalStateException("test"));
+        ByteBuf message = Unpooled.directBuffer(1).writeByte(1);
+        try {
+            proxyConnection.channelRead(context, message);
+            assertThat(message.refCnt())
+                    .as("a message dropped by the closing proxy connection must be released")
+                    .isZero();
+        } finally {
+            ReferenceCountUtil.safeRelease(message);
+        }
+    }
+
+    @Test
+    public void droppedInboundMessageIsReleasedWhileConnectingToBroker() throws Exception {
+        ProxyService proxyService = mock(ProxyService.class);
+        doReturn(new ProxyConfiguration()).when(proxyService).getConfiguration();
+        ProxyConnection proxyConnection = new ProxyConnection(proxyService, null);
+        proxyConnection.setStateForTesting(ProxyConnection.State.ProxyConnectingToBroker);
+        ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+        ByteBuf message = Unpooled.directBuffer(1).writeByte(1);
+        try {
+            proxyConnection.channelRead(context, message);
+            assertThat(message.refCnt())
+                    .as("a message dropped while connecting to a broker must be released")
+                    .isZero();
+        } finally {
+            ReferenceCountUtil.safeRelease(message);
+        }
+    }
+
+    @Test
+    public void droppedInboundMessageIsReleasedWhenBrokerHandlerIsMissing() throws Exception {
+        ProxyService proxyService = mock(ProxyService.class);
+        doReturn(new ProxyConfiguration()).when(proxyService).getConfiguration();
+        ProxyConnection proxyConnection = new ProxyConnection(proxyService, null);
+        proxyConnection.setStateForTesting(ProxyConnection.State.ProxyConnectionToBroker);
+        ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+        ByteBuf message = Unpooled.directBuffer(1).writeByte(1);
+        try {
+            proxyConnection.channelRead(context, message);
+            assertThat(message.refCnt())
+                    .as("a message dropped without a broker handler must be released")
+                    .isZero();
+        } finally {
+            ReferenceCountUtil.safeRelease(message);
+        }
+    }
+
+    @Test
+    public void receivedHAProxyMessageIsReleasedAndRetainedMetadataRemainsAvailable() throws Exception {
+        ProxyService proxyService = mock(ProxyService.class);
+        doReturn(new ProxyConfiguration()).when(proxyService).getConfiguration();
+        Set<ProxyConnection> clientConnections = Collections.newSetFromMap(new ConcurrentHashMap<>());
+        doReturn(clientConnections).when(proxyService).getClientCnxs();
+        ProxyConnection proxyConnection = new ProxyConnection(proxyService, null);
+        ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+        ByteBuf tlvContent = Unpooled.directBuffer(3).writeBytes(new byte[] {'h', '2', 'c'});
+        HAProxyTLV tlv = new HAProxyTLV(HAProxyTLV.Type.PP2_TYPE_ALPN, tlvContent);
+        HAProxyMessage message = new HAProxyMessage(
+                HAProxyProtocolVersion.V2,
+                HAProxyCommand.PROXY,
+                HAProxyProxiedProtocol.TCP4,
+                "192.0.2.1",
+                "192.0.2.2",
+                1234,
+                6650,
+                Collections.singletonList(tlv));
+        try {
+            proxyConnection.channelRead(context, message);
+            assertThat(message.refCnt())
+                    .as("the HAProxy message must be released as soon as the proxy stores it")
+                    .isZero();
+            assertThat(tlvContent.refCnt())
+                    .as("the HAProxy v2 TLV buffer must be released with the message")
+                    .isZero();
+            assertThat(proxyConnection.hasHAProxyMessage()).isTrue();
+            HAProxyMessage retainedMessage = proxyConnection.getHAProxyMessage();
+            assertThat(retainedMessage.sourceAddress()).isEqualTo("192.0.2.1");
+            assertThat(retainedMessage.destinationAddress()).isEqualTo("192.0.2.2");
+            assertThat(retainedMessage.sourcePort()).isEqualTo(1234);
+            assertThat(retainedMessage.destinationPort()).isEqualTo(6650);
+            assertThat(retainedMessage.proxiedProtocol()).isEqualTo(HAProxyProxiedProtocol.TCP4);
+        } finally {
+            ReferenceCountUtil.safeRelease(message);
+        }
+    }
 }