GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets · Issue #156204 · python/cpython

PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets #156204

New issue
Copy link
New issue
Copy link
Closed
Closed
PyErr_GivenExceptionMatches crashes with SIGSEGV on deeply nested tuple targets#156204
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

Description

@BHUVANSH855
BHUVANSH855
opened on Aug 21, 2026
Issue body actions

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_LeaveRecursiveCall().

When 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.

Steps to Reproduce

import ctypes

lib = ctypes.pythonapi
lib.PyErr_GivenExceptionMatches.argtypes = [ctypes.py_object, ctypes.py_object]
lib.PyErr_GivenExceptionMatches.restype = ctypes.c_int

# Construct deeply nested tuple target
tup = (1, ValueError)
for _ in range(1_000_000):
    tup = (1, tup)

# Trigger exception matching traversal
res = lib.PyErr_GivenExceptionMatches(TypeError(), tup)

Actual Result

Segmentation fault (core dumped)

Program received signal SIGSEGV, Segmentation fault.
PyTuple_Size (op=op@entry=0x7ffff2beda30) at Objects/tupleobject.c:100
100             return Py_SIZE(op);
#0  PyTuple_Size (op=op@entry=0x7ffff2beda30) at Objects/tupleobject.c:100
#1  0x0000555555824f63 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2beda30) at Python/errors.c:339
#2  0x0000555555824fb0 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2beda90) at Python/errors.c:342
#3  0x0000555555824fb0 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2bedaf0) at Python/errors.c:342
#4  0x0000555555824fb0 in PyErr_GivenExceptionMatches (err=err@entry=0x7ffff78ea900, exc=0x7ffff2bedb50) at Python/errors.c:342
...
rsp             0x7fffff7ff000      0x7fffff7ff000

Expected Result

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.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.16.0a0 (heads/fix-err-given-exception-matches-recursion:999a046b24c, Aug 21 2026, 18:34:) [GCC 13.3.0]

Linked PRs

  • gh-156204: Guard recursion in PyErr_GivenExceptionMatches #156205
  • [3.15] gh-156204: Guard recursion in PyErr_GivenExceptionMatches (GH-156205) #158622
  • [3.14] gh-156204: Guard recursion in PyErr_GivenExceptionMatches (GH-156205) #158623

Activity

  1. BHUVANSH855
    added
    type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
    on Aug 21, 2026
  2. BHUVANSH855
    added a commit that references this issue on Aug 21, 2026

    pythongh-156204: Safely skip ctypes test on mobile/wasm and reduce tu…

    d0d8595
  3. StanFromIreland
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Aug 21, 2026
  4. BHUVANSH855
    added 2 commits that reference this issue on Aug 21, 2026

    pythongh-156204: address review feedback

    b6be6c5

    pythongh-156204: drop docs change

    170d3c9
  5. picnixz commented on Aug 22, 2026

    @picnixz
    picnixz
    on Aug 22, 2026
    Member
    More actions

    I think it's not a real use-case here but since we document that recursion is supported, let's fix this. Is there a noticable effect on performances for regular code or not at all? Because PyErr_GivenExceptionMatches is AFAIR used in hot paths (loops, iterators, coroutines, etc).

  6. BHUVANSH855
    added 3 commits that reference this issue on Sep 24, 2026

    pythongh-156204: convert test helper to Argument Clinic

    12ce58f

    pythongh-156204: remove comment

    e7c80b9

    pythongh-156204: report recursion overflow as unraisable

    141b181
  7. ZeroIntensity
    added 2 commits that reference this issue on Oct 2, 2026

    gh-156204: Guard recursion in `PyErr_GivenExceptionMatches` (GH-156205)

    20d5e67

    [3.14] gh-156204: Guard recursion in `PyErr_GivenExceptionMatches` (G…

    a9eeb9f
  8. ZeroIntensity commented on Oct 2, 2026

    @ZeroIntensity
    ZeroIntensity
    on Oct 2, 2026
    Member
    More actions

    The 3.15 backport will be merged later. Thanks @BHUVANSH855 for the report and the fix.

    Reacted by Bhuvansh
  9. ZeroIntensity
    closed this as completedon Oct 2, 2026
  10. ZeroIntensity
    added a commit that references this issue on Oct 11, 2026

    [3.15] gh-156204: Guard recursion in `PyErr_GivenExceptionMatches` (G…

    ae21cb7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.