8232673: (dc) DatagramChannel socket adaptor issues

Alan Bateman Alan.Bateman at oracle.com
Sun Oct 27 14:16:51 UTC 2019


DatagramChannel::socket returns a socket adaptor, essentially a 
DatagramSocket view on the channel. The original motivation for the 
socket method was binding and socket options but it also created the 
potential for migration or interop with code using the older 
DatagramSocket API. The adaptor has been on my radar for sometime as it 
implements a blocking receive method that relies on the channel's 
blockingLock. This locking is problematic for several reasons, including 
preventing a send to proceed when a thread is blocked in receive. The 
main change in the patch proposed here is to re-implememt the adaptor's 
send and receive methods so they can execute concurrently and don't 
block while holding a built-in monitor. I've also changed these methods 
to use the buffer cache mechanism and avoid creating a ByteBuffer per 
I/O operation. The new receive implementation also works around two 
flaws in the legacy DatagramPacket API/implementation (discussed in 
JDK-8232817).

While in the area, a few other smaller issues are fixed too:

1. A connected DatagramChannel needs to disconnect before connecting it 
to another address. DatagramSocket doesn't require this. The adaptor is 
changed to allow it connect without throwing an exception. The connect 
methods are also fixed to throw IllegalArgumentException rather than 
NullPointerException when invoked with a null address.

2. The adaptor's getLocalSocketAddress method returns the local address 
when channel was closed. This is an inconsistency in DatagramSocket's 
API that was missed in the adaptor.

I've expanded the test coverage. The new AdaptorGetters test attempts to 
exercise all the getter methods when the channel is in different states 
so gives us confidence that there aren't further issues with these methods.

The webrev with the proposed changes is here:
     http://cr.openjdk.java.net/~alanb/8232673/webrev/index.html

-Alan


More information about the nio-dev mailing list