Fix NPE in the TcpNetServerConnectionFactory

https://build.spring.io/browse/INTSAMPLES-NIGHTLY-2413

When we fail with the
`java.net.BindException: Address already in use (Bind failed)` in the
`TcpNetServerConnectionFactory.run()`, the `serverSocket` property
remains `null` and we get `NPE` in the `catch` block trying to `close()`
the socket.

* Call `stop()` instead which has all the required protections.

**Cherry-pick to 5.0.x and 4.3.x**
This commit is contained in:
Artem Bilan
2018-04-02 13:13:42 -04:00
committed by Gary Russell
parent 544de6bf5f
commit 5cdcd81444

View File

@@ -33,6 +33,8 @@ import org.springframework.util.Assert;
* a {@link ServerSocket}. Must have a {@link TcpListener} registered.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*
*/
@@ -181,12 +183,7 @@ public class TcpNetServerConnectionFactory extends AbstractServerConnectionFacto
else if (isActive()) {
logger.error("Error on ServerSocket; port = " + getPort(), e);
publishServerExceptionEvent(e);
try {
this.serverSocket.close();
}
catch (IOException e1) {
// empty
}
stop();
}
}
finally {
@@ -223,7 +220,8 @@ public class TcpNetServerConnectionFactory extends AbstractServerConnectionFacto
try {
this.serverSocket.close();
}
catch (IOException e) { }
catch (IOException e) {
}
this.serverSocket = null;
super.stop();
}