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 — [3.13]gh-137894: Fix segmentation fault in deeply nested filter() iterator chains by tangyuan0821 · Pull Request #137895 · python/cpython

Conversation

@tangyuan0821

@tangyuan0821 tangyuan0821 commented Aug 18, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Summary

Fixed a critical segmentation fault bug in Python 3.13 where creating deeply nested chains of filter() iterators would crash the interpreter instead of raising a proper Python exception. The fix adds a tp_clear method to the filter object to prevent stack overflow during garbage collection of cyclic references.

Problem

When creating deeply nested filter() iterator chains (e.g., applying filter() thousands of times in a loop), Python 3.13 would crash with a segmentation fault instead of raising a RecursionError. This violates Python's fundamental principle that user code should never be able to crash the interpreter.

Example problematic code:

i = filter(bool, range(1000000))
for _ in range(100000):
    i = filter(bool, i)
print(list(i))  # Segmentation fault in Python 3.13

Expected behavior: Should raise RecursionError or similar Python exception
Actual behavior: Segmentation fault (interpreter crash)

Root Cause

The filter object type in Python/bltinmodule.c was missing a tp_clear method implementation. During garbage collection of deeply nested filter iterator chains, the garbage collector would attempt to deallocate objects through recursive calls to filter_dealloc(), eventually exceeding the C stack limit and causing a segmentation fault.

Solution

Added a tp_clear method to the filter object type:

  1. Implemented filter_clear() function:

    static int
    filter_clear(filterobject *lz)
    {
        Py_CLEAR(lz->it);
        Py_CLEAR(lz->func);
        return 0;
    }
  2. Updated PyFilter_Type definition:

    // Changed from:
    0,                                  /* tp_clear */
    // To:
    (inquiry)filter_clear,              /* tp_clear */

The tp_clear method allows the garbage collector to break reference cycles safely without relying on deep recursive deallocation, preventing stack overflow crashes.

  • Issue: Segmentation fault in Python 3.13 when chaining deep filter() iterators #137894

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
miss-islington and others added 30 commits June 19, 2025 13:04
@miss-islington @rffontenelle
[3.13] Docs: Emphasize parameter name in pkgutil.iter_importers (py…
af48f39
…thonGH-135597) (pythonGH-135711)

Docs: Emphasize parameter name in `pkgutil.iter_importers` (pythonGH-135597)
(cherry picked from commit ff639af)

Co-authored-by: Rafael Fontenelle <[email protected]>
@miss-islington @rffontenelle
[3.13] Docs: Add missing lines between regex and text (pythonGH-134505)…
2537188
… (pythonGH-135719)

Docs: Add missing lines between regex and text (pythonGH-134505)
(cherry picked from commit 7541902)

Co-authored-by: Rafael Fontenelle <[email protected]>
@miss-islington @hrimov
[3.13] pythongh-135273: Unify ZoneInfo.from_file signatures (python…
a8c3af0
…GH-135274) (python#135715)

pythongh-135273: Unify `ZoneInfo.from_file` signatures (pythonGH-135274)

Align `ZoneInfo.from_file` pure-Python signature with Argument Clinic signature.
(cherry picked from commit 7cc8949)

Co-authored-by: Andrii Hrimov <[email protected]>
@miss-islington Yuki Kobayashi
[3.13] Docs: Document PyExceptionClass functions in the C API (pyth…
c6e26ef
…onGH-135697) (pythonGH-135758)

Docs: Document `PyExceptionClass` functions in the C API (pythonGH-135697)

* Docs: Document `PyExceptionClass_Name`

`PyExceptionClass_Name` is an undocumented function in the limited API.

* Document `PyExceptionClass_Check`
(cherry picked from commit 59963e8)

Co-authored-by: Yuki Kobayashi <[email protected]>
@miss-islington @MarcellPerger1
[3.13] pythongh-135756: Fix nonexistent parameter in tkinter docs (py…
4eab9da
…thonGH-135770) (python#135777)

pythongh-135756: Fix nonexistent parameter in tkinter docs (pythonGH-135770)

Remove nonexistent color parameter from tkinter.commondialog.Dialog.show() method documentation.
(cherry picked from commit 4ddf505)

Co-authored-by: Marcell Perger <[email protected]>
@miss-islington @picnixz
[3.13] pythongh-135815: skip netrc security checks if os.getuid i…
6e0ad71
…s missing (pythonGH-135816) (python#135826)

pythongh-135815: skip `netrc` security checks if `os.getuid` is missing (pythonGH-135816)
(cherry picked from commit b57b619)

Co-authored-by: Bénédikt Tran <[email protected]>
@miss-islington @kattni
[3.13] patchcheck: use URL paths to identify upstream remote (pythonG…
b79e64b
…H-135806) (python#135809)

Co-authored-by: Kattni <[email protected]>
@miss-islington @emmatyping
[3.13] pythongh-134986: Catch PermissionError when trying to call per…
3e81d56
…f in tests (pythonGH-134987) (python#135842)

pythongh-134986: Catch PermissionError when trying to call perf in tests (pythonGH-134987)

Using Ubuntu 24.04 on the Windows Subsystem for Linux, perf will raise a
`PermissionError` instead of `FileNotFoundError`. This commit modifies
the tests to catch that.
(cherry picked from commit 6ab842f)

Co-authored-by: Emma Smith <[email protected]>
@ericsnowcurrently
[3.13] pythongh-135698: Fix Cross-interpreter Queue.full() With Negat…
666795a
…ive/Default max_size (pythongh-135778)

We weren't handling non-positive maxsize values (including the default) properly
in Queue.full().  This change fixes that and adjusts an associated assert.

(cherry picked from commit c5ea8e8, AKA pythongh-135724)
@miss-islington @Yzi-Li
[3.13] Docs: Use arguments to replace args in argparse.rst (pyt…
980d6cc
…honGH-135510) (pythonGH-135867)

Docs: Use `arguments` to replace `args` in `argparse.rst` (pythonGH-135510)
(cherry picked from commit caad163)

Co-authored-by: Yongzi Li <[email protected]>
@hugovk @sobolevn
[3.13] Bump mypy to 1.16.1 (pythonGH-135720) (python#135849)
028d56f
Co-authored-by: sobolevn <[email protected]>
@vstinner
[3.13] pythongh-135494: Fix python -m test --pgo -x test_re (python#1…
a27398d
…35713) (python#135881)

pythongh-135494: Fix python -m test --pgo -x test_re (python#135713)

Fix regrtest to support excluding tests from --pgo tests.

(cherry picked from commit 15c6d63)
@miss-islington @picnixz
[3.13] pythongh-135487: fix reprlib.Repr.repr_int when given very l…
86d1821
…arge integers (pythonGH-135506) (python#135886)

pythongh-135487: fix `reprlib.Repr.repr_int` when given very large integers (pythonGH-135506)
(cherry picked from commit e5f03b9)

Co-authored-by: Bénédikt Tran <[email protected]>
@miss-islington @sobolevn @ZeroIntensity
[3.13] pythongh-135878: Fix crash in types.SimpleNamespace.__repr__ (…
469f69d
…pythonGH-135889) (python#135895)

pythongh-135878: Fix crash in `types.SimpleNamespace.__repr__` (pythonGH-135889)
(cherry picked from commit b3ab94a)

Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
@brianschubert
[3.13] pythongh-135855: Raise TypeError When Passing Non-dict Object …
3e43628
…to `_interpreters.set___main___attrs` (pythongh-135903)

(cherry picked from commit 4e6f0d1, AKA pythongh-135856)
@miss-islington @ZeroIntensity
@brianschubert @encukou
[3.13] pythongh-135755: Docs: C API: Document missing `PyFunction_GET…
c344aad
…*` macros (pythonGH-135762) (pythonGH-135917)

(cherry picked from commit ca87a47)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Brian Schubert <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
@sobolevn
[3.13] pythongh-135839: Fix module_traverse and module_clear in `…
28c5adf
…_interpchannelsmodule` (pythonGH-135840) (python#135919)

(cherry picked from commit dd59c78)
@miss-islington @HarryLHW
[3.13] Docs: Add cross-reference for positional_item in the calls…
86c050a
… productionlist (pythonGH-129977) (pythonGH-135941)

Docs: Add cross-reference for `positional_item` in the `calls` productionlist (pythonGH-129977)

Add missing hyperlink for `positional_item`
(cherry picked from commit d215491)

Co-authored-by: HarryLHW <[email protected]>
@colesbury @jtibbertsma
[3.13] pythongh-135871: Fix needless spinning in _PyMutex_LockTimed…
c64df2a
… with zero timeout (pythongh-135872) (pythongh-135947)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <[email protected]>
@miss-islington @reynoldsnlp
[3.13] Docs: Fix indentation in slice class of functions.rst (pyt…
335d7da
…honGH-134393) (pythonGH-135949)

Docs: Fix indentation in `slice` class of `functions.rst` (pythonGH-134393)

Paragraph should not be under `slice.step`. It applies to the whole class.

(cherry picked from commit 6227662)

Co-authored-by: Rob Reynolds <[email protected]>
@miss-islington @vsajip
[3.13] pythongh-91555: Revert disabling of logger while handling log …
c148a68
…record. (pythonGH-135858) (pythonGH-135911)

Co-authored-by: Vinay Sajip <[email protected]>
@miss-islington @terryjreedy
[3.13] pythongh-135956: Remove duplicate word in _pydatetime docstring (
21f9873
pythonGH-135957) (python#135963)

pythongh-135956: Remove duplicate word in _pydatetime docstring (pythonGH-135957)

_pydatetime.isoformat docstring repeats 'giving'.
(cherry picked from commit e3ea6f2)

Co-authored-by: Terry Jan Reedy <[email protected]>
@miss-islington @LamentXU123
[3.13] pythongh-135965: Delete duplicate word in isolating-extensions…
3789d8d
… howto (pythonGH-135964) (python#135978)

pythongh-135965: Delete duplicate word in isolating-extensions howto (pythonGH-135964)

Change use use to use.
(cherry picked from commit ffb2a02)

Co-authored-by: Weilin Du <[email protected]>
@miss-islington @cdenihan
[3.13] pythongh-135110: Fix misleading generator.close() documentat…
a724632
…ion (pythonGH-135152) (pythonGH-135986)

pythongh-135110: Fix misleading `generator.close()` documentation (pythonGH-135152)

The documentation incorrectly stated that generator.close() 'raises' a
GeneratorExit exception. This was misleading because the method doesn't
raise the exception to the caller - it sends the exception internally
to the generator and returns None.
(cherry picked from commit 0d76dcc)

Co-authored-by: Connor Denihan <[email protected]>
@miss-islington
[3.13] pythongh-91555: add warning to docs about possibility of deadl…
3be3c49
…ock/infinite recursion (pythonGH-135954) (pythonGH-135989)

(cherry picked from commit a4625d5)
@miss-islington @StanFromIreland
[3.13] IDLE: Update NEWS2x.txt with 2.7.0 release date (pythonGH-129908…
ac8b868
…) (python#136000)

IDLE: Update NEWS2x.txt with 2.7.0 release date (pythonGH-129908)
(cherry picked from commit 642e5df)

Co-authored-by: Stan Ulbrych <[email protected]>
@miss-islington @nkorth
@blurb-it @brianschubert @terryjreedy
[3.13] pythongh-135995: Fix missing char in palmos encoding (pythonGH…
784c0ed
…-135990) (python#136002)

pythongh-135995: Fix missing char in palmos encoding (pythonGH-135990)

0x8b correctly encodes to ‹, but 0x9b was mistakenly marked as a control character instead of ›.
---------
(cherry picked from commit 58a42de)

Co-authored-by: Nathan Korth <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Brian Schubert <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
@brianschubert
[3.13] Docs: Fix duplicate word typos (pythonGH-135958) (pythonGH-136008
cee66dd
)

(cherry picked from commit 34ce192)
@freakboy3742
[3.13] pythongh-135966: Modify iOS testbed to make app_packages a sit…
07a7636
…e directory (pythonGH-135967) (python#136013)

The iOS testbed now treats the app_packages folder as a site folder. This ensures it is
on the path, but also ensures any .pth files are processed on app startup.
(cherry picked from commit b38810b)
@miss-islington @freakboy3742
[3.13] pythongh-135968: Add iOS binary stubs for strip (pythonGH-135970…
d105eae
…) (python#136015)

Adds iOS binary stubs for invoking `strip`
(cherry picked from commit 0c6c09b)

Co-authored-by: Russell Keith-Magee <[email protected]>
@tangyuan0821
tangyuan0821 requested review from a team, 1st1, Eclips4, Fidget-Spinner, ZeroIntensity, abalkin, ambv, asvetlov, brandtbucher, carljm, cjw296, corona10, diegorusso, ezio-melotti, gpshead, iritkatriel, isidentical, lysnikolaou, markshannon, methane, ncoghlan, pablogsal, pganssle, picnixz, savannahostrowski, serhiy-storchaka, tiran, vsajip and warsaw as code owners August 18, 2025 07:46
@tangyuan0821 tangyuan0821 closed this Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@tomasr8 tomasr8 Awaiting requested review from tomasr8 tomasr8 is a code owner
@ammaraskar ammaraskar Awaiting requested review from ammaraskar
@hugovk hugovk Awaiting requested review from hugovk hugovk is a code owner
@kumaraditya303 kumaraditya303 Awaiting requested review from kumaraditya303 kumaraditya303 is a code owner
@willingc willingc Awaiting requested review from willingc willingc is a code owner
@jaraco jaraco Awaiting requested review from jaraco jaraco is a code owner
@sethmlarson sethmlarson Awaiting requested review from sethmlarson sethmlarson is a code owner
@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@freakboy3742 freakboy3742 Awaiting requested review from freakboy3742 freakboy3742 is a code owner
@mhsmith mhsmith Awaiting requested review from mhsmith mhsmith is a code owner
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@barneygale barneygale Awaiting requested review from barneygale barneygale is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner
@encukou encukou Awaiting requested review from encukou encukou is a code owner
@hauntsaninja hauntsaninja Awaiting requested review from hauntsaninja hauntsaninja is a code owner
@ethanfurman ethanfurman Awaiting requested review from ethanfurman ethanfurman is a code owner
@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra JelleZijlstra is a code owner
@AlexWaygood AlexWaygood Awaiting requested review from AlexWaygood AlexWaygood is a code owner
@terryjreedy terryjreedy Awaiting requested review from terryjreedy terryjreedy is a code owner
@pfmoore pfmoore Awaiting requested review from pfmoore pfmoore is a code owner
@pradyunsg pradyunsg Awaiting requested review from pradyunsg pradyunsg is a code owner
@ericvsmith ericvsmith Awaiting requested review from ericvsmith ericvsmith is a code owner
@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger is a code owner
@berkerpeksag berkerpeksag Awaiting requested review from berkerpeksag
@gaogaotiantian gaogaotiantian Awaiting requested review from gaogaotiantian gaogaotiantian is a code owner
@gpshead gpshead Awaiting requested review from gpshead gpshead is a code owner
@cjw296 cjw296 Awaiting requested review from cjw296 cjw296 is a code owner
@Eclips4 Eclips4 Awaiting requested review from Eclips4 Eclips4 is a code owner
@isidentical isidentical Awaiting requested review from isidentical isidentical is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@pganssle pganssle Awaiting requested review from pganssle pganssle is a code owner
@abalkin abalkin Awaiting requested review from abalkin
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@ezio-melotti ezio-melotti Awaiting requested review from ezio-melotti ezio-melotti is a code owner
@vsajip vsajip Awaiting requested review from vsajip vsajip is a code owner
@picnixz picnixz Awaiting requested review from picnixz picnixz is a code owner
@tiran tiran Awaiting requested review from tiran
@iritkatriel iritkatriel Awaiting requested review from iritkatriel iritkatriel is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner
@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka serhiy-storchaka is a code owner
@ambv ambv Awaiting requested review from ambv ambv is a code owner
@brandtbucher brandtbucher Awaiting requested review from brandtbucher brandtbucher is a code owner
@carljm carljm Awaiting requested review from carljm carljm is a code owner
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner Fidget-Spinner is a code owner
@methane methane Awaiting requested review from methane methane is a code owner
@savannahostrowski savannahostrowski Awaiting requested review from savannahostrowski savannahostrowski is a code owner
@diegorusso diegorusso Awaiting requested review from diegorusso diegorusso is a code owner
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

20 participants

@tangyuan0821 @StanFromIreland @sergey-miryanov @miss-islington @ericsnowcurrently @hugovk @vstinner @brianschubert @sobolevn @colesbury @freakboy3742 @serhiy-storchaka @picnixz @skirpichev @rffontenelle @pablogsal @kumaraditya303 @duaneg @zware @facundobatista
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — 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.