diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/EventHandler.java b/libs/nio/src/main/java/org/elasticsearch/nio/EventHandler.java index 4a6fed2bb34..bbd878b4656 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/EventHandler.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/EventHandler.java @@ -180,9 +180,15 @@ public class EventHandler { closeException(context, e); } } else { - SelectionKey selectionKey = context.getSelectionKey(); - boolean currentlyWriteInterested = SelectionKeyUtils.isWriteInterested(selectionKey); boolean pendingWrites = context.readyForFlush(); + SelectionKey selectionKey = context.getSelectionKey(); + if (selectionKey == null) { + if (pendingWrites) { + writeException(context, new IllegalStateException("Tried to write to an not yet registered channel")); + } + return; + } + boolean currentlyWriteInterested = SelectionKeyUtils.isWriteInterested(selectionKey); if (currentlyWriteInterested == false && pendingWrites) { SelectionKeyUtils.setWriteInterested(selectionKey); } else if (currentlyWriteInterested && pendingWrites == false) {