From ed7613e1830a69da5370916dc8e9086b3cc231e4 Mon Sep 17 00:00:00 2001 From: John Fallows Date: Sun, 5 Aug 2012 15:11:12 -0700 Subject: [PATCH] Resolve issue #489 with an atomic readInProgress guard to prevent ReadPendingException. --- .../io/netty/channel/socket/aio/AioSocketChannel.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java index 817cbc0af7..16a44c6c77 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java @@ -54,6 +54,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne private boolean flushing; private final AtomicBoolean readSuspended = new AtomicBoolean(); + private final AtomicBoolean readInProgress = new AtomicBoolean(); private final Runnable readTask = new Runnable() { @Override @@ -191,6 +192,11 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne return; } + // prevent ReadPendingException + if (!readInProgress.compareAndSet(false, true)) { + return; + } + ByteBuf byteBuf = pipeline().inboundByteBuffer(); if (!byteBuf.readable()) { byteBuf.discardReadBytes(); @@ -297,6 +303,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne } } } finally { + // see beginRead + channel.readInProgress.set(false); + if (read) { if (!channel.readSuspended.get()) { pipeline.fireInboundBufferUpdated();