Title: Reference counting bug with manually allocated heap types · Issue #128923 · python/cpython · GitHub
Open Graph Title: Reference counting bug with manually allocated heap types · Issue #128923 · python/cpython
X Title: Reference counting bug with manually allocated heap types · Issue #128923 · python/cpython
Description: Bug report Found by @vfdev-5. This is specific to the free threading build and 3.14. XLA/Jax uses the following code to create a heap type: // We need to use heap-allocated type objects because we want to add // additional methods dynami...
Open Graph Description: Bug report Found by @vfdev-5. This is specific to the free threading build and 3.14. XLA/Jax uses the following code to create a heap type: // We need to use heap-allocated type objects because we ...
X Description: Bug report Found by @vfdev-5. This is specific to the free threading build and 3.14. XLA/Jax uses the following code to create a heap type: // We need to use heap-allocated type objects because we ...
Opengraph URL: https://github.com/python/cpython/issues/128923
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Reference counting bug with manually allocated heap types","articleBody":"# Bug report\n\nFound by @vfdev-5.\n\nThis is specific to the free threading build and 3.14.\n\nXLA/Jax uses the following code to create a heap type:\n\n```c++\n // We need to use heap-allocated type objects because we want to add\n // additional methods dynamically.\n...\n nb::str name = nb::str(\"PmapFunction\");\n nb::str qualname = nb::str(\"PmapFunction\");\n PyHeapTypeObject* heap_type = reinterpret_cast\u003cPyHeapTypeObject*\u003e(\n PyType_Type.tp_alloc(\u0026PyType_Type, 0));\n // Caution: we must not call any functions that might invoke the GC until\n // PyType_Ready() is called. Otherwise the GC might see a half-constructed\n // type object.\n CHECK(heap_type) \u003c\u003c \"Unable to create heap type object\";\n heap_type-\u003eht_name = name.release().ptr();\n heap_type-\u003eht_qualname = qualname.release().ptr();\n ...\n```\n\nhttps://github.com/openxla/xla/blob/19a8e8e05fb34c5c4b8c38c9a8225e89f008c8c1/xla/python/pmap_lib.cc#L1027-L1058\n\nIn other words, the heap type is created by by calling `PyType_Type.tp_alloc` and filling in the fields, instead of the more common use of `PyType_FromSpec`. This leaves [`unique_id`](https://github.com/python/cpython/blob/211f41316b7f205d18eb65c1ececd7f7fb30b02d/Include/cpython/object.h#L284-L286) zero initialized. The problem is that `unique_id=0` currently looks like a valid unique id for per-thread reference counting, which leads to reference counting errors and use-after-frees.\n\nI think we should change the per-thread reference counting so that `unique_id=0` is the sentinel value indicating that it's not assigned instead of the current `unique_id=-1` convention.\n\n### Full repro\n\n* https://gist.github.com/vfdev-5/a2f8f0716611afe2e0721c4332bedcd5\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-128925\n* gh-128951\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2025-01-16T19:29:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/128923/cpython/issues/128923"}
| 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:a2a00816-9d2d-3fe8-b841-321b88dcc5be |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 80E2:F5661:2428B76:3120ADC:696AD179 |
| html-safe-nonce | 57c6d9d10426376ad5f79915f7b7b4772cfe08a30ac6584b1ab9d2f63135cc16 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MEUyOkY1NjYxOjI0MjhCNzY6MzEyMEFEQzo2OTZBRDE3OSIsInZpc2l0b3JfaWQiOiIxNjA4MDY1Nzk2MzkyMjc2MzQ1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c62bef3588844baf3f885c234a778436f5f3e621b59d53339a83a7dc14b7d57d |
| hovercard-subject-tag | issue:2793601964 |
| 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/128923/issue_layout |
| twitter:image | https://opengraph.githubassets.com/25e550436f77f5baa87db06717c47ae20917c6cbc8fd72c92ccd9b9c8dea3b5e/python/cpython/issues/128923 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/25e550436f77f5baa87db06717c47ae20917c6cbc8fd72c92ccd9b9c8dea3b5e/python/cpython/issues/128923 |
| og:image:alt | Bug report Found by @vfdev-5. This is specific to the free threading build and 3.14. XLA/Jax uses the following code to create a heap type: // We need to use heap-allocated type objects because we ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | colesbury |
| hostname | github.com |
| expected-hostname | github.com |
| None | c785f4ce187e9e7331257791b36ddee01625bb8e292a9b4fe2c16d4c006abf5d |
| 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 | c718a376fcf780eb22089171adb84a543f660bf7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width