Fixes #3133 - Logging of key.readyOps() can throw unchecked CancelledKeyException.

Introduced safeInterestOps() and safeReadyOps() to catch exceptions
they may throw and using them in relevant places to fix the issue.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2018-11-21 17:37:16 +01:00
parent 041e8fd9cf
commit 910665a55a
2 changed files with 38 additions and 30 deletions

View File

@ -31,7 +31,6 @@ import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Invocable; import org.eclipse.jetty.util.thread.Invocable;
import org.eclipse.jetty.util.thread.Locker;
import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.Scheduler;
/** /**
@ -426,21 +425,11 @@ public abstract class ChannelEndPoint extends AbstractEndPoint implements Manage
public String toEndPointString() public String toEndPointString()
{ {
// We do a best effort to print the right toString() and that's it. // We do a best effort to print the right toString() and that's it.
try
{
boolean valid = _key != null && _key.isValid();
int keyInterests = valid ? _key.interestOps() : -1;
int keyReadiness = valid ? _key.readyOps() : -1;
return String.format("%s{io=%d/%d,kio=%d,kro=%d}", return String.format("%s{io=%d/%d,kio=%d,kro=%d}",
super.toEndPointString(), super.toEndPointString(),
_currentInterestOps, _currentInterestOps,
_desiredInterestOps, _desiredInterestOps,
keyInterests, ManagedSelector.safeInterestOps(_key),
keyReadiness); ManagedSelector.safeReadyOps(_key));
}
catch (Throwable x)
{
return String.format("%s{io=%s,kio=-2,kro=-2}", super.toString(), _desiredInterestOps);
}
} }
} }

View File

@ -272,6 +272,34 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable
} }
} }
static int safeReadyOps(SelectionKey selectionKey)
{
try
{
return selectionKey.readyOps();
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug(x);
return -1;
}
}
static int safeInterestOps(SelectionKey selectionKey)
{
try
{
return selectionKey.interestOps();
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug(x);
return -1;
}
}
@Override @Override
public void dump(Appendable out, String indent) throws IOException public void dump(Appendable out, String indent) throws IOException
{ {
@ -474,7 +502,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable
{ {
Object attachment = key.attachment(); Object attachment = key.attachment();
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("selected {} {} {} ",key.readyOps(),key,attachment); LOG.debug("selected {} {} {} ", safeReadyOps(key), key, attachment);
try try
{ {
if (attachment instanceof Selectable) if (attachment instanceof Selectable)
@ -490,7 +518,7 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable
} }
else else
{ {
throw new IllegalStateException("key=" + key + ", att=" + attachment + ", iOps=" + key.interestOps() + ", rOps=" + key.readyOps()); throw new IllegalStateException("key=" + key + ", att=" + attachment + ", iOps=" + safeInterestOps(key) + ", rOps=" + safeReadyOps(key));
} }
} }
catch (CancelledKeyException x) catch (CancelledKeyException x)
@ -571,19 +599,10 @@ public class ManagedSelector extends ContainerLifeCycle implements Dumpable
List<String> list = new ArrayList<>(selector_keys.size()); List<String> list = new ArrayList<>(selector_keys.size());
for (SelectionKey key : selector_keys) for (SelectionKey key : selector_keys)
{ {
if (key==null) if (key != null)
continue; list.add(String.format("SelectionKey@%x{i=%d}->%s", key.hashCode(), safeInterestOps(key), key.attachment()));
try
{
list.add(String.format("SelectionKey@%x{i=%d}->%s", key.hashCode(), key.interestOps(), key.attachment()));
}
catch (Throwable x)
{
list.add(String.format("SelectionKey@%x[%s]->%s", key.hashCode(), x, key.attachment()));
}
} }
keys = list; keys = list;
latch.countDown(); latch.countDown();
} }