<div dir="ltr">@Alan, <br><br>Yes, Arenas can be closed at any time. Still, from my understanding, this implicit restriction should only be imposed in code that does not invoke JavaNioAccess::acquireSession explicitly, like in NioSocketImpl or SocketChannelImpl.<br><br>Regarding safety issues, do you mean the possibility of the NIO layer accessing an invalid address? <br><br>In the case of interleaving JavaNioAccess::acquireSession & Arena::close, I think there are only two possible outcomes:<br><br>A)Arena is closed in Thread A, then Writer Thread B fails with IllegalStateException<br>B)Writer Thread B acquires the session, then Thread A fails with IllegalStateException<br><br>I don't think it is possible to reach a state where the NIO layer sees an invalid (freed) address (unless of course the user code messes with jdk internals by calling MemorySession::release0 explicitly in other thread).<br><br>As for "APIs that do asynchronous IO", are you refering to JDK internals where the user has no control over the thread performing the write operation? This is supported by, e.g., by AsynchronousSocketChannel<br><br>---<br> var buffer = Arena.ofShared().allocate(1024).asByteBuffer().putInt(42).flip();<br> var q = new LinkedBlockingQueue<>();<br> var asc = AsynchronousSocketChannel.open();<br> asc.connect(new InetSocketAddress("<a href="http://openjdk.org">openjdk.org</a>", 443)).get();<br><br> asc.write(buffer, null, new CompletionHandler<Integer, Void>() {<br><br> public void completed(Integer result, Void attachment) {<br> q.offer(result);<br> }<br><br> public void failed(Throwable exc, Void attachment) {<br> exc.printStackTrace();<br> q.offer(exc);<br> }<br> });<br><br> System.out.println(q.take()); // 4<br>---<br><br>Regards</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Jun 8, 2024 at 5:22 AM Alan Bateman <<a href="mailto:Alan.Bateman@oracle.com">Alan.Bateman@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 07/06/2024 18:00, Cleber Muramoto wrote:<br>
> Hello, I'd like to understand why a disconnected DatagramChannel does <br>
> not support direct buffers derived from MemorySegments created from <br>
> shared Arenas.<br>
><br>
><br>
A shared Arena can be closed at any time. There are safety guarantees <br>
that go along with that are problematic when doing I/O with byte buffers <br>
that are backed by a memory segment allocated. For APIs that do <br>
synchronous I/O then it may be possible to remove this restriction at <br>
some point. For APIs that do asynchronous I/O (as in initiate an I/O op <br>
on one thread, complete on another) then it is a harder problem.<br>
<br>
-Alan<br>
<br>
<br>
</blockquote></div>