core.cache

SoftCache can cause an NPE if reference has been queued before eviction

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test

Description

While using the SoftCache we found a NullPointerException could occur while attempting to clear the cache of any enqueued references. We think this is because the references had been enqueued (presumably as a result of GC) before being evicted. In our case the SoftCache is being used as a base for a TTLCacheQ.

The (slightly reduced) stack-trace is as follows:

java.lang.NullPointerException: null
	at java.util.concurrent.ConcurrentHashMap.replaceNode(ConcurrentHashMap.java:1106)
	at java.util.concurrent.ConcurrentHashMap.remove(ConcurrentHashMap.java:1097)
	at clojure.core.cache$clear_soft_cache_BANG_.invokeStatic(cache.clj:512)
	at clojure.core.cache$clear_soft_cache_BANG_.invoke(cache.clj:509)
	at clojure.core.cache.SoftCache.evict(cache.clj:558)
	at clojure.core.cache.SoftCache.without(cache.clj:521)
	at clojure.lang.RT.dissoc(RT.java:884)
	at clojure.core$dissoc.invokeStatic(core.clj:1503)
	at clojure.core$dissoc.invoke(core.clj:1496)
        ...

The exception is thrown in ConcurrentHashMap when attempting to get the hash-code of the key we're removing but since that key is null the call fails.

I have included a test which causes the same exception and a defensive fix which prevents the exception by not attempting to remove from cache unless we have found the corresponding item in rcache.

Activity

Hide
Sean Corfield added a comment -

Will be in 0.7.2

Show
Sean Corfield added a comment - Will be in 0.7.2

People

Vote (1)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: