[foreign-jextract] RFR: Minor change for possible race in shared resource list

Radoslaw Smogura github.com+7535718+rsmogura at openjdk.java.net
Sat Apr 24 13:31:38 UTC 2021


On Sat, 24 Apr 2021 11:06:32 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> (1) One or more threads can see "prev" as CLOSED_LIST. Even those CAS will be performed
>> and list head will be set to new cleanup. Than exception is going to be thrown.
>> 
>> (2) Than cleanup will continue (maybe skipping just added cleanup).
>> 
>> If in addition, other thread will join in parallel to (1) with cleanup2, than
>> queue could look like this HEAD = cleanup2 -> list end marker.
>
> src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/SharedScope.java line 142:
> 
>> 140:                     throw new IllegalStateException("Already closed");
>> 141:                 }
>> 142:                 cleanup.next = prev;
> 
> Why not moving this assigment inside the `if` block (e.g. when `add` succeeded) and then remove the `cleanup.next = null`  (since the assignment would never had been executed in that case) ?

Do you mean something like this?

if (FST.compareAndSet(this, prev, cleanup)) {
cleanup.next = prev;
}

It could create race with clean block, as clean block could read and process new head, before the pointer to next has been assigned.

(I actually bit wonder, if this cleaning is required, I thought about case that this class will be kept by someone, and it can create some memory leak, but I think there's no many places where it can happen)

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

PR: https://git.openjdk.java.net/panama-foreign/pull/519


More information about the panama-dev mailing list