rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

[Bug] NullPointerException in NettyRemotingClient.NettyConnectManageHandler.channelActive due to unchecked null channel

Open UnZHGPPT opened this issue 10 months ago • 2 comments

Before Creating the Bug Report

  • [x] I found a bug, not just asking a question, which should be created in GitHub Discussions.

  • [x] I have searched the GitHub Issues and GitHub Discussions of this repository and believe that this is not a duplicate.

  • [x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.

Runtime platform environment

OS name: "linux", version: "5.15.0-138-generic", arch: "amd64", family: "unix"

RocketMQ version

rocketmq-all-5.4.0

JDK Version

Apache Maven 3.9.1 Java version: 1.8.0_452, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre

Describe the Bug

In the method NettyRemotingClient.NettyConnectManageHandler.channelActive(ChannelHandlerContext ctx), the code directly accesses ctx.channel() and calls .id() on it without verifying that the channel is non-null:

LOGGER.info("NETTY CLIENT PIPELINE: ACTIVE, {}, channelId={}", remoteAddress, ctx.channel().id());

However, if the ChannelHandlerContext is constructed with a null channel (e.g., via SimpleChannelHandlerContext), ctx.channel() returns null, leading to a NullPointerException when invoking .id().

This also affects the constructor of SimpleChannelHandlerContext, which does not validate its input channel, allowing null propagation.

Steps to Reproduce

Test Code:

package org.apache.rocketmq.remoting.netty;
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
import io.netty.channel.DefaultChannelId;
import io.netty.channel.ChannelHandler;
import org.apache.rocketmq.remoting.netty.NettyRemotingClient;
import org.junit.Test;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import org.apache.rocketmq.proxy.service.channel.SimpleChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;

public class TestClass {
    @Test(timeout=3000)
    public void test() throws Throwable {
        NettyClientConfig nettyClientConfig0 = new NettyClientConfig();
        NettyRemotingClient nettyRemotingClient0 = new NettyRemotingClient(nettyClientConfig0);
        NettyRemotingClient.NettyConnectManageHandler nettyRemotingClient_NettyConnectManageHandler0 = nettyRemotingClient0.new NettyConnectManageHandler();
        DefaultChannelId defaultChannelId0 = DefaultChannelId.newInstance();
        ChannelHandler[] channelHandlerArray0 = new ChannelHandler[0];
        EmbeddedChannel embeddedChannel0 = null;
        SimpleChannelHandlerContext simpleChannelHandlerContext0 = new SimpleChannelHandlerContext(embeddedChannel0);
        nettyRemotingClient_NettyConnectManageHandler0.channelActive(simpleChannelHandlerContext0);
    }
}

Execution Result

JUnit version 4.13.2
.E
Time: 0.856
There was 1 failure:
1) test(org.apache.rocketmq.broker.schedule.TestClass)
java.lang.NullPointerException
	at org.apache.rocketmq.broker.schedule.ScheduleMessageService$HandlePutResultTask.run(ScheduleMessageService.java:562)
	at org.apache.rocketmq.broker.schedule.TestClass.test(TestClass.java:22)

FAILURES!!!
Tests run: 1,  Failures: 1

What Did You Expect to See?

The channelActive method should gracefully handle cases where the channel is null (e.g., log a warning or skip processing), or SimpleChannelHandlerContext should reject null channels at construction time.

What Did You See Instead?

A NullPointerException is thrown:

java.lang.NullPointerException
	at org.apache.rocketmq.broker.schedule.ScheduleMessageService$HandlePutResultTask.run(ScheduleMessageService.java:562)
	at org.apache.rocketmq.broker.schedule.TestClass.test(TestClass.java:22)

Additional Context

I think maybe we can add a null check or default-init in the SimpleChannelHandlerContext(...)?

UnZHGPPT avatar Jan 15 '26 11:01 UnZHGPPT

It's worth noting that the issue is not limited to ScheduleMessageService$HandlePutResultTask.run. Several other methods in the Netty client handler are also vulnerable to NullPointerException when the channel is null.

Such as,

  • NettyRemotingClient$NettyConnectManageHandler.exceptionCaught()
  • NettyRemotingClient$NettyConnectManageHandler.close().

UnZHGPPT avatar Jan 15 '26 11:01 UnZHGPPT

Thanks for the suggestion. However, according to Netty's design and guarantees, the ChannelHandlerContext (ctx) is always tied to a Channel. The channelRead event can only be triggered on an active, existing channel. Therefore, ctx.channel() is guaranteed to be non-null in this context. Adding a null check would be redundant.

sinberCS avatar Jan 19 '26 09:01 sinberCS