summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-01-12 19:42:20 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2017-01-12 19:42:20 +0200
commit42a6f161f56ed721fdbddad2f984964c78353036 (patch)
treebb4caabd9553910df55ec2981665ac3ec01dec2d /Lib
parente9d97e7152ba10c29a04ab4f2d4aa1e657a4f36f (diff)
parent979774a08dfd512591156977db395684704696ea (diff)
downloadcpython-42a6f161f56ed721fdbddad2f984964c78353036.tar.gz
Issue #28969: Fixed race condition in C implementation of functools.lru_cache.
KeyError could be raised when cached function with full cache was simultaneously called from differen threads with the same uncached arguments.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_functools.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index eeb3a227a2..63fe83e5db 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -7,6 +7,7 @@ import pickle
from random import choice
import sys
from test import support
+import time
import unittest
import unittest.mock
from weakref import proxy
@@ -1446,6 +1447,20 @@ class TestLRU:
pause.reset()
self.assertEqual(f.cache_info(), (0, (i+1)*n, m*n, i+1))
+ @unittest.skipUnless(threading, 'This test requires threading.')
+ def test_lru_cache_threaded3(self):
+ @self.module.lru_cache(maxsize=2)
+ def f(x):
+ time.sleep(.01)
+ return 3 * x
+ def test(i, x):
+ with self.subTest(thread=i):
+ self.assertEqual(f(x), 3 * x, i)
+ threads = [threading.Thread(target=test, args=(i, v))
+ for i, v in enumerate([1, 2, 2, 3, 2])]
+ with support.start_threads(threads):
+ pass
+
def test_need_for_rlock(self):
# This will deadlock on an LRU cache that uses a regular lock