From 67796521dd22b3008788a75108b45f33d06f85dd Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 12 Jan 2017 18:34:33 +0200 Subject: 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. --- Lib/test/test_functools.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'Lib/test/test_functools.py') diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index b431e05764..9ccd0cafc4 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 -- cgit v1.2.1