From 13d65d7ccfcaa90f8e905e9b6c46aa0f246d8109 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 9 Jan 2014 18:27:56 +0100 Subject: [PATCH] [#2104] Make sure we only act on the SelectionKey if it is valid --- .../channel/nio/AbstractNioByteChannel.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java index 535ebd255e..f22aca6df7 100644 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java @@ -57,6 +57,12 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { private void removeReadOp() { SelectionKey key = selectionKey(); + // Check first if the key is still valid as it may be canceled as part of the deregistration + // from the EventLoop + // See https://github.com/netty/netty/issues/2104 + if (!key.isValid()) { + return; + } int interestOps = key.interestOps(); if ((interestOps & readInterestOp) != 0) { // only remove readInterestOp if needed @@ -289,6 +295,12 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { protected final void setOpWrite() { final SelectionKey key = selectionKey(); + // Check first if the key is still valid as it may be canceled as part of the deregistration + // from the EventLoop + // See https://github.com/netty/netty/issues/2104 + if (!key.isValid()) { + return; + } final int interestOps = key.interestOps(); if ((interestOps & SelectionKey.OP_WRITE) == 0) { key.interestOps(interestOps | SelectionKey.OP_WRITE); @@ -297,6 +309,12 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { protected final void clearOpWrite() { final SelectionKey key = selectionKey(); + // Check first if the key is still valid as it may be canceled as part of the deregistration + // from the EventLoop + // See https://github.com/netty/netty/issues/2104 + if (!key.isValid()) { + return; + } final int interestOps = key.interestOps(); if ((interestOps & SelectionKey.OP_WRITE) != 0) { key.interestOps(interestOps & ~SelectionKey.OP_WRITE);