- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 33.3k
 
gh-137894: Fix segmentation fault in deeply nested filter() iterator chains #137896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      0919dec
              
                Fix segmentation fault in deeply nested filter() iterator chains
              
              
                tangyuan0821 21a3924
              
                📜🤖 Added by blurb_it.
              
              
                blurb-it[bot] 61a0506
              
                Add tests for filter object recursion and garbage collection handling
              
              
                tangyuan0821 1e63409
              
                Removed trailing whitespace from test_builtin.py
              
              
                tangyuan0821 413beaa
              
                Fix filter_clear signature and address review feedback
              
              
                tangyuan0821 0256904
              
                Fix the signature of filter_clear to solve the problem in the Recursi…
              
              
                tangyuan0821 757bd88
              
                Fix the blank line issue in test_builtin.py
              
              
                tangyuan0821 95def8e
              
                Add skip conditions for test_filter_deep_nesting_recursion_error and …
              
              
                tangyuan0821 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -1132,6 +1132,22 @@ def test_filter_dealloc(self): | |
| del i | ||
| gc.collect() | ||
| 
     | 
||
| @support.skip_wasi_stack_overflow() | ||
| @support.skip_emscripten_stack_overflow() | ||
| @support.requires_resource('cpu') | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it really need a CPU resource? (or maybe it's because of   | 
||
| def test_filter_deep_nesting_recursion_error(self): | ||
| # gh-137894: Test that deeply nested filter() iterator chains | ||
| # raise RecursionError instead of causing segmentation fault. | ||
| # This verifies that the tp_clear method prevents stack overflow | ||
| # during garbage collection of cyclic references. | ||
| i = filter(bool, range(1000000)) | ||
| for _ in range(100000): | ||
| i = filter(bool, i) | ||
| 
     | 
||
| # Should raise RecursionError, not segmentation fault | ||
| with self.assertRaises(RecursionError): | ||
| list(i) | ||
                
      
                  tangyuan0821 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| def test_getattr(self): | ||
| self.assertTrue(getattr(sys, 'stdout') is sys.stdout) | ||
| self.assertRaises(TypeError, getattr) | ||
| 
          
            
          
           | 
    ||
        
          
          
            6 changes: 6 additions & 0 deletions
          
          6 
        
  Misc/NEWS.d/next/Core_and_Builtins/2025-08-18-08-14-52.gh-issue-137894.SrkIA_.rst
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Fix segmentation fault in deeply nested :func:`filter` iterator chains by | ||
| adding a ``tp_clear`` method to the filter object. Previously, creating | ||
| thousands of nested filter iterators and then consuming them would cause | ||
| a stack overflow during garbage collection, leading to interpreter crashes. | ||
| Now such cases properly raise :exc:`RecursionError` instead of crashing | ||
| the interpreter. | ||
                
      
                  ZeroIntensity marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.