From b147fa68792ab58af18debbb61fd2d294804e2ae Mon Sep 17 00:00:00 2001 From: zhangduo Date: Mon, 21 Dec 2015 16:33:42 +0800 Subject: [PATCH] Clear selectedKeys in OioSctpChannel.doReadMessages Motivation: The fix for https://github.com/netty/netty/issues/3884 breaks SctpEchoTest because Selector.select will always return 0 if you do not clear last selectedKeys. Modifications: Clear readSelector.selectedKeys() if it is not empty. Result: SctpEchoTest is green again. --- .../java/io/netty/channel/sctp/oio/OioSctpChannel.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java index def93f72e3..77ded22112 100755 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java @@ -185,10 +185,18 @@ public class OioSctpChannel extends AbstractOioMessageChannel return readMessages; } + // We must clear the selectedKeys because the Selector will never do it. If we do not clear it, the selectionKey + // will always be returned even if there is no data can be read which causes performance issue. And in some + // implementation of Selector, the select method may return 0 if the selectionKey which is ready for process has + // already been in the selectedKeys and cause the keysSelected above to be false even if we actually have + // something to read. + readSelector.selectedKeys().clear(); + RecvByteBufAllocator.Handle allocHandle = this.allocHandle; if (allocHandle == null) { this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle(); } + ByteBuf buffer = allocHandle.allocate(config().getAllocator()); boolean free = true;