Title: Segfault when cleaning up after a MemoryError with deeply nested objects · Issue #149146 · python/cpython · GitHub
Open Graph Title: Segfault when cleaning up after a MemoryError with deeply nested objects · Issue #149146 · python/cpython
X Title: Segfault when cleaning up after a MemoryError with deeply nested objects · Issue #149146 · python/cpython
Description: Crash report What happened? When in memory-constrained situations, the following code generates a segfault on main: b = 9 while True: b = (b, None) Clearly, b grows to be an unbounded nested tuple, which eventually exhausts the memory. A...
Open Graph Description: Crash report What happened? When in memory-constrained situations, the following code generates a segfault on main: b = 9 while True: b = (b, None) Clearly, b grows to be an unbounded nested tuple,...
X Description: Crash report What happened? When in memory-constrained situations, the following code generates a segfault on main: b = 9 while True: b = (b, None) Clearly, b grows to be an unbounded nested tuple,...
Opengraph URL: https://github.com/python/cpython/issues/149146
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault when cleaning up after a MemoryError with deeply nested objects","articleBody":"# Crash report\n\n### What happened?\n\nWhen in memory-constrained situations, the following code generates a segfault on main:\n\n```python\nb = 9\nwhile True:\n b = (b, None)\n```\n\nClearly, b grows to be an unbounded nested tuple, which eventually exhausts the memory.\nA MemoryError is correctly thrown, but during unwind, python tries to clean up the object, and this triggers a stack overflow.\n\n\u003cdetails\u003e\n\u003csummary\u003eExample stack\u003c/summary\u003e\n\n```\nProcess 369 stopped\n\nThread 1 \"fuzz_python\" stop reason = signal SIGSEGV\n frame #0: frame #0: 0x0000aaaaaea31648 fuzz_python`tuple_dealloc(self=0x0000ffff9fa045e0) at tupleobject.c:256\n frame #1: frame #1: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffff9fa045e0) at object.c:3303:5\n frame #2: frame #2: 0x0000aaaaaea3182c fuzz_python`Py_DECREF(op=\u003cunavailable\u003e) at refcount.h:427:9 [inlined]\n frame #3: frame #3: 0x0000aaaaaea3180c fuzz_python`Py_XDECREF(op=\u003cunavailable\u003e) at refcount.h:520:9 [inlined]\n frame #4: frame #4: 0x0000aaaaaea31804 fuzz_python`tuple_dealloc(self=0x0000ffff9fa04630) at tupleobject.c:277:9\n frame #5: frame #5: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffff9fa04630) at object.c:3303:5\n frame #6: frame #6: 0x0000aaaaaea3182c fuzz_python`Py_DECREF(op=\u003cunavailable\u003e) at refcount.h:427:9 [inlined]\n frame #7: frame #7: 0x0000aaaaaea3180c fuzz_python`Py_XDECREF(op=\u003cunavailable\u003e) at refcount.h:520:9 [inlined]\n...\n frame #2709: frame #2709: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffff9fa11cd0) at object.c:3303:5\n frame #2710: frame #2710: 0x0000aaaaaea3182c fuzz_python`Py_DECREF(op=\u003cunavailable\u003e) at refcount.h:427:9 [inlined]\n frame #2711: frame #2711: 0x0000aaaaaea3180c fuzz_python`Py_XDECREF(op=\u003cunavailable\u003e) at refcount.h:520:9 [inlined]\n frame #2712: frame #2712: 0x0000aaaaaea31804 fuzz_python`tuple_dealloc(self=0x0000ffff9fa11d20) at tupleobject.c:277:9\n frame #2713: frame #2713: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffff9fa11d20) at object.c:3303:5\n frame #2714: frame #2714: 0x0000aaaaaea3182c fuzz_python`Py_DECREF(op=\u003cunavailable\u003e) at refcount.h:427:9 [inlined]\n frame #2715: frame #2715: 0x0000aaaaaea3180c fuzz_python`Py_XDECREF(op=\u003cunavailable\u003e) at refcount.h:520:9 [inlined]\n frame #2716: frame #2716: 0x0000aaaaaea31804 fuzz_python`tuple_dealloc(self=0x0000ffff9fa11d70) at tupleobject.c:277:9\n frame #2717: frame #2717: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffff9fa11d70) at object.c:3303:5\n frame #2718: frame #2718: 0x0000aaaaaea3182c fuzz_python`Py_DECREF(op=\u003cunavailable\u003e) at refcount.h:427:9 [inlined]\n frame #2719: frame #2719: 0x0000aaaaaea3180c fuzz_python`Py_XDECREF(op=\u003cunavailable\u003e) at refcount.h:520:9 [inlined]\n frame #2720: frame #2720: 0x0000aaaaaea31804 fuzz_python`tuple_dealloc(self=0x0000ffff9fa11dc0) at tupleobject.c:277:9\n frame #2721: frame #2721: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffff9fa11dc0) at object.c:3303:5\n frame #2722: frame #2722: 0x0000aaaaae98f290 fuzz_python`Py_DECREF(op=\u003cunavailable\u003e) at refcount.h:427:9 [inlined]\n frame #2723: frame #2723: 0x0000aaaaae98f26c fuzz_python`Py_XDECREF(op=\u003cunavailable\u003e) at refcount.h:520:9 [inlined]\n frame #2724: frame #2724: 0x0000aaaaae98f26c fuzz_python`dictkeys_decref(dk=0x0000ffffbe045210, use_qsbr=false) at dictobject.c:495:17\n frame #2725: frame #2725: 0x0000aaaaae98630c fuzz_python`dict_dealloc(self=0x0000ffffbe055f40) at dictobject.c:0\n frame #2726: frame #2726: 0x0000aaaaae9ccd54 fuzz_python`_Py_Dealloc(op=0x0000ffffbe055f40) at object.c:3303:5\n frame #2727: frame #2727: 0x0000aaaaae7ecb8c fuzz_python`Py_DECREF(op=0x0000ffffbe055f40) at refcount.h:427:9 [inlined]\n frame #2728: frame #2728: 0x0000aaaaae7ecb58 fuzz_python`main(argc=1, argv=0x0000fffff67f4238) at fuzz_python.c:226:17\n frame #2729: frame #2729: 0x0000ffffbe1e1f9c ld-musl-aarch64.so.1`libc_start_main_stage2(main=(fuzz_python`main at fuzz_python.c:71), argc=1, argv=0x0000fffff67f4238) at __libc_start_main.c:95:2\n```\n\n\u003c/details\u003e\n\nThe following dockerfile reliably reproduces it for me:\n\n\u003cdetails\u003e\n\u003csummary\u003eDockerfile\u003c/summary\u003e\n\n```\nFROM alpine:latest\n\nRUN apk add --no-cache \\\n git build-base linux-headers openssl-dev zlib-dev bzip2-dev \\\n xz-dev sqlite-dev readline-dev libffi-dev ncurses-dev gdbm-dev \\\n tcl-dev tk-dev\n\nWORKDIR /src\nRUN git clone --depth=1 https://github.com/python/cpython.git\nWORKDIR /src/cpython\nRUN ./configure \u0026\u0026 make -j2\n\nRUN cat \u003e /tc.py \u003c\u003c'PY'\nb = 9\nwhile True:\n b = (b, None)\nPY\n\nRUN cat \u003e /entrypoint.sh \u003c\u003c'SH'\n#!/bin/sh\n\necho \"Running test\"\n# 512 MiB virtual memory limit\nulimit -v 524288\n\n/src/cpython/python /tc.py\necho \"Python process exited with code $?\"\nSH\n\nRUN chmod +x /entrypoint.sh\nENTRYPOINT [\"/entrypoint.sh\"]\n```\n\n\u003c/details\u003e\n\nGives me:\n```\n\u003edocker run --rm -it 29apr\nRunning test\nTraceback (most recent call last):\n File \"/tc.py\", line 3, in \u003cmodule\u003e\nobject address : 0xffffae809fc0\nobject refcount : 3\nobject type : 0xaaaad32175a8\nobject type name: MemoryError\nobject repr : MemoryError()\nlost sys.stderr\nSegmentation fault\nPython process exited with code 139\n```\n\nISTR that stack overflows from recursive objects were at least considered low priority, if not 'not worth fixing' before, but I can't recall where I read that, it was a while ago, so apologies if this isn't a useful report!\n\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.15.0a8+ (heads/main:d4eee16, Apr 29 2026, 11:03:26) [GCC 15.2.0]\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-151595\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/stestagg","@type":"Person","name":"stestagg"},"datePublished":"2026-04-29T11:14:31.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/149146/cpython/issues/149146"}
| 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:d9ee174b-8874-ab2f-5d6b-ef82711f1e88 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2AA:18AAA8:1D404EB:281734D:6A540BE8 |
| html-safe-nonce | d30260960749ddbdb48f5052c3c24ad8894c9eed851567a08a60fea8a65756ca |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkFBOjE4QUFBODoxRDQwNEVCOjI4MTczNEQ6NkE1NDBCRTgiLCJ2aXNpdG9yX2lkIjoiMzU5Mjk1NzkyMTIwNDYzNjY0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 41f8e62dcd6c60900ce163cc569abae278b4f6557ff1dbfa0dad7a4483b957ee |
| hovercard-subject-tag | issue:4350054959 |
| 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/149146/issue_layout |
| twitter:image | https://opengraph.githubassets.com/998b6997ca41d4eb8b46966c2c8c79262bcc6d0163a3c79f229328d63fa5de14/python/cpython/issues/149146 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/998b6997ca41d4eb8b46966c2c8c79262bcc6d0163a3c79f229328d63fa5de14/python/cpython/issues/149146 |
| og:image:alt | Crash report What happened? When in memory-constrained situations, the following code generates a segfault on main: b = 9 while True: b = (b, None) Clearly, b grows to be an unbounded nested tuple,... |
| 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 | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width