Skip to content

Commit 277637f

Browse files
authored
Do not set SO_LINGER on server channels (#26997)
Right now we are attempting to set SO_LINGER to 0 on server channels when we are stopping the tcp transport. This is not a supported socket option and throws an exception. This also prevents the channels from being closed. This commit 1. doesn't set SO_LINGER for server channges, 2. checks that it is a supported option in nio, and 3. changes the log message to warn for server channel close exceptions.
1 parent bb013c6 commit 277637f

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

core/src/main/java/org/elasticsearch/transport/TcpTransport.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,9 @@ protected final void doStop() {
905905
// first stop to accept any incoming connections so nobody can connect to this transport
906906
for (Map.Entry<String, List<Channel>> entry : serverChannels.entrySet()) {
907907
try {
908-
closeChannels(entry.getValue(), true, true);
908+
closeChannels(entry.getValue(), true, false);
909909
} catch (Exception e) {
910-
logger.debug(
911-
(Supplier<?>) () -> new ParameterizedMessage(
912-
"Error closing serverChannel for profile [{}]", entry.getKey()), e);
910+
logger.warn(new ParameterizedMessage("Error closing serverChannel for profile [{}]", entry.getKey()), e);
913911
}
914912
}
915913
// we are holding a write lock so nobody modifies the connectedNodes / openConnections map - it's safe to first close
@@ -1024,9 +1022,9 @@ protected void innerOnFailure(Exception e) {
10241022
*
10251023
* @param channels the channels to close
10261024
* @param blocking whether the channels should be closed synchronously
1027-
* @param closingTransport whether we abort the connection on RST instead of FIN
1025+
* @param doNotLinger whether we abort the connection on RST instead of FIN
10281026
*/
1029-
protected abstract void closeChannels(List<Channel> channels, boolean blocking, boolean closingTransport) throws IOException;
1027+
protected abstract void closeChannels(List<Channel> channels, boolean blocking, boolean doNotLinger) throws IOException;
10301028

10311029
/**
10321030
* Sends message to channel. The listener's onResponse method will be called when the send is complete unless an exception

core/src/test/java/org/elasticsearch/transport/TcpTransportTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ protected Object bind(String name, InetSocketAddress address) throws IOException
191191
}
192192

193193
@Override
194-
protected void closeChannels(List channel, boolean blocking, boolean closingTransport) throws IOException {
194+
protected void closeChannels(List channel, boolean blocking, boolean doNotLinger) throws IOException {
195195

196196
}
197197

modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ protected void sendMessage(Channel channel, BytesReference reference, ActionList
331331
}
332332

333333
@Override
334-
protected void closeChannels(final List<Channel> channels, boolean blocking, boolean closingTransport) throws IOException {
335-
if (closingTransport) {
334+
protected void closeChannels(final List<Channel> channels, boolean blocking, boolean doNotLinger) throws IOException {
335+
if (doNotLinger) {
336336
for (Channel channel : channels) {
337337
/* We set SO_LINGER timeout to 0 to ensure that when we shutdown the node we don't have a gazillion connections sitting
338338
* in TIME_WAIT to free up resources quickly. This is really the only part where we close the connection from the server

test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ protected void sendMessage(MockChannel mockChannel, BytesReference reference, Ac
243243
}
244244

245245
@Override
246-
protected void closeChannels(List<MockChannel> channels, boolean blocking, boolean closingTransport) throws IOException {
247-
if (closingTransport) {
246+
protected void closeChannels(List<MockChannel> channels, boolean blocking, boolean doNotLinger) throws IOException {
247+
if (doNotLinger) {
248248
for (MockChannel channel : channels) {
249249
if (channel.activeChannel != null) {
250250
/* We set SO_LINGER timeout to 0 to ensure that when we shutdown the node we don't have a gazillion connections sitting

test/framework/src/main/java/org/elasticsearch/transport/nio/NioTransport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ protected NioServerSocketChannel bind(String name, InetSocketAddress address) th
9999
}
100100

101101
@Override
102-
protected void closeChannels(List<NioChannel> channels, boolean blocking, boolean closingTransport) throws IOException {
103-
if (closingTransport) {
102+
protected void closeChannels(List<NioChannel> channels, boolean blocking, boolean doNotLinger) throws IOException {
103+
if (doNotLinger) {
104104
for (NioChannel channel : channels) {
105105
/* We set SO_LINGER timeout to 0 to ensure that when we shutdown the node we don't have a gazillion connections sitting
106106
* in TIME_WAIT to free up resources quickly. This is really the only part where we close the connection from the server
107107
* side otherwise the client (node) initiates the TCP closing sequence which doesn't cause these issues. Setting this
108108
* by default from the beginning can have unexpected side-effects an should be avoided, our protocol is designed
109109
* in a way that clients close connection which is how it should be*/
110-
if (channel.isOpen()) {
110+
if (channel.isOpen() && channel.getRawChannel().supportedOptions().contains(StandardSocketOptions.SO_LINGER)) {
111111
channel.getRawChannel().setOption(StandardSocketOptions.SO_LINGER, 0);
112112
}
113113
}

0 commit comments

Comments
 (0)