Title: gh-149146: Add dealloc-depth counter fallback to trashcan trigger by bhuvi27 · Pull Request #151595 · python/cpython · GitHub
Open Graph Title: gh-149146: Add dealloc-depth counter fallback to trashcan trigger by bhuvi27 · Pull Request #151595 · python/cpython
X Title: gh-149146: Add dealloc-depth counter fallback to trashcan trigger by bhuvi27 · Pull Request #151595 · python/cpython
Description: Fixes #149146. while True: b = (b, None) under RLIMIT_AS segfaults during cleanup: b = 9 while True: b = (b, None) Docker with ulimit -v 524288: MemoryError → lost sys.stderr → segfault, exit 139. The trashcan deposit in _Py_Dealloc only fires when _Py_RecursionLimit_GetMargin(tstate) < 2, which compares the stack pointer against c_stack_soft_limit. Under RLIMIT_AS the kernel SIGSEGVs trying to grow the stack while SP is still nowhere near the soft limit — issue #150722 has an LLDB trace from the same reporter showing SP ~7 MB above c_stack_hard_limit at the fault. So the trashcan never deposits and the recursive tuple_dealloc → Py_XDECREF → _Py_Dealloc → tuple_dealloc chain just runs into the wall. This adds a per-thread c_dealloc_depth counter and uses it as a secondary trigger inside _Py_Dealloc, depositing at depth 50 (matches the old _PyTrash_UNWIND_LEVEL). The stack-pointer margin stays the primary signal; the counter only kicks in when SP can't. One thing I missed on the first pass: the drain side has the same problem. margin >= 4 is also true in this scenario, so every _Py_Dealloc exit would re-enter _PyTrash_thread_destroy_chain from inside the deallocator it just called. Fix: destroy_chain bumps the counter for the duration of the drain (same idea as the old delete_nesting), and the drain check in _Py_Dealloc becomes just c_dealloc_depth == 0. Counter is only touched for GC types so non-GC types pay nothing. Tests: new test_trashcan_nested_tuple_deep in test_gc.py (100k nested tuples, must clean up without crash); test_gc test_exceptions test_capi all green locally. Docker repro now exits 1 with MemoryError instead of 139 with Segmentation fault. #150722 is the same SP-signal blindness in the comparison path (Py_EnterRecursiveCall) — separate code path, separate PR.
Open Graph Description: Fixes #149146. while True: b = (b, None) under RLIMIT_AS segfaults during cleanup: b = 9 while True: b = (b, None) Docker with ulimit -v 524288: MemoryError → lost sys.stderr → segfault, exit 1...
X Description: Fixes #149146. while True: b = (b, None) under RLIMIT_AS segfaults during cleanup: b = 9 while True: b = (b, None) Docker with ulimit -v 524288: MemoryError → lost sys.stderr → segfault, exit 1...
Opengraph URL: https://github.com/python/cpython/pull/151595
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:9c3a4a5e-0db4-1491-933d-299c9ac6cf4a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AC4E:25F3E7:261A3BA:33E3E46:6A543808 |
| html-safe-nonce | 865818166abb342b3c78bffbfcc7fa20508b863ce145c3c94a7bc7322f513e23 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzRFOjI1RjNFNzoyNjFBM0JBOjMzRTNFNDY6NkE1NDM4MDgiLCJ2aXNpdG9yX2lkIjoiMzU0MzYxNTY3OTA1MDgyMzY4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e91a8a5111da6057c5b0ebbe380f671a18fc138c5a0318825c17ebb48ee962b3 |
| hovercard-subject-tag | pull_request:3882471119 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/python/cpython/pull/151595/files |
| twitter:image | https://avatars.githubusercontent.com/u/15888476?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/15888476?s=400&v=4 |
| og:image:alt | Fixes #149146. while True: b = (b, None) under RLIMIT_AS segfaults during cleanup: b = 9 while True: b = (b, None) Docker with ulimit -v 524288: MemoryError → lost sys.stderr → segfault, exit 1... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width