RFR: 8312166: (dc) DatagramChannel's socket adaptor does not release carrier thread when blocking in receive

Jaikiran Pai jpai at openjdk.org
Tue Jul 18 13:41:14 UTC 2023


On Mon, 17 Jul 2023 10:10:20 GMT, Alan Bateman <alanb at openjdk.org> wrote:

> DatagramChannel is "virtual thread friendly"; when configured blocking, the receive/read methods release the carrier when there is no datagram to receive.
> 
> DatagramChannel's socket adaptor (used by DatagramSocket) is not currently virtual thread friendly. The original changes proposed to address this in JDK 19 interacted with the carrier thread's buffer cache so we decided to not include them at the time. The change proposed here are to address this issue, mostly by moving the implementation of the socket adaptor's send/receive methods into the DatagramChannel implementation so that a temporary direct buffer is not held when parked.
> 
> The only observable change should be to silly/broken code that changes a DatagramPacket's buffer (with setData or setLength) while a receive is in progress. Long standing, and undocumented, behavior would for setXXX to block while a receive is in progress, new behavior will be only access the DatagramPacket's buffer when a datagram is received. I can't think of anything that could rely on behavior like this.

These changes look OK to me. 

The one issue that I could notice was a potential data loss when you read incoming data into the `ByteBuffer` and then while transferring to a `DatagramPacket` can potentially transfer lesser than the read data if the `bufLength` of the `DatagramPacket` has changed. However as you already noted, this is an odd case and shouldn't ideally happen (although this current change does allow it to happen in theory, unlike previously). Plus I think the data loss case is already covered in the API documentation of `DatagramSocket.receive()` which states:


> If the message is longer than the packet's length, the message is truncated.


So I think this change is fine.

-------------

Marked as reviewed by jpai (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/14901#pullrequestreview-1535063126


More information about the nio-dev mailing list