From a090741a600f2793b1684f5b28bca33139720b95 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 1 Aug 2018 16:57:08 +0200 Subject: [PATCH 1/2] Issue #2755 - ManagedSelector 100% CPU spin. Added check for thread interrupted. If interrupted and ManagedSelector is not running, bail out. Signed-off-by: Simone Bordet --- .../src/main/java/org/eclipse/jetty/io/ManagedSelector.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java index cac08240b57..1f512c3ae21 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.net.ConnectException; import java.net.SocketTimeoutException; import java.nio.channels.CancelledKeyException; +import java.nio.channels.ClosedSelectorException; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; @@ -426,6 +427,9 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable if (LOG.isDebugEnabled()) LOG.debug("Selector {} woken up from select, {}/{}/{} selected", selector, selected, selector.selectedKeys().size(), selector.keys().size()); + if (Thread.interrupted() && !isRunning()) + throw new ClosedSelectorException(); + int updates; synchronized(ManagedSelector.this) { From 0a336230efb344771d49a9e8fe13ac01867fd0c9 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 1 Aug 2018 17:25:46 +0200 Subject: [PATCH 2/2] Issue #2755 - ManagedSelector 100% CPU spin. Avoid race between doStart() and doStop() by waiting in doStart() for _started=true. Signed-off-by: Simone Bordet --- .../org/eclipse/jetty/io/ManagedSelector.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java index 1f512c3ae21..2023ff9c328 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java @@ -102,7 +102,9 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable _selectorManager.execute(_strategy::produce); // Set started only if we really are started - submit(s->_started.set(true)); + Start start = new Start(); + submit(start); + start._started.await(); } public int size() @@ -540,6 +542,18 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable public void update(Selector selector); } + private class Start implements SelectorUpdate + { + private final CountDownLatch _started = new CountDownLatch(1); + + @Override + public void update(Selector selector) + { + ManagedSelector.this._started.set(true); + _started.countDown(); + } + } + private static class DumpKeys implements SelectorUpdate { private CountDownLatch latch = new CountDownLatch(1);