You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cayenne.apache.org by David Feshbach <dj...@gmail.com> on 2017/11/10 17:15:57 UTC

Race Condition in o.a.c.u.ReferenceMap.values() Causing NPE in ObjectStore.objectsInState(int)

This is in 4.1.M1

The stack trace is pretty short:
java.lang.NullPointerException
at
org.apache.cayenne.access.ObjectStore.objectsInState(ObjectStore.java:520)
at org.apache.cayenne.access.DataContext.newObjects(DataContext.java:242)
...

The problem is in ReferenceMap.values(). If the GC runs between
checkReferenceQueue() and the last v.get(), then null can be added to the
collection (when using the default WeakValueMap). keySet() and entrySet()
have similar issues.

I tried simply adding a check to exclude nulls from the collection, but I
get a test failure in WeakValueMapTest.testMapConstructor() because it
expects (a weak reference to) null to stay in the map.

What's the proper behavior here? Should weak mappings really allow null?
I'm surprised you're even allowed to create a reference with a null
referent.

Thanks,
David

Re: Race Condition in o.a.c.u.ReferenceMap.values() Causing NPE in ObjectStore.objectsInState(int)

Posted by Nikita Timofeev <nt...@objectstyle.com>.
Hi David,

Thanks for this issue, I've made a fix for it. See Jira ticket for details [1].
Basically I've made what you suggested, i.e. forbid null references in this map.

[1] https://issues.apache.org/jira/browse/CAY-2380

On Fri, Nov 10, 2017 at 8:15 PM, David Feshbach <dj...@gmail.com> wrote:
> This is in 4.1.M1
>
> The stack trace is pretty short:
> java.lang.NullPointerException
> at
> org.apache.cayenne.access.ObjectStore.objectsInState(ObjectStore.java:520)
> at org.apache.cayenne.access.DataContext.newObjects(DataContext.java:242)
> ...
>
> The problem is in ReferenceMap.values(). If the GC runs between
> checkReferenceQueue() and the last v.get(), then null can be added to the
> collection (when using the default WeakValueMap). keySet() and entrySet()
> have similar issues.
>
> I tried simply adding a check to exclude nulls from the collection, but I
> get a test failure in WeakValueMapTest.testMapConstructor() because it
> expects (a weak reference to) null to stay in the map.
>
> What's the proper behavior here? Should weak mappings really allow null?
> I'm surprised you're even allowed to create a reference with a null
> referent.
>
> Thanks,
> David



-- 
Best regards,
Nikita Timofeev