Skip to content

Commit af2625c

Browse files
zrlwEarthChen
andauthored
Fix uninitialized non-static final fields potential NPE issue in dubbo-remoting module (#15602)
* Fix uninitialized final variables issue in dubbo-remoting * Remove redundant initializations of AbstractPortUnificationServer fields * Change modifier of AbstractTimerTask start method --------- Co-authored-by: earthchen <earthchen1996@gmail.com>
1 parent ed65284 commit af2625c

12 files changed

Lines changed: 47 additions & 27 deletions

File tree

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/pu/AbstractPortUnificationServer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public abstract class AbstractPortUnificationServer extends AbstractServer {
4444
protocol name --> URL object
4545
wire protocol will get url object to config server pipeline for channel
4646
*/
47-
private final Map<String, URL> supportedUrls = new ConcurrentHashMap<>();
47+
private Map<String, URL> supportedUrls;
4848

4949
/*
5050
protocol name --> ChannelHandler object
5151
wire protocol will get handler to config server pipeline for channel
5252
(for triple protocol, it's a default handler that do nothing)
5353
*/
54-
private final Map<String, ChannelHandler> supportedHandlers = new ConcurrentHashMap<>();
54+
private Map<String, ChannelHandler> supportedHandlers;
5555

5656
public AbstractPortUnificationServer(URL url, ChannelHandler handler) throws RemotingException {
5757
super(url, handler);
@@ -63,6 +63,10 @@ public Map<String, WireProtocol> getProtocols() {
6363

6464
@Override
6565
protected final void doOpen() {
66+
// initialize supportedUrls and supportedHandlers before potential usage to avoid NPE.
67+
supportedUrls = new ConcurrentHashMap<>();
68+
supportedHandlers = new ConcurrentHashMap<>();
69+
6670
ExtensionLoader<WireProtocol> loader =
6771
getUrl().getOrDefaultFrameworkModel().getExtensionLoader(WireProtocol.class);
6872
Map<String, WireProtocol> protocols = loader.getActivateExtension(getUrl(), new String[0]).stream()

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class AbstractTimerTask implements TimerTask {
4242
this.channelProvider = channelProvider;
4343
this.hashedWheelTimer = hashedWheelTimer;
4444
this.tick = tick;
45-
start();
45+
// do not start here because inheritor should set additional timeout parameters before doing task.
4646
}
4747

4848
static Long lastRead(Channel channel) {
@@ -57,7 +57,7 @@ static Long now() {
5757
return System.currentTimeMillis();
5858
}
5959

60-
private void start() {
60+
protected void start() {
6161
this.timeout = hashedWheelTimer.newTimeout(this, tick, TimeUnit.MILLISECONDS);
6262
}
6363

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/CloseTimerTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public CloseTimerTask(
3434
ChannelProvider channelProvider, HashedWheelTimer hashedWheelTimer, Long tick, int closeTimeout) {
3535
super(channelProvider, hashedWheelTimer, tick);
3636
this.closeTimeout = closeTimeout;
37+
start();
3738
}
3839

3940
@Override

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeartbeatTimerTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class HeartbeatTimerTask extends AbstractTimerTask {
3636
ChannelProvider channelProvider, HashedWheelTimer hashedWheelTimer, Long heartbeatTick, int heartbeat) {
3737
super(channelProvider, hashedWheelTimer, heartbeatTick);
3838
this.heartbeat = heartbeat;
39+
start();
3940
}
4041

4142
@Override

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public ReconnectTimerTask(
3838
int idleTimeout) {
3939
super(channelProvider, hashedWheelTimer, heartbeatTimeoutTick);
4040
this.idleTimeout = idleTimeout;
41+
start();
4142
}
4243

4344
@Override

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
public abstract class AbstractClient extends AbstractEndpoint implements Client {
5252

53-
private final Lock connectLock = new ReentrantLock();
53+
private Lock connectLock;
5454

5555
private final boolean needReconnect;
5656

@@ -64,6 +64,10 @@ public abstract class AbstractClient extends AbstractEndpoint implements Client
6464

6565
public AbstractClient(URL url, ChannelHandler handler) throws RemotingException {
6666
super(url, handler);
67+
68+
// initialize connectLock before calling connect()
69+
connectLock = new ReentrantLock();
70+
6771
// set default needReconnect true when channel is not connected
6872
needReconnect = url.getParameter(Constants.SEND_RECONNECT_KEY, true);
6973

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/MultiMessageHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public MultiMessageHandler(ChannelHandler handler) {
3737
super(handler);
3838
}
3939

40-
@SuppressWarnings("unchecked")
4140
@Override
4241
public void received(Channel channel, Object message) throws RemotingException {
4342
if (message instanceof MultiMessage) {

dubbo-remoting/dubbo-remoting-http3/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyHttp3Server.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,23 @@ public class NettyHttp3Server extends AbstractServer {
5757
private EventLoopGroup bossGroup;
5858
private io.netty.channel.Channel channel;
5959

60-
private final Consumer<ChannelPipeline> pipelineConfigurator;
61-
private final int serverShutdownTimeoutMills;
60+
private Consumer<ChannelPipeline> pipelineConfigurator;
61+
private int serverShutdownTimeoutMills;
6262

63-
@SuppressWarnings("unchecked")
6463
public NettyHttp3Server(URL url, ChannelHandler handler) throws RemotingException {
6564
super(url, ChannelHandlers.wrap(handler, url));
66-
pipelineConfigurator = (Consumer<ChannelPipeline>) getUrl().getAttribute(PIPELINE_CONFIGURATOR_KEY);
67-
Objects.requireNonNull(pipelineConfigurator, "pipelineConfigurator should be set");
68-
serverShutdownTimeoutMills = ConfigurationUtils.getServerShutdownTimeout(getUrl().getOrDefaultModuleModel());
6965
}
7066

7167
@Override
68+
@SuppressWarnings("unchecked")
7269
protected void doOpen() throws Throwable {
7370
bootstrap = new Bootstrap();
71+
72+
// initialize pipelineConfigurator and serverShutdownTimeoutMills before potential usage to avoid NPE.
73+
pipelineConfigurator = (Consumer<ChannelPipeline>) getUrl().getAttribute(PIPELINE_CONFIGURATOR_KEY);
74+
Objects.requireNonNull(pipelineConfigurator, "pipelineConfigurator should be set");
75+
serverShutdownTimeoutMills = ConfigurationUtils.getServerShutdownTimeout(getUrl().getOrDefaultModuleModel());
76+
7477
bossGroup = NettyEventLoopFactory.eventLoopGroup(1, EVENT_LOOP_BOSS_POOL_NAME);
7578
NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
7679
channels = nettyServerHandler.getChannels();

dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyPortUnificationServer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.ArrayList;
3333
import java.util.Collection;
3434
import java.util.Map;
35-
import java.util.concurrent.ConcurrentHashMap;
3635
import java.util.concurrent.ExecutorService;
3736
import java.util.concurrent.Executors;
3837

@@ -56,7 +55,8 @@
5655
*/
5756
public class NettyPortUnificationServer extends AbstractPortUnificationServer {
5857

59-
private Map<String, Channel> dubboChannels = new ConcurrentHashMap<>(); // <ip:port, channel>
58+
// <ip:port, channel>
59+
private Map<String, Channel> dubboChannels;
6060

6161
private ServerBootstrap bootstrap;
6262

@@ -95,6 +95,7 @@ protected void doOpen0() {
9595
bootstrap = new ServerBootstrap(channelFactory);
9696

9797
final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
98+
// set dubboChannels
9899
dubboChannels = nettyHandler.getChannels();
99100
// https://issues.jboss.org/browse/NETTY-365
100101
// https://issues.jboss.org/browse/NETTY-379

dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public class NettyServer extends AbstractServer implements RemotingServer {
5454

5555
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NettyServer.class);
5656

57-
private Map<String, Channel> channels; // <ip:port, channel>
57+
// <ip:port, channel>
58+
private Map<String, Channel> channels;
5859

5960
private ServerBootstrap bootstrap;
6061

@@ -75,6 +76,7 @@ protected void doOpen() throws Throwable {
7576
bootstrap = new ServerBootstrap(channelFactory);
7677

7778
final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
79+
// set channels
7880
channels = nettyHandler.getChannels();
7981
// https://issues.jboss.org/browse/NETTY-365
8082
// https://issues.jboss.org/browse/NETTY-379

0 commit comments

Comments
 (0)