Title: Compiler crash: inlined comprehensions overflow `_PyCfgExceptStack`, bypassing the `u_nfblocks` block-nesting limit · Issue #156091 · python/cpython · GitHub
Open Graph Title: Compiler crash: inlined comprehensions overflow `_PyCfgExceptStack`, bypassing the `u_nfblocks` block-nesting limit · Issue #156091 · python/cpython
X Title: Compiler crash: inlined comprehensions overflow `_PyCfgExceptStack`, bypassing the `u_nfblocks` block-nesting limit · Issue #156091 · python/cpython
Description: Crash report What happened? Compiling source with 23 or more nested list/set/dict comprehensions crashes the compiler: an assertion failure in debug builds, a segfault in release (NDEBUG) builds. Nothing is executed — compile() alone is ...
Open Graph Description: Crash report What happened? Compiling source with 23 or more nested list/set/dict comprehensions crashes the compiler: an assertion failure in debug builds, a segfault in release (NDEBUG) builds. N...
X Description: Crash report What happened? Compiling source with 23 or more nested list/set/dict comprehensions crashes the compiler: an assertion failure in debug builds, a segfault in release (NDEBUG) builds. N...
Opengraph URL: https://github.com/python/cpython/issues/156091
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Compiler crash: inlined comprehensions overflow `_PyCfgExceptStack`, bypassing the `u_nfblocks` block-nesting limit","articleBody":"# Crash report\n\n### What happened?\n\nCompiling source with 23 or more nested list/set/dict comprehensions crashes\nthe compiler: an assertion failure in debug builds, a segfault in release\n(NDEBUG) builds. Nothing is executed — `compile()` alone is enough. The input\nis ~300 bytes.\n\n## Reproduction\n\n```python\ndef src(d):\n e = \"i for i in r\"\n for _ in range(d - 1):\n e = \"[\" + e + \"] for i in r\"\n return \"x = [\" + e + \"]\"\n\ncompile(src(23), \"\u003cs>\", \"exec\") # crashes; depth 22 compiles fine\n```\n\n## Observed behavior\n\nCliff is exactly `CO_MAXBLOCKS + 2` nested comprehensions:\n\n| Build | Crash at | Behavior |\n|---|---|---|\n| main (`20e6c2fc`), assertions on | depth 23 | `python: Python/flowgraph.c: push_except_block: Assertion 'stack->depth \u003c= CO_MAXBLOCKS' failed` |\n| main (`20e6c2fc`), `-DNDEBUG` (-O0 and -O2) | depth 23 | SIGSEGV |\n| 3.12.3 (`CO_MAXBLOCKS` = 20), distro release build | depth 22 | SIGSEGV |\n| any build | depth ≤ cliff−1 | compiles fine |\n\nRelease-build backtrace (NDEBUG, `-O0 -g`, gdb):\n\n```\nThread 1 \"fuzz_pycompile_\" received signal SIGSEGV, Segmentation fault.\n0x00005b2b9480d547 in except_stack_top (stack=0x7ba300866070) at Python/flowgraph.c:724\nwarning: Source file is more recent than executable.\n724\t return stack->handlers[stack->depth];\n\n=== full backtrace (NDEBUG, -O0 -g, HEAD 20e6c2fc) ===\n#0 0x00005b2b9480d547 in except_stack_top (stack=0x7ba300866070) at Python/flowgraph.c:724\n#1 0x00005b2b94809f6d in label_exception_targets (entryblock=0x7ba300529d40) at Python/flowgraph.c:921\n#2 0x00005b2b94809a4d in _PyCfg_OptimizeCodeUnit (g=0x7ba300589350, consts=0x7ba3005a6a80, const_cache=0x7ba30053b140, nlocals=1, nparams=0, firstlineno=1) at Python/flowgraph.c:3798\n#3 0x00005b2b947ff534 in optimize_and_assemble_code_unit (u=0x5b2ba5b4cd30, const_cache=0x7ba30053b140, code_flags=0, filename=0x7ba30053b0b0) at Python/compile.c:1481\n#4 0x00005b2b947ff29b in _PyCompile_OptimizeAndAssemble (c=0x7ba300542f10, addNone=1) at Python/compile.c:1523\n#5 0x00005b2b947ff771 in compiler_mod (c=0x7ba300542f10, mod=0x5b2ba5b6a600) at Python/compile.c:901\n#6 0x00005b2b947ff644 in _PyAST_Compile (mod=0x5b2ba5b6a600, filename=0x7ba30053b0b0, pflags=0x7ffeec136588, optimize=0, arena=0x7ba300831b90, module=0x0) at Python/compile.c:1536\n#7 0x00005b2b9487244b in _Py_CompileString (str=0x7ffeec132580 \"x = \", '[' \u003crepeats 23 times>, \"i for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for\"..., filename=0x7ba30053b0b0, start=257, flags=0x7ffeec136588, optimize=0, module=0x0) at Python/pythonrun.c:1595\n#8 0x00005b2b948722e8 in Py_CompileStringObject (str=0x7ffeec132580 \"x = \", '[' \u003crepeats 23 times>, \"i for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for\"..., filename=0x7ba30053b0b0, start=257, flags=0x7ffeec136588, optimize=0) at Python/pythonrun.c:1561\n#9 0x00005b2b94872548 in Py_CompileStringExFlags (str=0x7ffeec132580 \"x = \", '[' \u003crepeats 23 times>, \"i for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for i in r] for\"..., filename_str=0x5b2b94b12d58 \"\u003cfuzz input>\", start=257, flags=0x7ffeec136588, optimize=0) at Python/pythonrun.c:1608\n#10 0x00005b2b9467b515 in fuzz_pycompile (data=0x5b2ba5ac08f0 \"\\002\\001\", size=307) at Modules/_xxtestfuzz/fuzzer.c:564\n#11 0x00005b2b9467b342 in _run_fuzz (data=0x5b2ba5ac08f0 \"\\002\\001\", size=307, fuzzer=0x5b2b9467b370 \u003cfuzz_pycompile>) at Modules/_xxtestfuzz/fuzzer.c:583\n#12 0x00005b2b9467b30b in LLVMFuzzerTestOneInput (data=0x5b2ba5ac08f0 \"\\002\\001\", size=307) at Modules/_xxtestfuzz/fuzzer.c:724\n#13 0x00005b2b94638813 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) ()\n#14 0x00005b2b94622691 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) ()\n#15 0x00005b2b94628358 in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) ()\n#16 0x00005b2b94651973 in main ()\n#17 0x00007ba30062a1ca in __libc_start_call_main (main=main@entry=0x5b2b94651950 \u003cmain>, argc=argc@entry=2, argv=argv@entry=0x7ffeec136df8) at ../sysdeps/nptl/libc_start_call_main.h:58\n#18 0x00007ba30062a28b in __libc_start_main_impl (main=0x5b2b94651950 \u003cmain>, argc=2, argv=0x7ffeec136df8, init=\u003coptimized out>, fini=\u003coptimized out>, rtld_fini=\u003coptimized out>, stack_end=0x7ffeec136de8) at ../csu/libc-start.c:360\n#19 0x00005b2b9461d035 in _start ()\n```\n\nThe faulting address equals `&handlers[0] + depth * 8`, where `depth` is a\ncorrupted value (see below), ~1 GB away from the struct.\n\n## Root cause\n\nSince PEP 709 (3.12), non-generator comprehensions are inlined into the\nenclosing scope. Each inlined comprehension emits one \"virtual try/finally\"\n(`SETUP_FINALLY` cleanup to restore shadowed outer locals) — but it is\ndeliberately not counted as an fblock, in `codegen_push_inlined_comprehension_locals`\n(Python/codegen.c):\n\n```c\n// no need to push an fblock for this \"virtual\" try/finally; there can't\n// be return/continue/break inside a comprehension\n```\n\nSo the only static nesting guard, `u_nfblocks >= CO_MAXBLOCKS → \"too many\nstatically nested blocks\"` (Python/compile.c), never sees these blocks.\n\nThe backend then trusts the frontend limit and uses a fixed-size stack in\nPython/flowgraph.c:\n\n```c\nstruct _PyCfgExceptStack {\n basicblock *handlers[CO_MAXBLOCKS+2];\n int depth;\n};\n```\n\nThe N-th inlined comprehension pushes the N-th block; at nesting depth\n`CO_MAXBLOCKS + 2` the push writes one slot past the array.\n\n- **Assertions on:** `assert(stack->depth \u003c= CO_MAXBLOCKS)` in\n `push_except_block` fires → abort.\n- **NDEBUG:** the out-of-bounds store writes `handlers[CO_MAXBLOCKS+2]`,\n which by struct layout is exactly the `depth` field\n (`&handlers[CO_MAXBLOCKS+2] == &depth`, confirmed with gdb). `depth` is\n clobbered with the low 32 bits of a `basicblock *`; the next\n `except_stack_top` uses it as an index into `handlers[]` → wild read →\n deterministic SIGSEGV.\n\n## Suggested fix\n\nCount the inlined-comprehension cleanup blocks toward `u_nfblocks` so the\nexisting `\"too many statically nested blocks\"` SyntaxError fires (consistent\nwith gh-118348), or make `_PyCfgExceptStack` dynamically sized / bounds-checked.\n\n## Environment\n\n- CPython main `20e6c2fc7c174342214d561845419c4030f8638f` (2026-08-19)\n- Ubuntu 24.04 \n\n\n### CPython versions tested on:\n\n3.15, CPython main branch, 3.16, 3.14, 3.13, 3.12\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156957\n* gh-156992\n* gh-156993\n* gh-157050\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kid-lxy","@type":"Person","name":"kid-lxy"},"datePublished":"2026-08-20T09:43:01Z","url":"https://github.com/python/cpython/issues/156091"}
| 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:a2d4c9a7-0049-592c-573e-fbf806e3ffb3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE32:25F5AB:3B164:49AF6:6B01FA3C |
| html-safe-nonce | 2bd1d41487c5d3a298c45036ad182e600f6d4cedae82dd3a3386d674d0a382e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTMyOjI1RjVBQjozQjE2NDo0OUFGNjo2QjAxRkEzQyIsInZpc2l0b3JfaWQiOiI0NTY3ODg2MTY3MDc2MzcxMDA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7a94b9d9262e1669ff09647eb99ec23b9ce9ff39d5520735c4c3d0346250ec9a |
| hovercard-subject-tag | issue:5202016421 |
| 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/156091/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d21d85a85628fd11a655924e809dd425eac35cd3402c9ac58b6ba6b26fd2927f/python/cpython/issues/156091 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d21d85a85628fd11a655924e809dd425eac35cd3402c9ac58b6ba6b26fd2927f/python/cpython/issues/156091 |
| og:image:alt | Crash report What happened? Compiling source with 23 or more nested list/set/dict comprehensions crashes the compiler: an assertion failure in debug builds, a segfault in release (NDEBUG) builds. N... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kid-lxy |
| 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