]> granicus.if.org Git - python/commitdiff
Handle a possible race condition
authorRaymond Hettinger <python@rcn.com>
Mon, 30 Apr 2012 21:14:28 +0000 (14:14 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 30 Apr 2012 21:14:28 +0000 (14:14 -0700)
Lib/functools.py

index e4458f4920c5ec92245b3ad0032a3105c8513469..8206c4a2229ada390c8a9931a6661b3afcc9c5c7 100644 (file)
@@ -241,6 +241,12 @@ def lru_cache(maxsize=100, typed=False):
                         return result
                 result = user_function(*args, **kwds)
                 with lock:
+                    if key in cache:
+                        # getting here means that this same key was added to the
+                        # cache while the lock was released.  since the link
+                        # update is already done, we need only return the
+                        # computed result and update the count of misses.
+                        pass
                     if currsize < maxsize:
                         # put result in a new link at the front of the queue
                         last = root[PREV]