diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index 386648197a..bd876750b4 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -374,6 +374,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha /** true if the channel has never been registered, false otherwise */ private boolean neverRegistered = true; + private void assertEventLoop() { + assert !registered || eventLoop.inEventLoop(); + } + @Override public final ChannelOutboundBuffer outboundBuffer() { return outboundBuffer; @@ -470,6 +474,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void bind(final SocketAddress localAddress, final ChannelPromise promise) { + assertEventLoop(); + if (!promise.setUncancellable() || !ensureOpen(promise)) { return; } @@ -510,6 +516,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void disconnect(final ChannelPromise promise) { + assertEventLoop(); + if (!promise.setUncancellable()) { return; } @@ -538,6 +546,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void close(final ChannelPromise promise) { + assertEventLoop(); + close(promise, CLOSED_CHANNEL_EXCEPTION, false); } @@ -630,6 +640,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void closeForcibly() { + assertEventLoop(); + try { doClose(); } catch (Exception e) { @@ -639,7 +651,9 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void deregister(final ChannelPromise promise) { - deregister(promise, false); + assertEventLoop(); + + deregister(promise, false); } private void deregister(final ChannelPromise promise, final boolean fireChannelInactive) { @@ -688,6 +702,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void beginRead() { + assertEventLoop(); + if (!isActive()) { return; } @@ -707,6 +723,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void write(Object msg, ChannelPromise promise) { + assertEventLoop(); + ChannelOutboundBuffer outboundBuffer = this.outboundBuffer; if (outboundBuffer == null) { // If the outboundBuffer is null we know the channel was closed and so @@ -737,6 +755,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final void flush() { + assertEventLoop(); + ChannelOutboundBuffer outboundBuffer = this.outboundBuffer; if (outboundBuffer == null) { return; @@ -797,6 +817,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final ChannelPromise voidPromise() { + assertEventLoop(); + return unsafeVoidPromise; }