Title: Segfault from _PyDict_NewKeysForClass under memory pressure · Issue #153182 · python/cpython · GitHub
Open Graph Title: Segfault from _PyDict_NewKeysForClass under memory pressure · Issue #153182 · python/cpython
X Title: Segfault from _PyDict_NewKeysForClass under memory pressure · Issue #153182 · python/cpython
Description: Crash report What happened? import _testcapi def make_class(): class C: def f(self): self.x = 1 return C for start in range(100): _testcapi.set_nomemory(start, start + 1) try: x = make_class() except MemoryError: pass fish: Job 1, './pyt...
Open Graph Description: Crash report What happened? import _testcapi def make_class(): class C: def f(self): self.x = 1 return C for start in range(100): _testcapi.set_nomemory(start, start + 1) try: x = make_class() exce...
X Description: Crash report What happened? import _testcapi def make_class(): class C: def f(self): self.x = 1 return C for start in range(100): _testcapi.set_nomemory(start, start + 1) try: x = make_class() exce...
Opengraph URL: https://github.com/python/cpython/issues/153182
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault from _PyDict_NewKeysForClass under memory pressure","articleBody":"# Crash report\n\n### What happened?\n\n```python\nimport _testcapi\n\ndef make_class():\n class C:\n def f(self):\n self.x = 1\n\n return C\n\nfor start in range(100):\n _testcapi.set_nomemory(start, start + 1)\n try:\n x = make_class()\n except MemoryError:\n pass\n```\n\n```\nfish: Job 1, './python.exe staticattrs.py' terminated by signal SIGSEGV (Address boundary error)\n```\n\nIf an allocation failure happens when defining a class that has a non-empty `__static_attributes__`, then you can get a null pointer deref.\n\n### Root cause\n\nEarly during the class build process, `__static_attributes__` is correctly populated with the list of detected static attributes.\n\nThen later during PyType_Ready(): `type_ready_managed_dict` is called to make the shared key cache.\nThis calls `_PyDict_NewKeysForClass` and this line:\nhttps://github.com/python/cpython/blob/9f9787d83462f5b44cb59e3c69e7c1799612a33f/Objects/dictobject.c#L7219\n\nTries to allocate a keys object, and if that allocation fails, just clears the `MemoryError` and continues, skipping out some keys setup steps.\n\nHowever, later on in that `_PyDict_NewKeysForClass` it fetches the `__static_attributes__` tuple and tries to populate them into the keys object:\nhttps://github.com/python/cpython/blob/9f9787d83462f5b44cb59e3c69e7c1799612a33f/Objects/dictobject.c#L7232-L7238\n\nAnd `insert_split_key` reasonably assumes that `keys` is a valid Keys object, resulting in a relative pointer from NULL dereferenec.\n\n\n### Stacktrace\n\n```text\nframe #0 _DK_ENTRIES(dk=\u003cunavailable\u003e) at pycore_dict.h:269\nframe #1 do_lookup(mp=0x0, dk=0x0, ...) at dictobject.c:1090\nframe #2 unicodekeys_lookup_unicode(dk=0x0, ...) at dictobject.c:1183\nframe #3 insert_split_key(keys=0x0, ...) at dictobject.c:1931\nframe #4 _PyDict_NewKeysForClass(cls=0x0000aaaaabfffa70) at dictobject.c:7236\nframe #5 type_ready_managed_dict(type=0x0000aaaaabfffa70) at typeobject.c:9473\nframe #6 type_ready(...) at typeobject.c:9578\nframe #7 PyType_Ready(...) at typeobject.c:9622\n```\n\nInterestingly bisect failed here, because before this change, there was a different allocation failure segfault from class definition, so either this one is shadowing the other on (I'll check separately) or the other issue has already/accidentally been fixed.\n\n### Suggested fix.\n\nGiven that _PyDict_NewKeysForClass returns `keys` at the end anyway, it seems reasonable to just return NULL from the `keys == NULL` branch here:\nhttps://github.com/python/cpython/blob/9f9787d83462f5b44cb59e3c69e7c1799612a33f/Objects/dictobject.c#L7220-L7222\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a0 (heads/main-dirty:639a552, Jul 4 2026, 21:13:52) [Clang 22.1.6 ]\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-153183\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/stestagg","@type":"Person","name":"stestagg"},"datePublished":"2026-07-06T10:13:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/153182/cpython/issues/153182"}
| 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:f5889349-9833-2065-3588-db02e24e6079 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 95AE:32735F:2C7301:3EE3AE:6A50F5B5 |
| html-safe-nonce | 843c199870f86e3bcc3956613747caec1c9bc9bc5db29c78122da3ff1b116bda |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUFFOjMyNzM1RjoyQzczMDE6M0VFM0FFOjZBNTBGNUI1IiwidmlzaXRvcl9pZCI6IjI3MDc1MTU0MzkyMTUzNDMwMjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 69de6521143452350f3f21aa09d98ee12b6bf2e8ca32576137751be3d660edc5 |
| hovercard-subject-tag | issue:4818115602 |
| 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/153182/issue_layout |
| twitter:image | https://opengraph.githubassets.com/42458012ecad534ae23b588fc34605fb74aa507bac5be24a57e6df4a339683f6/python/cpython/issues/153182 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/42458012ecad534ae23b588fc34605fb74aa507bac5be24a57e6df4a339683f6/python/cpython/issues/153182 |
| og:image:alt | Crash report What happened? import _testcapi def make_class(): class C: def f(self): self.x = 1 return C for start in range(100): _testcapi.set_nomemory(start, start + 1) try: x = make_class() exce... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stestagg |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5266e58c17a510c403505cc811606465e90a881d2007ee7df1c4800d5c659838 |
| 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 | 8d836581d020676cc0ef4a83cd2fe17e4af02c3d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width