<< Back to previous view

[CCACHE-28] SoftCache NullPointerException in has? with many threads Created: 24/Oct/12  Updated: 04/Aug/14  Resolved: 04/Aug/14

Status: Closed
Project: core.cache
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: James Aley Assignee: Fogus
Resolution: Completed Votes: 0
Labels: None
Environment:

Linux, Oracle JRE 1.6


Attachments: Text File 0001-CCACHE-28-fixes-concurrency-bug-in-has.patch    
Patch: Code

 Description   

"cell" can be null on this call to SoftReference.get():
https://github.com/clojure/core.cache/blob/master/src/main/clojure/clojure/core/cache.clj#L501

If the cache did not contain "item" in the call to (get cache item) in the let binding, but another thread pre-empts and adds it before the subsequent call to contains?, we'll attempt to call .get() on nil.

Would it perhaps be sufficient to just check whether cell is nil instead of calling contains? in the check on the previous line?



 Comments   
Comment by Paul Stadig [ 09/Feb/13 6:13 AM ]

Good catch!

This is rather simple to reproduce:

(use 'clojure.core.cache)
(def cache (soft-reference-cache {}))
(def mutator (Thread. #(loop [] (assoc cache :foo :bar) (dissoc cache :foo) (recur))))
(.start mutator)
(loop [] (has? cache :foo) (recur))

The attached patch fixes the issue, by not referring back to the cache after attempting to pull out a SoftReference.

Comment by Ambrose Bonnaire-Sergeant [ 04/Aug/14 3:56 PM ]

Applied, thanks! https://github.com/clojure/core.cache/commit/8aac045991a0486a68d7a653cb02cff27d27c574

Generated at Tue Sep 02 12:32:27 CDT 2014 using JIRA 4.4#649-r158309.