Title: Structseq objects created with PyStructSequence_New() are not tracked by the GC · Issue #157443 · python/cpython · GitHub
Open Graph Title: Structseq objects created with PyStructSequence_New() are not tracked by the GC · Issue #157443 · python/cpython
X Title: Structseq objects created with PyStructSequence_New() are not tracked by the GC · Issue #157443 · python/cpython
Description: Bug report PyStructSequence_New() allocates the object with PyObject_GC_NewVar() but never calls PyObject_GC_Track(). Only structseq_new_impl() (calling the type from Python) and __replace__() track the result (bpo-37126, gh-145376). All...
Open Graph Description: Bug report PyStructSequence_New() allocates the object with PyObject_GC_NewVar() but never calls PyObject_GC_Track(). Only structseq_new_impl() (calling the type from Python) and __replace__() trac...
X Description: Bug report PyStructSequence_New() allocates the object with PyObject_GC_NewVar() but never calls PyObject_GC_Track(). Only structseq_new_impl() (calling the type from Python) and __replace__() trac...
Opengraph URL: https://github.com/python/cpython/issues/157443
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Structseq objects created with PyStructSequence_New() are not tracked by the GC","articleBody":"# Bug report\n\n`PyStructSequence_New()` allocates the object with `PyObject_GC_NewVar()` but never calls `PyObject_GC_Track()`. Only `structseq_new_impl()` (calling the type from Python) and `__replace__()` track the result (bpo-37126, gh-145376). All 45 C callers of `PyStructSequence_New()` in the tree, including `make_unraisable_hook_args()` in `Python/errors.c`, produce objects which the GC never sees, so a reference cycle through them is never collected.\n\nMost structseq objects hold only ints and strings, but `sys.UnraisableHookArgs` holds the exception and its traceback, which keeps the frames alive (the current frame, when the exception has no traceback, `format_unraisable_v()` synthesizes one from it). Storing the hook argument in something reachable from those frames creates an uncollectable cycle:\n\n```python\nimport gc\nimport sys\nimport weakref\n\nclass Holder:\n pass\n\ndef f():\n holder = Holder()\n def hook(unraisable):\n holder.unraisable = unraisable\n sys.unraisablehook = hook\n class C:\n def __del__(self):\n raise ValueError\n C()\n print(gc.is_tracked(holder.unraisable))\n return weakref.ref(holder)\n\nwr = f()\nsys.unraisablehook = sys.__unraisablehook__\ngc.collect()\nprint(wr())\n```\n\nOutput (3.13 and main):\n\n```\nFalse\n\u003c__main__.Holder object at 0x7a7fda9d22c0>\n```\n\nExpected: `True` and `None`.\n\n`test.support.catch_unraisable_exception()` works around this by deleting its `unraisable` attribute on exit (bpo-37261). I hit it in `test_class.test_detach_materialized_dict_no_memory()`, which leaks about 140 references per run as soon as it keeps `ex.unraisable` in a local variable.\n\nThe simplest fix is to call `_PyObject_GC_TRACK()` in `PyStructSequence_New()`, as `PyTuple_New()` does, and drop the explicit calls in `structseq_new_impl()` and `structseq_replace()`. `Py_VISIT()` handles the NULL items of a partially filled object.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157447\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2026-09-13T17:10:29Z","url":"https://github.com/python/cpython/issues/157443"}
| 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:d43ae4e7-0995-911e-1685-4a4bd96d574a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B6CA:A71F9:310DCB8:3E1A4E3:6B020167 |
| html-safe-nonce | 5112630a0d86fece6d71d0abdd14f00cd6002afa1d319520446f8a2874004d89 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNkNBOkE3MUY5OjMxMERDQjg6M0UxQTRFMzo2QjAyMDE2NyIsInZpc2l0b3JfaWQiOiIzNjE4OTUxNjIxNjM3MTc3NzAzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5c76a6b654d8b494254b38268be65eadde637f5f1a2f8d6f0f7d8def34960cfe |
| hovercard-subject-tag | issue:5441505480 |
| 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/157443/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e972383b9ad8a6cadcb705c5ca48462b4a273853d36a77fd1f422631bb23bd42/python/cpython/issues/157443 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e972383b9ad8a6cadcb705c5ca48462b4a273853d36a77fd1f422631bb23bd42/python/cpython/issues/157443 |
| og:image:alt | Bug report PyStructSequence_New() allocates the object with PyObject_GC_NewVar() but never calls PyObject_GC_Track(). Only structseq_new_impl() (calling the type from Python) and __replace__() trac... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | serhiy-storchaka |
| 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