Repository navigation
Description
Bug report
It seems like the problem comes from test_object_clear_managed_dict_ft_memory_error() of test_capi.test_object, a test added 2 days ago (commit 369d8bc).
I don't think that the problem comes from this specific test, but more from _testcapi.set_nomemory() and _testcapi.call_with_nomemory() which are not thread safe. The problem comes from PyMem_SetAllocator() which uses a lock whereas functions reading the allocation function don't use a lock.
I suppose that @support.nomemtest should be modified to skip the test if TSan is used.
I'm working on a fix.
Python built with ./configure --config-cache 'OPT=-O2 -g' --with-thread-sanitizer --with-pydebug --disable-gil CC=clang LD=clang.
Test: TSAN_OPTIONS="log_path=$PWD/san_log handle_segv=0" ./python -m test test_capi --matchfile=bisect -v; echo "exitcode => $?".
The command fails with exit code 66.
With bisect file:
test.test_capi.test_abstract.CAPITest.test_mapping_haskeystring
test.test_capi.test_capsule.CapsuleImportTests.test_error_from_import
test.test_capi.test_complex.CAPIComplexTest.test_py_c_pow
test.test_capi.test_config.CAPITests.test_config_set_cpu_count
test.test_capi.test_exceptions.Test_PyUnstable_Exc_PrepReraiseStar.test_reraise_orig
test.test_capi.test_function.FunctionTest.test_function_get_module
test.test_capi.test_getargs.Bytes_TestCase.test_c
test.test_capi.test_getargs.Bytes_TestCase.test_w_star
test.test_capi.test_mem.PyMemMallocDebugTests.test_pyobject_freed_is_freed
test.test_capi.test_misc.Test_testcapi.test_atomic_exchange_uint64
test.test_capi.test_misc.Test_testcapi.test_buildvalue_N
test.test_capi.test_misc.Test_testcapi.test_dict_iteration
test.test_capi.test_misc.Test_testcapi.test_pythread_tss_key_state
test.test_capi.test_object.CAPITest.test_object_clear_managed_dict_ft_memory_error
test.test_capi.test_slots.TypeSlotsTests.test_repeat_warning
test.test_capi.test_type.BuiltinStaticTypesTests.test_tp_bases_is_set
test.test_capi.test_unicode.CAPITest.test_fromstring
test.test_capi.test_unicode.CAPITest.test_tailmatch
test.test_capi.test_watchers.TestDictWatchers.test_watch_unassigned_watcher_id
test.test_capi.test_weakref.CAPIWeakrefTest.test_pyweakref_checkproxy
TSan log:
WARNING: ThreadSanitizer: data race (pid=2260396)
Write of size 8 at 0x000000dee638 by thread T13:
#0 __tsan_memcpy ??:? (python+0x760682) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
#1 set_allocator_unlocked /home/vstinner/python/main/Objects/obmalloc.c:? (python+0x9fec80) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
#2 PyMem_SetAllocator /home/vstinner/python/main/Objects/obmalloc.c:1152 (python+0x9fec80)
#3 fm_setup_hooks /home/vstinner/python/main/./Modules/_testcapi/mem.c:163 (_testcapi.cpython-316td-x86_64-linux-gnu.so+0x52303) (BuildId: 0eac47155c6133af464962558ffdbdb649d9b928)
#4 fm_set_nomemory /home/vstinner/python/main/./Modules/_testcapi/mem.c:189 (_testcapi.cpython-316td-x86_64-linux-gnu.so+0x52303)
#5 call_with_nomemory /home/vstinner/python/main/./Modules/_testcapi/mem.c:239 (_testcapi.cpython-316td-x86_64-linux-gnu.so+0x511b1) (BuildId: 0eac47155c6133af464962558ffdbdb649d9b928)
...
Previous read of size 8 at 0x000000dee638 by main thread:
#0 PyMem_Malloc /home/vstinner/python/main/Objects/obmalloc.c:1266 (python+0x9ff062) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
#1 list_allocate_array /home/vstinner/python/main/Objects/listobject.c:42 (python+0x957c14) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
#2 py_list_resize /home/vstinner/python/main/Objects/listobject.c:163 (python+0x957c14)
#3 list_resize /home/vstinner/python/main/Objects/listobject.c:128 (python+0x957c14)
#4 _PyList_AppendTakeRefListResize /home/vstinner/python/main/Objects/listobject.c:586 (python+0x957923) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
#5 _PyList_AppendTakeRef /home/vstinner/python/main/./Include/internal/pycore_list.h:53 (python+0xb4eb97) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
#6 _PyEval_EvalFrameDefault /home/vstinner/python/main/Python/generated_cases.c.h:3984 (python+0x7061fe) (BuildId: 088d0863e8351f5ea2fa99217aa4430d2048d6e2)
...
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Activity
vstinner commentedon Nov 17, 2026
I already made this change once 2 months ago: #157416. But I reverted the change a few days later: commit d1878cc, see issue gh-157415.
pythongh-162144: Skip `@nomemtest` when using a thread sanitizer (TSan)
pythongh-162144: Skip `@nomemtest` tests when using a thread sanitize…
gh-162144: Skip `@nomemtest` tests when using a thread sanitizer (TSa…
[3.15] gh-162144: Skip `@nomemtest` tests when using a thread sanitiz…
[3.14] gh-162144: Skip `@nomemtest` tests when using a thread sanitiz…