From 8c75eeccce81b158c2513ec29f6d6f644735568b Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 29 Apr 2020 11:57:06 +0200 Subject: [PATCH] Issue #4798 - Better handling of fatal Selector failures. Updates after review. Signed-off-by: Simone Bordet --- .../org/eclipse/jetty/io/ChannelEndPoint.java | 9 ++---- .../org/eclipse/jetty/io/ManagedSelector.java | 28 ++++--------------- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java index 835f6f332eb..30509c906cb 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java @@ -320,6 +320,7 @@ public abstract class ChannelEndPoint extends AbstractEndPoint implements Manage // This method runs from the selector thread, // possibly concurrently with changeInterests(int). + _key = key; int readyOps = key.readyOps(); int oldInterestOps; int newInterestOps; @@ -354,7 +355,7 @@ public abstract class ChannelEndPoint extends AbstractEndPoint implements Manage private void updateKey(Selector selector) { - _selector.compute(selector, _channel, _key, this::updateKey); + _selector.runKeyAction(selector, _channel, _key, this::updateKey); } @Override @@ -395,12 +396,6 @@ public abstract class ChannelEndPoint extends AbstractEndPoint implements Manage } } - @Override - public void replaceKey(SelectionKey oldKey, SelectionKey newKey) - { - _key = newKey; - } - private void changeInterests(int operation) { // This method runs from any thread, possibly 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 893b5861e88..04302b4b935 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 @@ -145,7 +145,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable super.doStop(); } - void compute(Selector selector, SelectableChannel channel, SelectionKey selectionKey, Consumer action) + void runKeyAction(Selector selector, SelectableChannel channel, SelectionKey selectionKey, Consumer action) { SelectionKey key = selectionKey; // Refresh the key if the selector has been recreated. @@ -206,11 +206,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable try { Object attachment = oldKey.attachment(); - SelectionKey newKey = channel.register(newSelector, interestOps, attachment); - - if (attachment instanceof Selectable) - ((Selectable)attachment).replaceKey(oldKey, newKey); - + channel.register(newSelector, interestOps, attachment); oldKey.cancel(); if (LOG.isDebugEnabled()) LOG.debug("Transferred {} iOps={} att={}", channel, interestOps, attachment); @@ -365,7 +361,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable EndPoint endPoint = _selectorManager.newEndPoint(channel, this, selectionKey); Connection connection = _selectorManager.newConnection(channel, endPoint, selectionKey.attachment()); endPoint.setConnection(connection); - submit(selector -> compute(selector, channel, selectionKey, key -> key.attach(endPoint)), true); + submit(selector -> runKeyAction(selector, channel, selectionKey, key -> key.attach(endPoint)), true); endPoint.onOpen(); endPointOpened(endPoint); _selectorManager.connectionOpened(connection); @@ -483,15 +479,6 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable * @param key the SelectionKey to update */ void updateKey(SelectionKey key); - - /** - * Callback method invoked when a selectable is transferred - * from one selector to a new selector. - * - * @param oldKey the old SelectionKey - * @param newKey the new SelectionKey - */ - void replaceKey(SelectionKey oldKey, SelectionKey newKey); } private class SelectorProducer implements ExecutionStrategy.Producer @@ -774,6 +761,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable @Override public Runnable onSelected(SelectionKey key) { + _key = key; SelectableChannel channel = null; try { @@ -798,18 +786,12 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable { } - @Override - public void replaceKey(SelectionKey oldKey, SelectionKey newKey) - { - _key = newKey; - } - @Override public void close() throws IOException { // May be called from any thread. // Implements AbstractConnector.setAccepting(boolean). - submit(selector -> compute(selector, _channel, _key, SelectionKey::cancel)); + submit(selector -> runKeyAction(selector, _channel, _key, SelectionKey::cancel)); } }