Skip to content

Commit 26a35c9

Browse files
committed
Improve null-safety of module/spring-boot-reactor-netty
See gh-46926
1 parent 6c288c9 commit 26a35c9

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

module/spring-boot-reactor-netty/src/main/java/org/springframework/boot/reactor/netty/GracefulShutdown.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ void shutDownGracefully(GracefulShutdownCallback callback) {
5252
return;
5353
}
5454
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
55-
this.shutdownThread = new Thread(() -> doShutdown(callback, server), "netty-shutdown");
56-
this.shutdownThread.start();
55+
Thread shutdownThread = new Thread(() -> doShutdown(callback, server), "netty-shutdown");
56+
this.shutdownThread = shutdownThread;
57+
shutdownThread.start();
5758
}
5859

5960
private void doShutdown(GracefulShutdownCallback callback, DisposableServer server) {

module/spring-boot-reactor-netty/src/main/java/org/springframework/boot/reactor/netty/NettyWebServer.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ public void setRouteProviders(List<NettyRouteProvider> routeProviders) {
112112

113113
@Override
114114
public void start() throws WebServerException {
115-
if (this.disposableServer == null) {
115+
DisposableServer disposableServer = this.disposableServer;
116+
if (disposableServer == null) {
116117
try {
117-
this.disposableServer = startHttpServer();
118+
disposableServer = startHttpServer();
119+
this.disposableServer = disposableServer;
118120
}
119121
catch (Exception ex) {
120122
PortInUseException.ifCausedBy(ex, ChannelBindException.class, (bindException) -> {
@@ -124,10 +126,8 @@ public void start() throws WebServerException {
124126
});
125127
throw new WebServerException("Unable to start Netty", ex);
126128
}
127-
if (this.disposableServer != null) {
128-
logger.info(getStartedOnMessage(this.disposableServer));
129-
}
130-
startDaemonAwaitThread(this.disposableServer);
129+
logger.info(getStartedOnMessage(disposableServer));
130+
startDaemonAwaitThread(disposableServer);
131131
}
132132
}
133133

@@ -140,8 +140,9 @@ private String getStartedOnMessage(DisposableServer server) {
140140
}
141141

142142
protected String getStartedLogMessage() {
143-
Assert.state(this.disposableServer != null, "'disposableServer' must not be null");
144-
return getStartedOnMessage(this.disposableServer);
143+
DisposableServer disposableServer = this.disposableServer;
144+
Assert.state(disposableServer != null, "'disposableServer' must not be null");
145+
return getStartedOnMessage(disposableServer);
145146
}
146147

147148
private void tryAppend(StringBuilder message, String format, Supplier<Object> supplier) {
@@ -225,16 +226,17 @@ public void run() {
225226

226227
@Override
227228
public void stop() throws WebServerException {
228-
if (this.disposableServer != null) {
229+
DisposableServer disposableServer = this.disposableServer;
230+
if (disposableServer != null) {
229231
if (this.gracefulShutdown != null) {
230232
this.gracefulShutdown.abort();
231233
}
232234
try {
233235
if (this.lifecycleTimeout != null) {
234-
this.disposableServer.disposeNow(this.lifecycleTimeout);
236+
disposableServer.disposeNow(this.lifecycleTimeout);
235237
}
236238
else {
237-
this.disposableServer.disposeNow();
239+
disposableServer.disposeNow();
238240
}
239241
}
240242
catch (IllegalStateException ex) {
@@ -246,9 +248,10 @@ public void stop() throws WebServerException {
246248

247249
@Override
248250
public int getPort() {
249-
if (this.disposableServer != null) {
251+
DisposableServer disposableServer = this.disposableServer;
252+
if (disposableServer != null) {
250253
try {
251-
return this.disposableServer.port();
254+
return disposableServer.port();
252255
}
253256
catch (UnsupportedOperationException ex) {
254257
return -1;

0 commit comments

Comments
 (0)