From 34d94a21018e8172f4e57a96fb537427f9dbb251 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 30 Apr 2012 14:14:28 -0700 Subject: [PATCH] Handle a possible race condition --- Lib/functools.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/functools.py b/Lib/functools.py index e4458f4920c..8206c4a2229 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -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]