Title: PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets · Issue #156204 · python/cpython · GitHub
Open Graph Title: PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets · Issue #156204 · python/cpython
X Title: PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets · Issue #156204 · python/cpython
Description: Crash report What happened? Problem Description PyErr_GivenExceptionMatches(err, exc) in Python/errors.c recursively iterates through nested tuple exception targets without guarding recursion depth via Py_EnterRecursiveCall() / Py_LeaveR...
Open Graph Description: Crash report What happened? Problem Description PyErr_GivenExceptionMatches(err, exc) in Python/errors.c recursively iterates through nested tuple exception targets without guarding recursion depth...
X Description: Crash report What happened? Problem Description PyErr_GivenExceptionMatches(err, exc) in Python/errors.c recursively iterates through nested tuple exception targets without guarding recursion depth...
Opengraph URL: https://github.com/python/cpython/issues/156204
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets","articleBody":"# Crash report\n\n### What happened?\n\n### Problem Description\n\n`PyErr_GivenExceptionMatches(err, exc)` in `Python/errors.c` recursively iterates through nested tuple exception targets without guarding recursion depth via `Py_EnterRecursiveCall()` / `Py_LeaveRecursiveCall()`.\n\nWhen evaluating deeply nested tuple structures passed through C API extensions or dynamic tuple composition, unbounded native C call stack growth leads to stack exhaustion and an immediate `SIGSEGV` crash.\n\n### Steps to Reproduce\n\n```python\nimport ctypes\n\nlib = ctypes.pythonapi\nlib.PyErr_GivenExceptionMatches.argtypes = [ctypes.py_object, ctypes.py_object]\nlib.PyErr_GivenExceptionMatches.restype = ctypes.c_int\n\n# Construct deeply nested tuple target\ntup = (1, ValueError)\nfor _ in range(1_000_000):\n tup = (1, tup)\n\n# Trigger exception matching traversal\nres = lib.PyErr_GivenExceptionMatches(TypeError(), tup)\n```\n\n### Actual Result\n\n`Segmentation fault (core dumped)`\n\n```text\nProgram received signal SIGSEGV, Segmentation fault.\nPyTuple_Size (op=op@entry=0x7ffff2beda30) at Objects/tupleobject.c:100\n100 return Py_SIZE(op);\n#0 PyTuple_Size (op=op@entry=0x7ffff2beda30) at Objects/tupleobject.c:100\n#1 0x0000555555824f63 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2beda30) at Python/errors.c:339\n#2 0x0000555555824fb0 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2beda90) at Python/errors.c:342\n#3 0x0000555555824fb0 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2bedaf0) at Python/errors.c:342\n#4 0x0000555555824fb0 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2bedb50) at Python/errors.c:342\n...\nrsp 0x7fffff7ff000 0x7fffff7ff000\n```\n\n### Expected Result\n\n`PyErr_GivenExceptionMatches` should guard recursion with `Py_EnterRecursiveCall()` / `Py_LeaveRecursiveCall()`, setting a `RecursionError` and returning `0` safely when recursion depth is exceeded instead of crashing the interpreter.\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/fix-err-given-exception-matches-recursion:999a046b24c, Aug 21 2026, 18:34:) [GCC 13.3.0]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156205\n* gh-158622\n* gh-158623\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/BHUVANSH855","@type":"Person","name":"BHUVANSH855"},"datePublished":"2026-08-21T18:42:58Z","url":"https://github.com/python/cpython/issues/156204"}
| 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:2df40165-5066-a7f2-0998-43ed9fc2ffe5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB08:14EB6E:1906266:20E01BB:6B020B51 |
| html-safe-nonce | b48b68308feecc8d724dbde8634f35d23e4baa9fdd7fe2a3d8842e6c43442fb7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjA4OjE0RUI2RToxOTA2MjY2OjIwRTAxQkI6NkIwMjBCNTEiLCJ2aXNpdG9yX2lkIjoiNzYzNjUzNjgzNDMwNjIxNDczNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f87631ce8084a9165160f9e91ec7a6a94e3e9badb4ceaa66b2ca7db8d5996d3e |
| hovercard-subject-tag | issue:5217146819 |
| 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/156204/issue_layout |
| twitter:image | https://opengraph.githubassets.com/368f4124f207a028b854e88d289bab6219e999149188fc58b50b8988e895a49a/python/cpython/issues/156204 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/368f4124f207a028b854e88d289bab6219e999149188fc58b50b8988e895a49a/python/cpython/issues/156204 |
| og:image:alt | Crash report What happened? Problem Description PyErr_GivenExceptionMatches(err, exc) in Python/errors.c recursively iterates through nested tuple exception targets without guarding recursion depth... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | BHUVANSH855 |
| 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