From ac348956e3b4f435d662ac7c1303e7d4fd8d9111 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 27 Jun 2014 17:09:27 +0200 Subject: [PATCH] [#2615] Correctly update SelectionKey after selector rebuild Motivation: When a select rebuild was triggered the reference to the SelectionKey is not updated in AbstractNioChannel. This will cause a CancelledKeyException later. Modification: Correctly update SelectionKey reference after rebuild Result: Fix exception --- .../main/java/io/netty/channel/nio/AbstractNioChannel.java | 2 +- .../src/main/java/io/netty/channel/nio/NioEventLoop.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java index c71467ce9e..516a94f665 100644 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java @@ -46,7 +46,7 @@ public abstract class AbstractNioChannel extends AbstractChannel { private final SelectableChannel ch; protected final int readInterestOp; - private volatile SelectionKey selectionKey; + volatile SelectionKey selectionKey; private volatile boolean inputShutdown; private volatile boolean readPending; diff --git a/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java b/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java index ab853e9521..bd7673a953 100644 --- a/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java +++ b/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java @@ -259,7 +259,11 @@ public final class NioEventLoop extends SingleThreadEventLoop { int interestOps = key.interestOps(); key.cancel(); - key.channel().register(newSelector, interestOps, a); + SelectionKey newKey = key.channel().register(newSelector, interestOps, a); + if (a instanceof AbstractNioChannel) { + // Update SelectionKey + ((AbstractNioChannel) a).selectionKey = newKey; + } nChannels ++; } catch (Exception e) { logger.warn("Failed to re-register a Channel to the new Selector.", e);