Title: gh-149590: Remove faulthandler_traverse by armaan-v924 · Pull Request #150023 · python/cpython · GitHub
Open Graph Title: gh-149590: Remove faulthandler_traverse by armaan-v924 · Pull Request #150023 · python/cpython
X Title: gh-149590: Remove faulthandler_traverse by armaan-v924 · Pull Request #150023 · python/cpython
Description: tldr; faulthandler_traverse visits Python objects owned by _PyRuntime, not by the module instance. With multi-phase init allowing multiple module instances, each instance's GC traversal decrements gc_refs on the same runtime-owned objects, driving it negative when two instances are collected simultaneously. Cleanup is already handled well, traversal is redundant, and harmless with a single instantiation. Expanded Explanation: Author's note: This is my first foray into the CPython codebase. Would appreciate all the scrutiny :D RCA: BG: faulthandler uses multi-phase init, so deleting it from sys.modules and re-importing produces a second, independent module object. Both module objects are tracked by the garbage collector and both have md_state_traverse = faulthandler_traverse stored. subtract_refs iterates every object in the GC generation list. For each object it calls Py_TYPE(op)->tp_traverse — for module objects that's module_traverse in Objects/moduleobject.c module_traverse calls md_state_traverse (i.e. faulthandler_traverse) then visits md_dict faulthandler_traverse calls Py_VISIT(fatal_error.file)(expands to visit_decref(_PyRuntime.faulthandler.fatal_error.file)) With two module instances this runs twice; gc_refs on fatal_error.file (stderr, by default) is decremented twice But fatal_error.file's real refcount only has one reference from _PyRuntime; gc_refs goes negative → debug assertion fires, silent UAF in release Safety m_traverse exists to tell the GC about Python objects owned by per-module C state (md_state, allocated when m_size > 0). faulthandler has m_size = 0, so there is no per-module C state. fatal_error.file, thread.file, and user_signals[signum].file are all owned by _PyRuntime, not by any module instance. _PyRuntime is not a Python object and is never in containers, so its references are never passed to visit_decref, they contribute to ob_refcnt but are never subtracted, meaning gc_refs >= 1 after subtract_refs. GC will never mark these objects as unreachable regardless of whether the module traverse visits them. Cleanup is already handled correctly: faulthandler_disable() calls Py_CLEAR(fatal_error.file) (and equivalents), and _PyFaulthandler_Fini() calls faulthandler_disable() at shutdown. No GC involvement is needed. Functionality From main: After removal: Issue: gh-149590
Open Graph Description: tldr; faulthandler_traverse visits Python objects owned by _PyRuntime, not by the module instance. With multi-phase init allowing multiple module instances, each instance's GC traversal decreme...
X Description: tldr; faulthandler_traverse visits Python objects owned by _PyRuntime, not by the module instance. With multi-phase init allowing multiple module instances, each instance's GC traversal dec...
Opengraph URL: https://github.com/python/cpython/pull/150023
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:b3280dfb-bf58-28dd-3b6f-c5afe4d259e2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B822:1DC4CC:14EC5CB:1BDA8E2:6B01FEE9 |
| html-safe-nonce | 86b74718e0795ef70187414a839dfdb304436ed099e4e3457e174a377d71bbfd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODIyOjFEQzRDQzoxNEVDNUNCOjFCREE4RTI6NkIwMUZFRTkiLCJ2aXNpdG9yX2lkIjoiMTg3Nzk3NTUyNTcxMzExNjkwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c0b339c05fcc5dc858c3a3ba714b8692207f330afd57bab1c4787046a957ffdc |
| hovercard-subject-tag | pull_request:3704274481 |
| 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/150023/files |
| twitter:image | https://avatars.githubusercontent.com/u/43391096?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/43391096?s=400&v=4 |
| og:image:alt | tldr; faulthandler_traverse visits Python objects owned by _PyRuntime, not by the module instance. With multi-phase init allowing multiple module instances, each instance's GC traversal decreme... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width