diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-01-12 17:42:20 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-01-12 17:42:20 (GMT) |
commit | 617c7753ce010617b45ae3bfa53a90c07b51510a (patch) | |
tree | 974d15dcc99f3504be149746e9b4069e5a2db6cc /Lib/test/test_functools.py | |
parent | 798ad2742b6523250e5477557e3cc2588d6e6475 (diff) | |
parent | 42e1ea9a10aa6c3df40af3b7561d6251c8e2ac9c (diff) | |
download | cpython-617c7753ce010617b45ae3bfa53a90c07b51510a.zip cpython-617c7753ce010617b45ae3bfa53a90c07b51510a.tar.gz cpython-617c7753ce010617b45ae3bfa53a90c07b51510a.tar.bz2 |
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/test/test_functools.py')
-rw-r--r-- | Lib/test/test_functools.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index eeb3a22..63fe83e 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 |