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


  • 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


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(
	at java.util.concurrent.ConcurrentHashMap.remove(
	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(
	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.


Sean Corfield added a comment -

Will be in 0.7.2

Sean Corfield added a comment - Will be in 0.7.2


Vote (1)
Watch (0)


  • Created: