Skip to content

Flaky test test_lru_cache_threaded3 #131677

@colesbury

Description

@colesbury

Bug report

Seen locally. The with self.subTest(): is not safe to call from multiple threads concurrently because subTest modifies self._subtest, so you might end up with the wrong or None subtest in the call to testPartExecutor.

@threading_helper.requires_working_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 threading_helper.start_threads(threads):
pass

self._subtest = _SubTest(self, msg, params_map)
try:
with self._outcome.testPartExecutor(self._subtest, subTest=True):
yield
if not self._outcome.success:
result = self._outcome.result
if result is not None and result.failfast:
raise _ShouldStop
elif self._outcome.expectedFailure:
# If the test is expecting a failure, we really want to
# stop now and register the expected failure.
raise _ShouldStop
finally:
self._subtest = parent

test_lru_cache_threaded3 (test.test_functools.TestLRUC.test_lru_cache_threaded3) ... Warning -- Uncaught thread exception: AttributeError
Exception in thread Thread-21 (test):
Traceback (most recent call last):
  File "/raid/sgross/cpython/Lib/threading.py", line 1054, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/raid/sgross/cpython/Lib/threading.py", line 996, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/raid/sgross/cpython/Lib/test/test_functools.py", line 1937, in test
    with self.subTest(thread=i):
         ~~~~~~~~~~~~^^^^^^^^^^
  File "/raid/sgross/cpython/Lib/contextlib.py", line 148, in __exit__
    next(self.gen)
    ~~~~^^^^^^^^^^
  File "/raid/sgross/cpython/Lib/unittest/case.py", line 555, in subTest
    with self._outcome.testPartExecutor(self._subtest, subTest=True):
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/raid/sgross/cpython/Lib/contextlib.py", line 148, in __exit__
    next(self.gen)
    ~~~~^^^^^^^^^^
  File "/raid/sgross/cpython/Lib/unittest/case.py", line 81, in testPartExecutor
    self.result.addSubTest(test_case.test_case, test_case, None)
                           ^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'test_case'
ok

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions