<< Back to previous view

[CMEMOIZE-15] ttl functions sometimes return nil Created: 01/Jul/14  Updated: 10/May/17

Status: Open
Project: core.memoize
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Ryan Fowler Assignee: Fogus
Resolution: Unresolved Votes: 4
Labels: None

Attachments: File CMEMOIZE-15-RETRY.diff     File fix-CMEMEMOIZE-15.diff     File ttl_bug.diff    
Patch: Code and Test


If I (memoize/ttl) a function, near the threshold time the result from calling the memoized function will sometimes be nil.

I've attached a patch that exploits the problem in the unit tests.

Reproducing in the REPL is easy. Just call memoize/ttl fn a bunch of times and watch for nil results.

user=> (require '[clojure.core.memoize :as m])

user=> (def f (m/ttl (constantly 1)))

user=> (dotimes [n 1000000] (when (nil? (f)) (println "nil f")))
nil f
nil f
nil f
nil f
nil f

The problem seems to be that when clojure.core.cache/through gets called, the item hasn't expired, but when clojure.core.cache/lookup happens in build-memoizer, the expiration has passed.

Comment by Ryan Fowler [ 01/Jul/14 8:22 PM ]

I'm out of my depth here, but this patch seems to resolve my problem. It looks in the cache before & after the (through*) swap, picks the "after" value if non-nil and "before" value if the "after" value is nil.

Comment by Colin Jones [ 08/Jul/14 5:41 PM ]

One alternative would be to repeat the

(swap! cache through* f args)
if the value is nil.

Something like

-            (and val @val)))
+            (if (nil? val)
+              (swap! cache through* f args)
+              @val)))

There are a few benefits: less code, only expiration forces you to pay for an additional cache lookup, and it seems like it might be preferable semantically (you don't return a known-to-be-expired value).

A downside is that you may do multiple cache writes if the ttl is low enough (not sure if you can do sub-millisecond ttls?) or the thread scheduling is wonky enough to make that happen. But retries can happen with swap! regardless, so I'm not sure this drawback is significant.

p.s. You should also make sure to check out http://dev.clojure.org/display/community/Developing+Patches and use git format-patch for your patch, since the current ones don't have a `git am`-friendly format.

Comment by Christophe Grand [ 10/Jul/14 4:50 AM ]

Isn't the problem the TTL cache impl itself? CacheProtocol is purely functional but TTLCache is not (has? or lookup are time sensitive). Cache-invalidation on write (rather than on read) may be a solution that wouldn't require changing the protocol.

Comment by Ryan Fowler [ 10/Jul/14 1:47 PM ]

Thanks Guys.

Chris, I think you're on to something. If TTLCache/lookup doesn't filter out results based on time, things make a whole lot more sense and the TTLCache acts more like normal Clojure.

(miss) and (evict) are the only functions that actually remove stuff from the cache, so one of those functions needs to get called regularly anyway.

Comment by Colin Jones [ 11/Jul/14 10:49 AM ]

Maybe I'm just being dense, but I'd personally be surprised if an implementation of CacheProtocol's lookup fn gave me back a value that should be expired according to the TTL. Imagine if your TTL was 30 seconds, and you did a read 10 minutes after your last write - calling lookup and getting a 10-minute-old value seems problematic.

Comment by Ryan Fowler [ 04/Nov/14 9:49 AM ]

Implementation of Colin Jones' suggestion to retry swap!/lookup when a cache lookup indicates that the cache entry doesn't exist.

Comment by Arthur Maciejewicz [ 03/Aug/16 3:47 PM ]

We ran into this issue as well, and the temporary fix if anyone else needs it, is to call the memoized function a second time, which results in a cache miss and returns a new value.

@Colin nil is an unexpected value because in the case of a cache miss, the key's value will be repopulated. However, in this scenario, has? returns both true and false in the same function call.

Comment by Ryan Fowler [ 03/Aug/16 4:08 PM ]

For what it's worth, we've been running with CMEMOIZE-15-RETRY.diff in production for well over a year without issue. It's a shame that this hasn't been fixed in a proper release of either core.memoize or core.cache.

Comment by Alex Miller [ 06/Aug/16 7:51 AM ]

Unless I'm missing something, it seems like this patch hides the issue rather than fixes it? That doesn't seem like the right "fix".

Comment by Joshua Tilles [ 08/Aug/16 12:05 PM ]

I agree with Alex: the “retry if nil” strategy feels like a hack to me, although I think it’s the most appropriate workaround in the absence of an official fix.

Here’s an alternative approach:

diff --git a/src/main/clojure/clojure/core/memoize.clj b/src/main/clojure/clojure/core/memoize.clj
index 5b448eb..a18ebfe 100644
--- a/src/main/clojure/clojure/core/memoize.clj
+++ b/src/main/clojure/clojure/core/memoize.clj
@@ -159,7 +159,11 @@
         (fn [& args]
           (let [cs  (swap! cache through* f args)
-                val (clojure.core.cache/lookup cs args)]
+                ;; Since `cache/through` (via `through*`) already
+                ;; handled hitting & missing, treat the resulting `cs`
+                ;; as a “snapshot” and sidestep any call to `cache/lookup`.
+                val (get (.cache (.cache ^PluggableMemoization cs))
+                         args)]
             ;; The assumption here is that if what we got
             ;; from the cache was non-nil, then we can dereference
             ;; it.  core.memo currently wraps all of its values in

With this change, all of the tests still pass, including the new test from the attached ttl_bug.diff.

However, it’s more of a proof-of-concept than a viable fix (hence its living in this comment instead of being attached as a patch) because it introduces reflection (the outer .cache) by relying on the implementation detail that all of the cache implementations defined with defcache in clojure.core.cache have a raw backing map in a field named cache.

Of course, the reflection could be resolved with a new protocol method. I don’t yet have an opinion on whether it would be “better” to expand clojure.core.cache/CacheProtocol or to define a new protocol, nor if such a new protocol should live in clojure.core.cache or clojure.core.memoize.

Comment by Joshua Tilles [ 08/Aug/16 12:17 PM ]

Possible names for the prospective protocol method:

  • raw-backing-map
  • backing-data
  • snapshot
    • I am aware that core.memoize already defines its own snapshot function, but I don’t think there would be any problem if the protocol method lived in core.cache.

And for the protocol itself (assuming the method isn’t just added to clojure.core.cache/CacheProtocol):

  • Transparence
  • Snapshottable
Comment by Joshua Tilles [ 17/Aug/16 11:01 AM ]

Alex Miller (or Fogus, of course) any thoughts on the strategy described above? Is it worth fleshing out?

Comment by Peter Monks [ 10/May/17 12:04 PM ]

I hate to be "that guy", but are there any updates on when this will be fixed in an official release?

IMHO this is a pretty serious bug, and given that there are several patches floating around that address it (from Colin Jones & Joshua Tilles above, and also at least one Github fork) it seems like the only barrier to doing so is time & interest. Are those things the community can help with?

[CMEMOIZE-9] memo-swap! is misnamed Created: 23/Sep/13  Updated: 23/Sep/13

Status: Open
Project: core.memoize
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Mark Engelberg Assignee: Fogus
Resolution: Unresolved Votes: 0
Labels: None


In order to be analogous to atoms, the current behavior of memo-swap! should be called memo-reset! since it overwrites the entire memo map. Similarly, there should be a function called memo-swap! that does an atomic swap operation on the memo cache so, for example, you can add a single value to the cache.

[CMEMOIZE-18] Confusing / erroneous documentation regarding seed values for clojure.core.memoize/memo Created: 03/Nov/14  Updated: 03/Nov/14

Status: Open
Project: core.memoize
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Minor
Reporter: Mr. Rm Assignee: Fogus
Resolution: Unresolved Votes: 0
Labels: documentation, memo

JVM 1.7, 1.8. Clojure 1.6


The documentation gives an example of passing a seed value to memo as a simple number. However, that does not work. The documentation is here: http://clojure.github.io/core.memoize/#clojure.core.memoize/memo

This does work:
((clojure.core.memoize/memo + {[42 1] (delay 99)}) 42 1)

This fails when it tries to deref 99:
((clojure.core.memoize/memo + {[42 1] 99}) 42 1)

[CMEMOIZE-20] Option for snapshot to return a lazy sequence Created: 23/Aug/15  Updated: 06/Nov/15

Status: Open
Project: core.memoize
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Armando Blancas Assignee: Fogus
Resolution: Unresolved Votes: 0
Labels: None



It'll be helpful to get a cache snapshot as a lazy sequence and thus avoiding having the whole cache and the snapshot in memory at once. As I'm working with big caches I've coded my own version of snapshot but would rather depend on a library function.

(defn snapshot
  "Custom version of memo/snapshot that returns a lazy sequence of
  [k v] pairs instead of an eagerly-built map."
  (when-let [cache (:clojure.core.memoize/cache (meta fn))]
    (for [[k v] (.cache ^clojure.core.memoize.PluggableMemoization @cache)]
      [(vec k) @v])))

Generated at Tue May 23 04:02:25 CDT 2017 using JIRA 4.4#649-r158309.