IdleStateAwareChannelHandler.channelDisconnected(...) is not called intermittently

Daniel Ferber dffforum at gmail.com
Wed Nov 10 15:57:50 EST 2010


After more investigation, I have a supposition that there is a race condition
in NioWorker.close() when the channel is requested to close by a thread that
is not a worker thread.

The HashedWheelTimer thread runs the idle event that calls
Channels.disconnect() and that indirectly calls NioWorker.close(), causing
the socket to be closed. Immediately, the worker thread awakens (because it
detected that the socket is now closed) and also calls NioWorker.close().

The race condition might cause incorrect execution if:
One thread reads "connected = channel.isConnected();" to be true, closes the
socket and calls "channel.setClosed()". The setClosed() changes the internal
status to "closed" but does not yet enter the synchronized block to update
the channelFuture. Under unfortunate conditions, the JVM schedules the
second threads just at this moment.

The second thread reads "connected = channel.isConnected();" to be FALSE(!),
since it was recently updated to "closed" by the first thread. The second
threads closes the socket again (with no effect), enters setClosed(), enters
the synchronized code that updates the ChannelFuture and gets permission to
enter the block that dispatches the events. However, the second thread
falsely believes that the socket was not connected nor bound, and skips the
channelDisconnected and channelUnbound events.

The first threads is scheduled again, since the channelFuture was already
updated, it does not gain access to the events block and returns.

Perhaps the complete Channel.setClosed() should be synchronized to prevent
both Threads from updating the channel status at the same time, and to
prevent from one thread read an inconsistent result.

Or even better, as already suggested in the jira, have all events, including
timer events, be executed by only one thread.
-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/IdleStateAwareChannelHandler-channelDisconnected-is-not-called-intermittently-tp5724710p5726494.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list