Title: Using _testcapi.set_nomemory() in a test triggers a ThreadSanitizer "data race" · Issue #157415 · python/cpython · GitHub
Open Graph Title: Using _testcapi.set_nomemory() in a test triggers a ThreadSanitizer "data race" · Issue #157415 · python/cpython
X Title: Using _testcapi.set_nomemory() in a test triggers a ThreadSanitizer "data race" · Issue #157415 · python/cpython
Description: I recently added a test in test_io.test_memoryio using _testcapi.set_nomemory() to inject a MemoryError: commit 23eda00. The test was flagged as "data race" by clang ThreadSanitizer on a GHA CI run. The main thread calls _testcapi.set_no...
Open Graph Description: I recently added a test in test_io.test_memoryio using _testcapi.set_nomemory() to inject a MemoryError: commit 23eda00. The test was flagged as "data race" by clang ThreadSanitizer on a GHA CI run...
X Description: I recently added a test in test_io.test_memoryio using _testcapi.set_nomemory() to inject a MemoryError: commit 23eda00. The test was flagged as "data race" by clang ThreadSanitizer on a ...
Opengraph URL: https://github.com/python/cpython/issues/157415
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using _testcapi.set_nomemory() in a test triggers a ThreadSanitizer \"data race\"","articleBody":"I recently added a test in `test_io.test_memoryio` using `_testcapi.set_nomemory()` to inject a MemoryError: commit 23eda007f8d543495ba4935df75d243d76f9ef7f. The test was flagged as \"data race\" by clang ThreadSanitizer on a [GHA CI run](https://github.com/python/cpython/actions/runs/34737711156/job/103672505968?pr=157382).\n\n* The main thread calls `_testcapi.set_nomemory()` which calls `PyMem_SetAllocator()` which is implemented as `set_allocator_unlocked()`: its writes into `_PyRuntime.allocators.standard.raw` (type `PyMemAllocatorEx`).\n* The faulthandler thread calls `PyMem_RawFree()` which reads from `_PyRuntime.allocators.standard.raw`.\n\n`PyMem_SetAllocator()` acquires a lock, but `PyMem_RawFree()` doesn't acquire this lock.\n\nThis race condition was already seen with `tracemalloc`:\n\n* https://github.com/python/cpython/issues/143143#issuecomment-4081656148\n* https://github.com/python/cpython/issues/126315#issuecomment-3048731039\n\nUsing `_testcapi.set_nomemory()` in a multithreaded application is used, but this function should only be used for testing purpose. So I propose skipping the test if Python is built with TSAN (`./configure --with-thread-sanitizer` with clang).\n\nLogs:\n\n```\nWARNING: ThreadSanitizer: data race (pid=15518)\n Write of size 8 at 0x55b013af2f80 by main thread:\n #0 __tsan_memcpy \u003cnull> (python+0xfc452) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #1 set_allocator_unlocked /home/runner/work/cpython/cpython/Objects/obmalloc.c (python+0x38417f) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #2 PyMem_SetAllocator /home/runner/work/cpython/cpython/Objects/obmalloc.c:1143:5 (python+0x38417f)\n #3 fm_setup_hooks /home/runner/work/cpython/cpython/./Modules/_testcapi/mem.c:160:5 (_testcapi.cpython-316d-x86_64-linux-gnu.so+0x353e9) (BuildId: 97ab4e2a64a9d371fd1394f000a763262bd7b835)\n #4 fm_set_nomemory /home/runner/work/cpython/cpython/./Modules/_testcapi/mem.c:189:5 (_testcapi.cpython-316d-x86_64-linux-gnu.so+0x353e9)\n #5 set_nomemory /home/runner/work/cpython/cpython/./Modules/_testcapi/mem.c:202:5 (_testcapi.cpython-316d-x86_64-linux-gnu.so+0x34590) (BuildId: 97ab4e2a64a9d371fd1394f000a763262bd7b835)\n #6 cfunction_call /home/runner/work/cpython/cpython/Objects/methodobject.c:575:18 (python+0x34e22f) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #7 _PyObject_MakeTpCall /home/runner/work/cpython/cpython/Objects/call.c:242:18 (python+0x29318b) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #8 _PyObject_VectorcallTstate /home/runner/work/cpython/cpython/./Include/internal/pycore_call.h:142:16 (python+0x292ac9) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #9 PyObject_Vectorcall /home/runner/work/cpython/cpython/Objects/call.c:327:12 (python+0x294410) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n (...)\n\n Previous read of size 8 at 0x55b013af2f80 by thread T1:\n #0 PyMem_RawFree /home/runner/work/cpython/cpython/Objects/obmalloc.c:1239:5 (python+0x384578) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #1 pythread_wrapper /home/runner/work/cpython/cpython/Python/thread_pthread.h:234:5 (python+0x66c445) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n\n Location is global '_PyRuntime' of size 266672 at 0x55b013af2b70 (python+0xa46f80)\n\n Thread T1 (tid=15538, running) created by main thread at:\n #0 pthread_create \u003cnull> (python+0x1010de) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #1 do_start_joinable_thread /home/runner/work/cpython/cpython/Python/thread_pthread.h:283:14 (python+0x66b1db) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #2 PyThread_start_new_thread /home/runner/work/cpython/cpython/Python/thread_pthread.h:338:9 (python+0x66b28a) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #3 faulthandler_dump_traceback_later_impl /home/runner/work/cpython/cpython/./Modules/faulthandler.c:883:9 (python+0x6a4c64) (BuildId: 77d4ccbb4bf7bb80d66adcdbb8a43d9cd1f555bc)\n #4 faulthandler_dump_traceback_later /home/runner/work/cpython/cpython/./Modules/clinic/faulthandler.c.h:439:20 (python+0x6a4c64)\n (...)\n```\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157416\n* gh-157430\n* gh-159277\n* gh-159547\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-09-13T12:03:05Z","url":"https://github.com/python/cpython/issues/157415"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f7cabab2-0168-b181-70c8-a3040c362b29 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 956C:136CDA:11C8E02:1792D69:6B01E850 |
| html-safe-nonce | 861c21698d9ae0db1ba15f988caf0c0c87d8797001769fd5715775f15023f33e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTZDOjEzNkNEQToxMUM4RTAyOjE3OTJENjk6NkIwMUU4NTAiLCJ2aXNpdG9yX2lkIjoiNjYzNTkwMTAwMTU2NjEyMDAxNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 580ceb67e34f46a9da10c3392b62aef0d710e63c9c87dcd740ef516109b60af2 |
| hovercard-subject-tag | issue:5439925069 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/157415/issue_layout |
| twitter:image | https://opengraph.githubassets.com/803bcbc12c64e667a1c39ee95681ddae09b1b201fce37333003c77e71ea69af7/python/cpython/issues/157415 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/803bcbc12c64e667a1c39ee95681ddae09b1b201fce37333003c77e71ea69af7/python/cpython/issues/157415 |
| og:image:alt | I recently added a test in test_io.test_memoryio using _testcapi.set_nomemory() to inject a MemoryError: commit 23eda00. The test was flagged as "data race" by clang ThreadSanitizer on a GHA CI run... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width