JDK-7058336 : (so) Socket adpator is not synchronized on channel state
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 5.0u17
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux_redhat_5.0
  • CPU: x86
  • Submitted: 2011-06-23
  • Updated: 2012-01-26
  • Resolved: 2011-12-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
Other JDK 6
5.0u35Fixed 6u30-rev b22Fixed
When a customer try to do some operation for closed SocketChannel, the exception message does not seem appropriate.


 import java.nio.channels.*;

 public class TestSocket {
  public static void main(String[] args) throws Exception {
   SocketChannel sc = SocketChannel.open();
    Selector sel = Selector.open();
    sc.register(sel, 0);

Compile the attached test case and run

The following message will show up.
java.net.SocketException: Operation not supported
	at sun.nio.ch.Net.setIntOption0(Native Method)
	at sun.nio.ch.Net.setIntOption(Net.java:152)
	at sun.nio.ch.SocketChannelImpl$1.setInt(SocketChannelImpl.java:372)
	at sun.nio.ch.SocketOptsImpl.setBoolean(SocketOptsImpl.java:38)
	at sun.nio.ch.SocketOptsImpl$IP$TCP.noDelay(SocketOptsImpl.java:284)
	at sun.nio.ch.OptionAdaptor.setTcpNoDelay(OptionAdaptor.java:48)
	at sun.nio.ch.SocketAdaptor.setTcpNoDelay(SocketAdaptor.java:268)
	at com.sun.enterprise.web.connector.grizzly.SelectorThread.setSocketOptions(SelectorThread.java:1956)
	at com.sun.enterprise.web.connector.grizzly.SelectorThread.handleAccept(SelectorThread.java:1517)

"Operation not Supported" is EOPNOTSUPP.
So, the message,"Socket is closed" seems appropriate in this case
like the following result in jdk7.

Exception in thread "main" java.net.SocketException: Socket is closed
        at sun.nio.ch.Net.translateToSocketException(Net.java:105)
        at sun.nio.ch.SocketAdaptor.setBooleanOption(SocketAdaptor.java:288)
        at sun.nio.ch.SocketAdaptor.setTcpNoDelay(SocketAdaptor.java:321)
        at TestSocket.main(TestSocket.java:11)
Caused by: java.nio.channels.ClosedChannelException
        at sun.nio.ch.SocketChannelImpl.setOption(SocketChannelImpl.java:170)
        at sun.nio.ch.SocketAdaptor.setBooleanOption(SocketAdaptor.java:286)
        ... 2 more

EVALUATION one solution is to check if the socketChannel is closed before getting/setting various parameters

EVALUATION This bug is applicable to jdk6 and older. It is not applicable to jdk7 because the socket adpators were ssentially re-written in jdk7 as part of the socket-channel completion work. The basic issue in jdk6 and older is that the socket adpators aren't properly synchronized with the channel state. I've changed the synopsis of this bug to reflect the issue.