MemorySegment.close() is not idempotent

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Mar 15 10:53:31 UTC 2021


Hi Remi,
this kind of issues is gonna be superseded by the upcoming work in:

https://github.com/openjdk/panama-foreign/pull/466

As part of that work, MemorySegment will lose the AutoCloseable 
interface, and lifecycle will be managed in a different entity, called 
ResourceScope (which is attached onto a segment).

Now, closing ResourceScope is still not idem-potent, but I believe that, 
with the new API, closing a scope twice is something you might want to 
get notified about, as it 99% signals a bug in your code.

I think it would be great if you could adjust the experiment to the new 
API and see how much of a problem that is in there?

P.S.
in the new API, if two resources (e.g. unsigned and signed) have same 
lifecycle, they should be backed by same ResourceScope abstraction.

Maurizio

On 15/03/2021 09:11, Remi Forax wrote:
> To play with panama.foreign, i've partially re-implement the Apache Arrow [1] columnar format in pure Java.
> So i've a bunch of dataset class that are wrappers of one or two MemorySegment (two if the data can be nullable) that represent a column of data.
>
> I stumble on is the fact that a MemorySegment can not be closed more than once.
>  From dataset, i can be wrapped to another one, think by example the unsigned int dataset being a wrapper on top on the signed one, but they share the same MemorySegment.
>
> As a user i want to write something like
>    try(var dataset = IntDataSet.from(MemorySegment.allocateMemory(5 * 4));
>        var unsignedDataset = dataset.asUnsigned()) {
>      ...
>    }
>
> the problem is that the MemorySegment.close() is not idempotent, so i've to check myself (with isAlive()) if the MemorySegment of a dataset has already been closed or not.
>
> Did i miss something ?
>
> regards,
> Rémi
>
> [1] https://arrow.apache.org/docs/index.html


More information about the panama-dev mailing list