[code-reflection] RFR: Explicit Arena passing to Tensor construction and Session execution [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Mar 4 15:42:17 UTC 2025


On Tue, 4 Mar 2025 15:08:07 GMT, Adam Sotona <asotona at openjdk.org> wrote:

>>> I don't see a problem here. Users can also close a stream while reading it.
>> 
>> Yes, and that would cause an exception -- not a VM crash.
>
> Following exception is thrown when you run a session constructed with already closed arena: 
> 
> java.lang.AssertionError: should not reach here
> 	at oracle.code.onnx.foreign.OrtApi$SessionGetInputCount.invoke(OrtApi.java:1891)
> ...
> Caused by: java.lang.IllegalStateException: Already closed
> 	at java.base/jdk.internal.foreign.MemorySessionImpl.alreadyClosed(MemorySessionImpl.java:305)
> 
> 
> However yes, VM crashes are more frequent when playing with Onnx.

I think what saves us from crashes in the current code is the the fact that `sessionAddress` needs to be passed to the various onnx API calls. And, since `sessionAddress` points at the session arena, the `Linker` will not allow that arena to be closed until the downcall has finished running (this is part of the contract specified in tthe Linker API). Which probably avoids the worst cases.

As long as the lifetimes of the results of `run` are completely independent from that of the session (in the sense that it is ok for such results to "outlive" the session), then it should be ok -- but I couldn't find any guidance in the onnx API docs. The risk is that some of those results holds off to some memory of the session, and when the session is released, the result will point to already freed memory -- but I don't know if this could happen.

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

PR Review Comment: https://git.openjdk.org/babylon/pull/337#discussion_r1979708930


More information about the babylon-dev mailing list