Title: _testcapi._tuple_resize() leaks item references on allocation failure · Issue #159514 · python/cpython · GitHub
Open Graph Title: _testcapi._tuple_resize() leaks item references on allocation failure · Issue #159514 · python/cpython
X Title: _testcapi._tuple_resize() leaks item references on allocation failure · Issue #159514 · python/cpython
Description: Bug report _PyTuple_Resize() leaks references to the tuple's elements when the underlying PyObject_GC_Resize() fails. It sets the caller's pointer to NULL and frees the old tuple storage with PyObject_GC_Del(), but does not release the r...
Open Graph Description: Bug report _PyTuple_Resize() leaks references to the tuple's elements when the underlying PyObject_GC_Resize() fails. It sets the caller's pointer to NULL and frees the old tuple storage with PyObj...
X Description: Bug report _PyTuple_Resize() leaks references to the tuple's elements when the underlying PyObject_GC_Resize() fails. It sets the caller's pointer to NULL and frees the old tuple storage wi...
Opengraph URL: https://github.com/python/cpython/issues/159514
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_testcapi._tuple_resize() leaks item references on allocation failure","articleBody":"### Bug report\n\n`_PyTuple_Resize()` leaks references to the tuple's elements when the underlying `PyObject_GC_Resize()` fails. It sets the caller's pointer to NULL and frees the old tuple storage with `PyObject_GC_Del()`, but does not release the references held in that storage.\n\nThe old allocation remains valid after the failed resize, so its remaining elements must be decref'ed before it is freed. For a shrinking resize, the removed tail entries have already been cleared; the retained entries still need cleanup if resizing fails.\n\n### Reproducer\n\nRun this with a build that includes `_testcapi`. The helper creates a private copy of the input tuple, satisfying `_PyTuple_Resize()`'s ownership requirement. The requested size exceeds the GC allocator's checked size limit because of header overhead; this does not depend on exhausting available RAM.\n\n```python\nimport gc\nimport struct\nimport sys\nimport weakref\nimport _testcapi\n\nclass Item:\n pass\n\nitem = Item()\noriginal = (item,)\nref = weakref.ref(item)\n\ntry:\n # The helper creates a private copy of the tuple before resizing it.\n # Header overhead makes this exceed the GC allocator's size limit.\n _testcapi._tuple_resize(original, sys.maxsize // struct.calcsize(\"P\"))\nexcept MemoryError:\n pass\nelse:\n raise AssertionError(\"expected MemoryError\")\n\ndel original, item\ngc.collect()\nprint(\"element released:\", ref() is None)\nassert ref() is None\n```\n\nExpected: `element released: True`.\nActual: `element released: False`, followed by `AssertionError`.\n\n### CPython versions tested on\n\nReproduced with the unmodified runtime from CPython `main` at `21a36aa79ca0b2af49995fa1b9216b01ef98f195` (3.16.0a1+dev), with `--with-pydebug`. Only the new regression test was added; no runtime code was changed for the failing run.\n\nSource inspection also confirms the same cleanup path in release tags [v3.14.8](https://github.com/python/cpython/blob/v3.14.8/Objects/tupleobject.c#L1004-L1011) and [v3.15.0](https://github.com/python/cpython/blob/v3.15.0/Objects/tupleobject.c#L1084-L1091). Those versions were not executed for this report.\n\n### Operating systems tested on\n\nmacOS 27.0.1, Apple Silicon.\n\n### Related reports\n\nI searched for an existing report. #155053 concerns a caller dereferencing NULL after resize failure; its proposed fix does not address these leaked element references.\n\nThe bug was manually spotted and investigated, but AI tools assisted with the investigation, reproducer, and proposed fix. The reproducer and regression test were executed locally before submission.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159526\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/lagergren-oai","@type":"Person","name":"lagergren-oai"},"datePublished":"2026-10-16T08:23:37Z","url":"https://github.com/python/cpython/issues/159514"}
| 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:1a459d22-53bd-6fb8-260c-82b358791dd6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9CC6:3A8E64:1D7F5C2:26EFA8A:6B022F66 |
| html-safe-nonce | 8581cc315a50868eebb799b6d6b0e180b680f2793985f73029916444fc569a59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5Q0M2OjNBOEU2NDoxRDdGNUMyOjI2RUZBOEE6NkIwMjJGNjYiLCJ2aXNpdG9yX2lkIjoiNzg3MTEzMjM5NzU5OTg2MjYzMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2e714e5cf80d84f10a8f585836a4ed2de184fff4260c2a6eeb9a2dd05f53b462 |
| hovercard-subject-tag | issue:5880845658 |
| 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/159514/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2847eb6e30126990fce2b86b2a14f9a10b743f09df31bc1c161129cd9f4845b6/python/cpython/issues/159514 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2847eb6e30126990fce2b86b2a14f9a10b743f09df31bc1c161129cd9f4845b6/python/cpython/issues/159514 |
| og:image:alt | Bug report _PyTuple_Resize() leaks references to the tuple's elements when the underlying PyObject_GC_Resize() fails. It sets the caller's pointer to NULL and frees the old tuple storage with PyObj... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lagergren-oai |
| 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