Title: `ctypes` pointer writes are not thread safe · Issue #128182 · python/cpython · GitHub
Open Graph Title: `ctypes` pointer writes are not thread safe · Issue #128182 · python/cpython
X Title: `ctypes` pointer writes are not thread safe · Issue #128182 · python/cpython
Description: Bug report Bug description: Part of #127945. ctypes C data objects have an internal pointer for what they're looking at (b_ptr). This field itself is generally fine to read non-atomically, because ctypes objects don't tend to overwrite t...
Open Graph Description: Bug report Bug description: Part of #127945. ctypes C data objects have an internal pointer for what they're looking at (b_ptr). This field itself is generally fine to read non-atomically, because ...
X Description: Bug report Bug description: Part of #127945. ctypes C data objects have an internal pointer for what they're looking at (b_ptr). This field itself is generally fine to read non-atomically, beca...
Opengraph URL: https://github.com/python/cpython/issues/128182
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`ctypes` pointer writes are not thread safe","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nPart of #127945.\r\n\r\n`ctypes` C data objects have an internal pointer for what they're looking at (`b_ptr`). This field itself is generally fine to read non-atomically, because ctypes objects don't tend to overwrite the pointer that they're pointing to, but reading and writing the pointer's contents (such as via `memcpy`) isn't thread safe. This can be seen primarily with arrays:\r\n\r\n```python\r\nfrom threading import Thread\r\nimport ctypes\r\n\r\nbuffer = (ctypes.c_char_p * 10)()\r\n\r\ndef main():\r\n for i in range(100):\r\n buffer.value = b\"hello\"\r\n buffer[1] = b\"j\"\r\n\r\nthreads = [Thread(target=main) for _ in range(100)]\r\nfor thread in threads:\r\n thread.start()\r\n```\r\n\r\nThere's really only two options here, because the lockless `_Py_atomic` APIs can't be used for allocations of arbitrary sizes: add a critical section around all functions touching `b_ptr`, or just add a `PyMutex` around it.\r\n\r\nI think that a mutex is the right way to go here:\r\n\r\n- There's no chance of re-entrancy with `memcpy`.\r\n- AC doesn't work for some type slots (e.g. `__call__`), so they can't get wrapped with `@critical_section`. We'd need ugly wrapper functions.\r\n\r\n### CPython versions tested on:\r\n\r\nCPython main branch\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-128490\n* gh-132081\n* gh-132082\n* gh-132133\n* gh-132152\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ZeroIntensity","@type":"Person","name":"ZeroIntensity"},"datePublished":"2024-12-22T15:58:09Z","url":"https://github.com/python/cpython/issues/128182"}
| 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:56849ba9-6107-36b1-34ca-4797e970a01e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D21A:E0ADF:1A96640:217E581:6B022F41 |
| html-safe-nonce | e874867e5b7fbdca8e4d2bbb00df7d8d8d1c66c1b65860570b0f6ad082e35f84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjFBOkUwQURGOjFBOTY2NDA6MjE3RTU4MTo2QjAyMkY0MSIsInZpc2l0b3JfaWQiOiI1OTE2OTU4MTkxNDE0MjcwMDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5fcff18ffe8f89185a96eefc4e6e7535fdafa385c780c3290cccfc8f056e3667 |
| hovercard-subject-tag | issue:2754722630 |
| 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/128182/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6cd176f5819ad468d976f9740ed54e14dd5fd1c6c3006d59374cfcc8bdee40c8/python/cpython/issues/128182 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6cd176f5819ad468d976f9740ed54e14dd5fd1c6c3006d59374cfcc8bdee40c8/python/cpython/issues/128182 |
| og:image:alt | Bug report Bug description: Part of #127945. ctypes C data objects have an internal pointer for what they're looking at (b_ptr). This field itself is generally fine to read non-atomically, because ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ZeroIntensity |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width