Title: Data race in `_PyImport_SetDLOpenFlags` when `sys.setdlopenflags()` is called concurrently · Issue #151644 · python/cpython · GitHub
Open Graph Title: Data race in `_PyImport_SetDLOpenFlags` when `sys.setdlopenflags()` is called concurrently · Issue #151644 · python/cpython
X Title: Data race in `_PyImport_SetDLOpenFlags` when `sys.setdlopenflags()` is called concurrently · Issue #151644 · python/cpython
Description: Bug report Bug description: In the free-threaded build, calling sys.setdlopenflags() concurrently from multiple threads (or concurrently with sys.getdlopenflags()) data-races on the per-interpreter dlopenflags field: _PyImport_GetDLOpenF...
Open Graph Description: Bug report Bug description: In the free-threaded build, calling sys.setdlopenflags() concurrently from multiple threads (or concurrently with sys.getdlopenflags()) data-races on the per-interpreter...
X Description: Bug report Bug description: In the free-threaded build, calling sys.setdlopenflags() concurrently from multiple threads (or concurrently with sys.getdlopenflags()) data-races on the per-interpreter...
Opengraph URL: https://github.com/python/cpython/issues/151644
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race in `_PyImport_SetDLOpenFlags` when `sys.setdlopenflags()` is called concurrently","articleBody":"# Bug report\n\n### Bug description:\n\nIn the free-threaded build, calling `sys.setdlopenflags()` concurrently from multiple threads (or concurrently with `sys.getdlopenflags()`) data-races on the per-interpreter `dlopenflags` field: `_PyImport_GetDLOpenFlags` / `_PyImport_SetDLOpenFlags` read and write `(interp)-\u003eimports.dlopenflags` with no atomics or lock.\n\nhttps://github.com/python/cpython/blob/9e863fab283eddca9c2a8f9d1ee30f4dc243e314/Python/import.c#L908-L918\n\n`sys.setdlopenflags()` / `sys.getdlopenflags()` expose these directly, so concurrent calls produce a non-atomic write/write (and read/write) race on the field.\n\nReproducer:\n```python\nimport sys\nfrom threading import Thread, Barrier\n\nN = 12\nbarrier = Barrier(N)\n\ndef worker():\n barrier.wait()\n for _ in range(20000):\n d = sys.getdlopenflags()\n sys.setdlopenflags(d)\n\nif __name__ == \"__main__\":\n threads = [Thread(target=worker) for _ in range(N)]\n for t in threads: t.start()\n for t in threads: t.join()\n```\n\nTSAN Report:\n```\n==================\nWARNING: ThreadSanitizer: data race (pid=2296999)\n Read of size 4 at 0x555555e3f648 by thread T1:\n #0 _PyImport_GetDLOpenFlags /cpython/Python/import.c:911:12 \n #1 sys_getdlopenflags_impl /cpython/./Python/sysmodule.c:1838:13 \n #2 sys_getdlopenflags /cpython/./Python/clinic/sysmodule.c.h:823:12 \n #3 cfunction_vectorcall_NOARGS /cpython/Objects/methodobject.c:508:24 \n #4 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11 \n #5 PyObject_Vectorcall /cpython/Objects/call.c:327:12 \n #6 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:768:11 \n #7 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1846:35 \n ...\n\n Previous write of size 4 at 0x555555e3f648 by thread T12:\n #0 _PyImport_SetDLOpenFlags /cpython/Python/import.c:917:25 \n #1 sys_setdlopenflags_impl /cpython/./Python/sysmodule.c:1819:5 \n #2 sys_setdlopenflags /cpython/./Python/clinic/sysmodule.c.h:796:20 \n #3 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2612:35 \n ...\n\n Location is global '_PyRuntime' of size 405824 at 0x555555e16c80 \n\nSUMMARY: ThreadSanitizer: data race /cpython/Python/import.c:911:12 in _PyImport_GetDLOpenFlags\n==================\n==================\nWARNING: ThreadSanitizer: data race (pid=2296999)\n Write of size 4 at 0x555555e3f648 by thread T12:\n #0 _PyImport_SetDLOpenFlags /cpython/Python/import.c:917:25 \n #1 sys_setdlopenflags_impl /cpython/./Python/sysmodule.c:1819:5 \n #2 sys_setdlopenflags /cpython/./Python/clinic/sysmodule.c.h:796:20 \n #3 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2612:35 \n ...\n\n Previous write of size 4 at 0x555555e3f648 by thread T9:\n #0 _PyImport_SetDLOpenFlags /cpython/Python/import.c:917:25 \n #1 sys_setdlopenflags_impl /cpython/./Python/sysmodule.c:1819:5 \n #2 sys_setdlopenflags /cpython/./Python/clinic/sysmodule.c.h:796:20 \n #3 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2612:35 \n ...\n\n Location is global '_PyRuntime' of size 405824 at 0x555555e16c80 \n\nSUMMARY: ThreadSanitizer: data race /cpython/Python/import.c:917:25 in _PyImport_SetDLOpenFlags\n==================\n```\n\n### CPython versions tested on:\n\n3.16\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-151768\n* gh-153789\n* gh-153790\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Naserume","@type":"Person","name":"Naserume"},"datePublished":"2026-06-18T11:09:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/151644/cpython/issues/151644"}
| 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:1a115a5d-2150-dfa5-95f7-4dba096f8b67 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8856:1FD712:12F9A20:1B788CD:6A58ABFE |
| html-safe-nonce | 178976b4014960aea44a2559b3af5a0745bf85e7fdce5eb0c96fc800920112ec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODU2OjFGRDcxMjoxMkY5QTIwOjFCNzg4Q0Q6NkE1OEFCRkUiLCJ2aXNpdG9yX2lkIjoiNjQ1ODExODIzMDU2NTc1OTk5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 29df9fa89d32ec1195c3e0db95831f05b4f7f2acad07532c687b2e28d495c566 |
| hovercard-subject-tag | issue:4691776235 |
| 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/151644/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bac8cdec08fdcf9ad9068728107129a168492a88fbe458f525c9aef646d503f3/python/cpython/issues/151644 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bac8cdec08fdcf9ad9068728107129a168492a88fbe458f525c9aef646d503f3/python/cpython/issues/151644 |
| og:image:alt | Bug report Bug description: In the free-threaded build, calling sys.setdlopenflags() concurrently from multiple threads (or concurrently with sys.getdlopenflags()) data-races on the per-interpreter... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Naserume |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| 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 | 8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width