Added the comment as suggested by Thomas Bocek

This commit is contained in:
Trustin Lee 2009-09-01 03:57:02 +00:00
parent 2fa19ff845
commit 404abfd198
2 changed files with 14 additions and 0 deletions

View File

@ -645,6 +645,13 @@ class NioDatagramWorker implements Runnable {
boolean connected = channel.isConnected();
boolean bound = channel.isBound();
try {
// It is necessary to cancel all keys before closing a socket
// because the shutdown flag in the Selector loop is set only when
// all keys are cancelled. Thus, SocketChannel.close() and
// SelectionKey.cancel() must be placed in a synchronized block.
// Otherwise SocketChannel.register() in RegisterTask can be called
// after cancel(), but before close(), resulting in the infinite
// Selector loop that refuses to shut down due to the dangling keys.
synchronized (channel.interestOpsLock) {
SelectionKey key = channel.getDatagramChannel().keyFor(selector);
if (key != null) {

View File

@ -559,6 +559,13 @@ class NioWorker implements Runnable {
boolean connected = channel.isConnected();
boolean bound = channel.isBound();
try {
// It is necessary to cancel all keys before closing a socket
// because the shutdown flag in the Selector loop is set only when
// all keys are cancelled. Thus, SocketChannel.close() and
// SelectionKey.cancel() must be placed in a synchronized block.
// Otherwise SocketChannel.register() in RegisterTask can be called
// after cancel(), but before close(), resulting in the infinite
// Selector loop that refuses to shut down due to the dangling keys.
synchronized (channel.interestOpsLock) {
SelectionKey key = channel.socket.keyFor(selector);
if (key != null) {