Behavior of DatagramChannel vs ByteBuffers bound to MemorySessions

Cleber Muramoto cleber.muramoto at gmail.com
Fri Jun 7 17:00:57 UTC 2024


Hello, I'd like to understand why a disconnected DatagramChannel does not
support direct buffers derived from MemorySegments created from shared
Arenas.

The connected code path uses IOUtil, which obtains the address via
JavaNioAccess and the disconnected one obtains the address
via ((sun.nio.ch.DirectBuffer)bb).address(), which undergoes MemorySession
validation semantics.

Example:

----
  var buffer =
Arena.ofShared().allocate(1024).asByteBuffer().putInt(42).flip();

  var dc = DatagramChannel.open();
  dc.bind(new InetSocketAddress(10000));
  dc.join(InetAddress.getByName("225.0.0.0"),
NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress()));

  // works
  dc.connect(new InetSocketAddress("225.0.0.0", 1024));
  dc.write(buffer);
  dc.disconnect();

  // UnsupportedOperationException
  dc.send(buffer.rewind(), new InetSocketAddress("225.0.0.0", 1024));
----

Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/nio-dev/attachments/20240607/9272f2a5/attachment.htm>


More information about the nio-dev mailing list