From 5ad80c5887c0520c0b26304ad5a3d29552b27775 Mon Sep 17 00:00:00 2001 From: greenjustin <72885340+greenjustin@users.noreply.github.com> Date: Sat, 24 Oct 2020 08:56:33 -0400 Subject: [PATCH] Allow EventLoops to rethrow Error (#10694) Motivation: Thread.stop() works by producing a ThreadDeath error in the target thread. EventLoops swallow all Throwables, which makes them effectively unkillable. This is effectively a memory leak, for our application. Beside this we should also just regrow all `Error` as there is almost no way to recover. Modification: Edit the EventLoops that swallow Throwables to instead rethrow Error. Result: `EventLoop` can crash if `Error` is thrown --- .../src/main/java/io/netty/channel/epoll/EpollHandler.java | 4 +++- .../src/main/java/io/netty/channel/kqueue/KQueueHandler.java | 2 ++ transport/src/main/java/io/netty/channel/nio/NioHandler.java | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollHandler.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollHandler.java index f406d7883a..9b53790a14 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollHandler.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollHandler.java @@ -298,7 +298,7 @@ public class EpollHandler implements IoHandler { int strategy = selectStrategy.calculateStrategy(selectNowSupplier, !context.canBlock()); switch (strategy) { case SelectStrategy.CONTINUE: - return 0 ; + return 0; case SelectStrategy.BUSY_WAIT: strategy = epollBusyWait(); @@ -345,6 +345,8 @@ public class EpollHandler implements IoHandler { //increase the size of the array as we needed the whole space for the events events.increase(); } + } catch (Error error) { + throw error; } catch (Throwable t) { handleLoopException(t); } diff --git a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueHandler.java b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueHandler.java index b2538bc761..275ee3a8b7 100644 --- a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueHandler.java +++ b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueHandler.java @@ -306,6 +306,8 @@ public final class KQueueHandler implements IoHandler { //increase the size of the array as we needed the whole space for the events eventList.realloc(false); } + } catch (Error e) { + throw e; } catch (Throwable t) { handleLoopException(t); } diff --git a/transport/src/main/java/io/netty/channel/nio/NioHandler.java b/transport/src/main/java/io/netty/channel/nio/NioHandler.java index 0469ca1dfb..aaad93fec8 100644 --- a/transport/src/main/java/io/netty/channel/nio/NioHandler.java +++ b/transport/src/main/java/io/netty/channel/nio/NioHandler.java @@ -451,6 +451,8 @@ public final class NioHandler implements IoHandler { cancelledKeys = 0; needsToSelectAgain = false; handled = processSelectedKeys(); + } catch (Error e) { + throw e; } catch (Throwable t) { handleLoopException(t); }