Remove assert check that is not valid anymore now that an Channel is tied to an EventLoop from the beginning. (#9966)

Motivation:

Because we now tie and EventLoop to a Channel from the beginning its possible that we schedule something on it before the registration with the Selector is done. This can lead to AssertionErrors in the current form of the code like:

java.lang.AssertionError
	at io.netty.channel.nio.AbstractNioChannel.selectionKey(AbstractNioChannel.java:109)
	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.isFlushPending(AbstractNioChannel.java:344)
	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.flush0(AbstractNioChannel.java:332)
	at io.netty.channel.AbstractChannel$AbstractUnsafe.flush(AbstractChannel.java:855)
	at io.netty.channel.DefaultChannelPipeline$HeadHandler.flush(DefaultChannelPipeline.java:1253)
	at io.netty.channel.DefaultChannelHandlerContext.invokeFlush(DefaultChannelHandlerContext.java:747)
	at io.netty.channel.DefaultChannelHandlerContext.access$1300(DefaultChannelHandlerContext.java:38)
	at io.netty.channel.DefaultChannelHandlerContext$WriteAndFlushTask.write(DefaultChannelHandlerContext.java:1144)
	at io.netty.channel.DefaultChannelHandlerContext$AbstractWriteTask.run(DefaultChannelHandlerContext.java:1055)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:360)
	at io.netty.channel.SingleThreadEventLoop.run(SingleThreadEventLoop.java:200)
	at io.netty.util.concurrent.SingleThreadEventExecutor.lambda$doStartThread$4(SingleThreadEventExecutor.java:855)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:75)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)

Modifications:

Remove assert and add a few null checks.

Result:

No more java.lang.AssertionError.
This commit is contained in:
Norman Maurer 2020-01-24 14:10:14 -08:00 committed by GitHub
parent 3117a43547
commit 699d3fcd54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View File

@ -327,7 +327,7 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
// 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()) {
if (key == null || !key.isValid()) {
return;
}
final int interestOps = key.interestOps();
@ -341,7 +341,7 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
// 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()) {
if (key == null || !key.isValid()) {
return;
}
final int interestOps = key.interestOps();

View File

@ -103,10 +103,10 @@ public abstract class AbstractNioChannel extends AbstractChannel {
}
/**
* Return the current {@link SelectionKey}
* Return the current {@link SelectionKey} or {@code null} if the underlying channel was not registered with the
* {@link java.nio.channels.Selector} yet.
*/
protected SelectionKey selectionKey() {
assert selectionKey != null;
return selectionKey;
}
@ -200,7 +200,7 @@ public abstract class AbstractNioChannel extends AbstractChannel {
// 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()) {
if (key == null || !key.isValid()) {
return;
}
int interestOps = key.interestOps();
@ -342,7 +342,8 @@ public abstract class AbstractNioChannel extends AbstractChannel {
private boolean isFlushPending() {
SelectionKey selectionKey = selectionKey();
return selectionKey.isValid() && (selectionKey.interestOps() & SelectionKey.OP_WRITE) != 0;
return selectionKey != null && selectionKey.isValid()
&& (selectionKey.interestOps() & SelectionKey.OP_WRITE) != 0;
}
}

View File

@ -128,6 +128,9 @@ public abstract class AbstractNioMessageChannel extends AbstractNioChannel {
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
final SelectionKey key = selectionKey();
if (key == null) {
return;
}
final int interestOps = key.interestOps();
for (;;) {