Browse Source

Fix Exceptions in EventHandler#postHandling Breaking Select Loop (#44347)

* Fix Exceptions in EventHandler#postHandling Breaking Select Loop

* We can run into the `write` path for SSL channels when they are not fully registered (if registration fails and a close message is attempted to be written) and thus into NPEs from missing selection keys
  * This is a quick fix to quiet down tests, a cleaner solution will be incoming for #44343
* Relates #44343
Armin Braun 6 years ago
parent
commit
156047f202
1 changed files with 7 additions and 1 deletions
  1. 7 1
      libs/nio/src/main/java/org/elasticsearch/nio/EventHandler.java

+ 7 - 1
libs/nio/src/main/java/org/elasticsearch/nio/EventHandler.java

@@ -180,9 +180,15 @@ public class EventHandler {
                 closeException(context, e);
             }
         } else {
+            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);
-            boolean pendingWrites = context.readyForFlush();
             if (currentlyWriteInterested == false && pendingWrites) {
                 SelectionKeyUtils.setWriteInterested(selectionKey);
             } else if (currentlyWriteInterested && pendingWrites == false) {