Catching Throwable and closing channels rigorously.

This commit is contained in:
Simone Bordet 2013-10-29 10:03:08 +01:00
parent 00867b094b
commit edcb39cc89
1 changed files with 27 additions and 37 deletions

View File

@ -25,8 +25,6 @@ import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.nio.channels.CancelledKeyException; import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.SelectionKey; import java.nio.channels.SelectionKey;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
@ -58,14 +56,10 @@ import org.eclipse.jetty.util.thread.Scheduler;
*/ */
public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable
{ {
protected static final Logger LOG = Log.getLogger(SelectorManager.class); public static final String SUBMIT_KEY_UPDATES = "org.eclipse.jetty.io.SelectorManager.submitKeyUpdates";
public static final String SUBMIT_KEY_UPDATES="org.eclipse.jetty.io.SelectorManager.submitKeyUpdates";
/**
* The default connect timeout, in milliseconds
*/
public static final int DEFAULT_CONNECT_TIMEOUT = 15000; public static final int DEFAULT_CONNECT_TIMEOUT = 15000;
protected static final Logger LOG = Log.getLogger(SelectorManager.class);
private final static boolean __submitKeyUpdates=Boolean.valueOf(System.getProperty(SUBMIT_KEY_UPDATES,"FALSE")); private final static boolean __submitKeyUpdates = Boolean.valueOf(System.getProperty(SUBMIT_KEY_UPDATES, "false"));
private final Executor executor; private final Executor executor;
private final Scheduler scheduler; private final Scheduler scheduler;
@ -233,7 +227,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
connection.onOpen(); connection.onOpen();
} }
catch (Exception x) catch (Throwable x)
{ {
if (isRunning()) if (isRunning())
LOG.warn("Exception while notifying connection " + connection, x); LOG.warn("Exception while notifying connection " + connection, x);
@ -253,9 +247,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
connection.onClose(); connection.onClose();
} }
catch (Exception x) catch (Throwable x)
{ {
LOG.info("Exception while notifying connection " + connection, x); LOG.debug("Exception while notifying connection " + connection, x);
} }
} }
@ -368,11 +362,12 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
public void updateKey(Runnable update) public void updateKey(Runnable update)
{ {
if (__submitKeyUpdates) if (__submitKeyUpdates)
{
submit(update); submit(update);
}
else else
{ {
update.run(); runChange(update);
if (_state.compareAndSet(State.SELECT, State.WAKEUP)) if (_state.compareAndSet(State.SELECT, State.WAKEUP))
wakeup(); wakeup();
} }
@ -435,8 +430,15 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
protected void runChange(Runnable change) protected void runChange(Runnable change)
{ {
LOG.debug("Running change {}", change); try
change.run(); {
LOG.debug("Running change {}", change);
change.run();
}
catch (Throwable x)
{
LOG.debug("Could not run change " + change, x);
}
} }
@Override @Override
@ -553,7 +555,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
if (attachment instanceof EndPoint) if (attachment instanceof EndPoint)
((EndPoint)attachment).close(); ((EndPoint)attachment).close();
} }
catch (Exception x) catch (Throwable x)
{ {
LOG.warn("Could not process key for channel " + key.channel(), x); LOG.warn("Could not process key for channel " + key.channel(), x);
if (attachment instanceof EndPoint) if (attachment instanceof EndPoint)
@ -563,10 +565,10 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
private void processConnect(SelectionKey key, Connect connect) private void processConnect(SelectionKey key, Connect connect)
{ {
key.attach(connect.attachment);
SocketChannel channel = (SocketChannel)key.channel(); SocketChannel channel = (SocketChannel)key.channel();
try try
{ {
key.attach(connect.attachment);
boolean connected = finishConnect(channel); boolean connected = finishConnect(channel);
if (connected) if (connected)
{ {
@ -580,10 +582,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
throw new ConnectException(); throw new ConnectException();
} }
} }
catch (Exception x) catch (Throwable x)
{ {
connect.failed(x); connect.failed(x);
closeNoExceptions(channel);
} }
} }
@ -593,7 +594,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
closeable.close(); closeable.close();
} }
catch (IOException x) catch (Throwable x)
{ {
LOG.ignore(x); LOG.ignore(x);
} }
@ -740,8 +741,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
EndPoint endpoint = createEndPoint(_channel, key); EndPoint endpoint = createEndPoint(_channel, key);
key.attach(endpoint); key.attach(endpoint);
} }
catch (IOException x) catch (Throwable x)
{ {
closeNoExceptions(_channel);
LOG.debug(x); LOG.debug(x);
} }
} }
@ -768,7 +770,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
channel.register(_selector, SelectionKey.OP_CONNECT, this); channel.register(_selector, SelectionKey.OP_CONNECT, this);
} }
catch (ClosedSelectorException | ClosedChannelException x) catch (Throwable x)
{ {
failed(x); failed(x);
} }
@ -779,22 +781,10 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
if (failed.compareAndSet(false, true)) if (failed.compareAndSet(false, true))
{ {
timeout.cancel(); timeout.cancel();
close(); closeNoExceptions(channel);
connectionFailed(channel, failure, attachment); connectionFailed(channel, failure, attachment);
} }
} }
private void close()
{
try
{
channel.close();
}
catch (IOException x)
{
LOG.ignore(x);
}
}
} }
private class ConnectTimeout implements Runnable private class ConnectTimeout implements Runnable
@ -878,7 +868,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
try try
{ {
endPoint.getConnection().close(); closeNoExceptions(endPoint.getConnection());
} }
finally finally
{ {