Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/105306.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 105306
summary: Fix race in HTTP response shutdown handling
area: Network
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import org.elasticsearch.common.util.concurrent.ListenableFuture;
import org.elasticsearch.http.HttpChannel;
import org.elasticsearch.http.HttpResponse;
import org.elasticsearch.transport.TransportException;
import org.elasticsearch.transport.netty4.Netty4TcpChannel;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;

public class Netty4HttpChannel implements HttpChannel {

Expand All @@ -32,12 +32,12 @@ public class Netty4HttpChannel implements HttpChannel {

@Override
public void sendResponse(HttpResponse response, ActionListener<Void> listener) {
if (isOpen()) {
channel.writeAndFlush(response, Netty4TcpChannel.addPromise(listener, channel));
} else {
// No need to dispatch to the event loop just to fail this listener; moreover the channel might be closed because the whole
// node is shutting down, in which case the event loop might not exist any more so the channel promise cannot be completed.
listener.onFailure(new ClosedChannelException());
// We need to both guard against double resolving the listener and not resolving it in case of event loop shutdown so we need to
// use #notifyOnce here until https://github.com/netty/netty/issues/8007 is resolved.
var wrapped = ActionListener.notifyOnce(listener);
channel.writeAndFlush(response, Netty4TcpChannel.addPromise(wrapped, channel));
if (channel.eventLoop().isShutdown()) {
wrapped.onFailure(new TransportException("Cannot send HTTP response, event loop is shutting down."));
}
}

Expand Down