8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection

Alan Bateman Alan.Bateman at oracle.com
Mon May 14 13:09:49 UTC 2018


This is a follow-up to the "Behaviour of SocketChannelImpl.close() in 
Java11 ..." thread where it was observed that closing a SocketChannel 
registered with a Selector does not obey the linger on close interval 
when enabled.

As discussed in that thread, the SO_LINGER socket option is not 
specified for channels configured non-blocking. At the same time, it can 
be useful to enable to a value of 0 to force a connection reset. As 
things stand, closing a socket channel with SO_LINGER enabled will obey 
the linger value when the channel is not registered with a Selector (it 
doesn't matter if the channel is configured blocking or non-blocking). 
It does not obey the linger value when the channel is registered with a 
Selector - this is because closing a SocketChannel registered with a 
Selector first shuts down the connection for writing (so that the peer 
reads EOF).

The following patches changes close so that SO_LINGER can be be used 
force a connection reset for the case that a channel is registered with 
a Selector and the channel is flushed from the Selector in timely 
manner. The linger interval > 0 case is too problematic and it can delay 
selection operations so the change will disable it and avoid selection 
operations blocking when they deregister closed channels. The test case 
exercises all the combinations and ensures that the behavior is 
consistent across all platforms.
     http://cr.openjdk.java.net/~alanb/8203059/webrev/

-Alan


More information about the nio-dev mailing list