diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java index 85b49ea022e15..48977762d1e54 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java @@ -66,6 +66,7 @@ import org.elasticsearch.transport.ConnectTransportException; import org.elasticsearch.transport.ConnectionProfile; import org.elasticsearch.transport.TcpTransport; +import org.elasticsearch.transport.TransportException; import org.elasticsearch.transport.TransportRequestOptions; import org.elasticsearch.transport.TransportServiceAdapter; import org.elasticsearch.transport.TransportSettings; @@ -393,8 +394,11 @@ protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile p @Override protected void sendMessage(Channel channel, BytesReference reference, ActionListener listener) { - final ChannelFuture future = channel.writeAndFlush(Netty4Utils.toByteBuf(reference)); - future.addListener(f -> { + if (channel.eventLoop().isShuttingDown()) { + listener.onFailure(new TransportException("Cannot send message, event loop is shutting down.")); + } else { + final ChannelFuture future = channel.writeAndFlush(Netty4Utils.toByteBuf(reference)); + future.addListener(f -> { if (f.isSuccess()) { listener.onResponse(channel); } else { @@ -405,7 +409,8 @@ protected void sendMessage(Channel channel, BytesReference reference, ActionList assert cause instanceof Exception; listener.onFailure((Exception) cause); } - }); + }); + } } @Override