Fix NPE that can happen in the WriteTimeoutHandler when multiple executors are used (#11056)

Motivation:

In WriteTimeoutHandler we did make the assumption that the executor which is used to schedule the timeout is the same that is backing the write promise. This may not be true which will cause concurrency issues

Modifications:

Ensure we are on the right thread when try to modify the doubly-linked-list and if not schedule it on the right thread.

Result:

Fixes https://github.com/netty/netty/issues/11053
This commit is contained in:
Norman Maurer 2021-03-04 15:27:02 +01:00
parent 75120472f5
commit 2ce03e0a08
2 changed files with 65 additions and 1 deletions

View File

@ -114,9 +114,11 @@ public class WriteTimeoutHandler implements ChannelHandler {
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
assert ctx.executor().inEventLoop();
WriteTimeoutTask task = lastTask;
lastTask = null;
while (task != null) {
assert task.ctx.executor().inEventLoop();
task.scheduledFuture.cancel(false);
WriteTimeoutTask prev = task.prev;
task.prev = null;
@ -139,6 +141,7 @@ public class WriteTimeoutHandler implements ChannelHandler {
}
private void addWriteTimeoutTask(WriteTimeoutTask task) {
assert task.ctx.executor().inEventLoop();
if (lastTask != null) {
lastTask.next = task;
task.prev = lastTask;
@ -147,6 +150,7 @@ public class WriteTimeoutHandler implements ChannelHandler {
}
private void removeWriteTimeoutTask(WriteTimeoutTask task) {
assert task.ctx.executor().inEventLoop();
if (task == lastTask) {
// task is the tail of list
assert task.next == null;
@ -214,7 +218,19 @@ public class WriteTimeoutHandler implements ChannelHandler {
public void operationComplete(ChannelFuture future) throws Exception {
// scheduledFuture has already be set when reaching here
scheduledFuture.cancel(false);
removeWriteTimeoutTask(this);
// Check if its safe to modify the "doubly-linked-list" that we maintain. If its not we will schedule the
// modification so its picked up by the executor..
if (ctx.executor().inEventLoop()) {
removeWriteTimeoutTask(this);
} else {
// So let's just pass outself to the executor which will then take care of remove this task
// from the doubly-linked list. Schedule ourself is fine as the promise itself is done.
//
// This fixes https://github.com/netty/netty/issues/11053
assert promise.isDone();
ctx.executor().execute(this);
}
}
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2021 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.timeout;
import io.netty.channel.DefaultChannelPromise;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import static org.junit.Assert.assertTrue;
public class WriteTimeoutHandlerTest {
@Test
public void testPromiseUseDifferentExecutor() throws Exception {
EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
EmbeddedChannel channel = new EmbeddedChannel(false, false);
try {
channel.pipeline().addLast(new WriteTimeoutHandler(10000));
final CountDownLatch latch = new CountDownLatch(1);
channel.register();
channel.writeAndFlush("something", new DefaultChannelPromise(channel, group1.next())).addListener(f -> {
latch.countDown();
});
latch.await();
assertTrue(channel.finishAndReleaseAll());
} finally {
group1.shutdownGracefully();
}
}
}