From 269896da132a3f6dee8ac17e1b8525f7dc30ab90 Mon Sep 17 00:00:00 2001 From: violetagg Date: Tue, 11 Feb 2020 21:24:41 +0200 Subject: [PATCH] When BlockHound is installed, do not report GlobalEventExecutor/SingleThreadEventExecutor#takeTask as blocking call. (#10020) Motivation: GlobalEventExecutor/SingleThreadEventExecutor#taskQueue is BlockingQueue. Modifications: Add allowBlockingCallsInside configuration for GlobalEventExecutor/SingleThreadEventExecutor#takeTask. Result: Fixes #9984 When BlockHound is installed, GlobalEventExecutor/SingleThreadEventExecutor#takeTask is not reported as a blocking call. --- .../java/io/netty/util/internal/Hidden.java | 7 ++++ .../NettyBlockHoundIntegrationTest.java | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/common/src/main/java/io/netty/util/internal/Hidden.java b/common/src/main/java/io/netty/util/internal/Hidden.java index c0e9bba538..52742f181c 100644 --- a/common/src/main/java/io/netty/util/internal/Hidden.java +++ b/common/src/main/java/io/netty/util/internal/Hidden.java @@ -62,6 +62,13 @@ class Hidden { "confirmShutdown" ); + builder.allowBlockingCallsInside("io.netty.util.concurrent.GlobalEventExecutor", + "takeTask"); + + builder.allowBlockingCallsInside( + "io.netty.util.concurrent.SingleThreadEventExecutor", + "takeTask"); + builder.allowBlockingCallsInside( "io.netty.handler.ssl.SslHandler", "handshake" diff --git a/transport-blockhound-tests/src/test/java/io/netty/util/internal/NettyBlockHoundIntegrationTest.java b/transport-blockhound-tests/src/test/java/io/netty/util/internal/NettyBlockHoundIntegrationTest.java index 489785fbd1..3dcd793a15 100644 --- a/transport-blockhound-tests/src/test/java/io/netty/util/internal/NettyBlockHoundIntegrationTest.java +++ b/transport-blockhound-tests/src/test/java/io/netty/util/internal/NettyBlockHoundIntegrationTest.java @@ -35,9 +35,13 @@ import io.netty.handler.ssl.SslProvider; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import io.netty.handler.ssl.util.SelfSignedCertificate; import io.netty.util.ReferenceCountUtil; +import io.netty.util.concurrent.DefaultThreadFactory; +import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.ImmediateEventExecutor; import io.netty.util.concurrent.ImmediateExecutor; +import io.netty.util.concurrent.ScheduledFuture; +import io.netty.util.concurrent.SingleThreadEventExecutor; import io.netty.util.internal.Hidden.NettyBlockHoundIntegration; import org.hamcrest.Matchers; import org.junit.BeforeClass; @@ -48,6 +52,7 @@ import reactor.blockhound.integration.BlockHoundIntegration; import java.net.InetSocketAddress; import java.util.ServiceLoader; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; @@ -93,6 +98,35 @@ public class NettyBlockHoundIntegrationTest { } } + @Test(timeout = 5000L) + public void testGlobalEventExecutorTakeTask() throws InterruptedException { + testEventExecutorTakeTask(GlobalEventExecutor.INSTANCE); + } + + @Test(timeout = 5000L) + public void testSingleThreadEventExecutorTakeTask() throws InterruptedException { + SingleThreadEventExecutor executor = + new SingleThreadEventExecutor(new DefaultThreadFactory("test")) { + @Override + protected void run() { + while (!confirmShutdown()) { + Runnable task = takeTask(); + if (task != null) { + task.run(); + } + } + } + }; + testEventExecutorTakeTask(executor); + } + + private static void testEventExecutorTakeTask(EventExecutor eventExecutor) throws InterruptedException { + CountDownLatch latch = new CountDownLatch(1); + ScheduledFuture f = eventExecutor.schedule(latch::countDown, 10, TimeUnit.MILLISECONDS); + f.sync(); + latch.await(); + } + // Tests copied from io.netty.handler.ssl.SslHandlerTest @Test public void testHandshakeWithExecutorThatExecuteDirectory() throws Exception {