diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-01-12 16:34:33 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-01-12 16:34:33 (GMT) |
commit | 67796521dd22b3008788a75108b45f33d06f85dd (patch) | |
tree | f737a3560e40ee17de082ff471687956cb4a8904 /Lib/test/test_functools.py | |
parent | 9b8dcc6b1c18d5539735b61004d2e84b3e26cc8f (diff) | |
download | cpython-67796521dd22b3008788a75108b45f33d06f85dd.zip cpython-67796521dd22b3008788a75108b45f33d06f85dd.tar.gz cpython-67796521dd22b3008788a75108b45f33d06f85dd.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 b431e05..9ccd0ca 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 from weakref import proxy try: @@ -1364,6 +1365,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 |