Title: Creating multiple instances of faulthandler module causes unbalanced refcount on fatal_error.file · Issue #149590 · python/cpython · GitHub
Open Graph Title: Creating multiple instances of faulthandler module causes unbalanced refcount on fatal_error.file · Issue #149590 · python/cpython
X Title: Creating multiple instances of faulthandler module causes unbalanced refcount on fatal_error.file · Issue #149590 · python/cpython
Description: Crash report What happened? In a DEBUG build: import sys, faulthandler, gc faulthandler.enable() del sys.modules["faulthandler"] # we still hold a ref to faulthandler above import faulthandler raises an abort from gc_decref due to negati...
Open Graph Description: Crash report What happened? In a DEBUG build: import sys, faulthandler, gc faulthandler.enable() del sys.modules["faulthandler"] # we still hold a ref to faulthandler above import faulthandler rais...
X Description: Crash report What happened? In a DEBUG build: import sys, faulthandler, gc faulthandler.enable() del sys.modules["faulthandler"] # we still hold a ref to faulthandler above import faultha...
Opengraph URL: https://github.com/python/cpython/issues/149590
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Creating multiple instances of faulthandler module causes unbalanced refcount on fatal_error.file","articleBody":"# Crash report\n\n### What happened?\n\nIn a DEBUG build:\n```python\nimport sys, faulthandler, gc\n\nfaulthandler.enable()\ndel sys.modules[\"faulthandler\"] # we still hold a ref to faulthandler above\nimport faulthandler\n```\nraises an abort from gc_decref due to negative reference counts.\n\nThis /is/ reproducible in non-debug builds (not the assert/abort, but subsequent use-after-free/segfault, but the reproducer is harder to pin down!)\n\nWith the help of chatgpt, we came up with the following guess of the root cause:\n\n - `_PyRuntime.faulthandler.fatal_error.file` is a global singleton reference\n - faulthandler.enable() sets this value to the relevant file ob\n - Because faulthandler uses multi-phase init, then you can end up with 2 separate instances of the module.\n - When gc runs on the faulthandler instances, they both Py_VISIT the fatal_error.file, causing invalid refcount calculations and the above abort.\n\n\u003cdetails>\n\u003csummary>Stack Trace\u003c/summary>\n\n```\n frame #0: 0x0000ffffbdb8e13c libc.so.6`___lldb_unnamed_symbol_8e000 + 316\nlibc.so.6`___lldb_unnamed_symbol_8e000:\n-> 0xffffbdb8e13c \u003c+316>: cmn w0, #0x1, lsl #12 ; =0x1000\n 0xffffbdb8e140 \u003c+320>: csneg w0, wzr, w0, ls\n 0xffffbdb8e144 \u003c+324>: b 0xffffbdb8e0bc ; \u003c+188>\n 0xffffbdb8e148 \u003c+328>: mov x0, x21\n(lldb) bt\n* thread #1, name = 'python', stop reason = signal SIGABRT\n * frame #0: 0x0000ffffbdb8e13c libc.so.6`___lldb_unnamed_symbol_8e000 + 316\n frame #1: 0x0000ffffbdb385fc libc.so.6`raise + 28\n frame #2: 0x0000ffffbdb22720 libc.so.6`abort + 44\n frame #3: 0x0000ffffbdb80a98 libc.so.6`___lldb_unnamed_symbol_808e0 + 440\n frame #4: 0x0000ffffbdb310d8 libc.so.6`__assert_fail + 184\n frame #5: 0x0000aaaae58931c0 python`visit_decref(op=0x0000ffffbd9747d0, data=\u003cunavailable>) at gcmodule.c:277:9\n frame #6: 0x0000aaaae57896b4 python`dict_traverse(op=0x0000ffffbda2f2e0, visit=(python`visit_decref at gcmodule.c:269:1), arg=0x0000000000000000) at dictobject.c:2989:17\n frame #7: 0x0000aaaae5892b84 python`subtract_refs(containers=0x0000aaaae5ac1600) at gcmodule.c:296:16\n frame #8: 0x0000aaaae58944c0 python`collect(generation=2, n_collected=0x0000ffffe9e2d128, n_uncollectable=0x0000ffffe9e2d130, nofail=0) at gcmodule.c:853:5\n frame #9: 0x0000aaaae5894aa4 python`collect_with_callback(generation=2) at gcmodule.c:1025:14\n frame #10: 0x0000aaaae58952b8 python`PyGC_Collect at gcmodule.c:1570:13\n frame #11: 0x0000aaaae58952fc python`_PyGC_CollectIfEnabled at gcmodule.c:1584:12\n frame #12: 0x0000aaaae5864720 python`Py_FinalizeEx at pylifecycle.c:1189:5\n frame #13: 0x0000aaaae5752694 python`pymain_main(pymain=0x0000ffffe9e2d228) at main.c:3127:9\n frame #14: 0x0000aaaae5752798 python`_Py_UnixMain(argc=\u003cunavailable>, argv=\u003cunavailable>) at main.c:3160:12\n frame #15: 0x0000aaaae574aab4 python`main(argc=\u003cunavailable>, argv=\u003cunavailable>) at python.c:15:12\n frame #16: 0x0000ffffbdb22f1c libc.so.6`___lldb_unnamed_symbol_22ea0 + 124\n frame #17: 0x0000ffffbdb2305c libc.so.6`__libc_start_main + 156\n frame #18: 0x0000aaaae574a9b0 python`_start + 48\n```\n\n\u003c/details>\n\nBisect was able to reproduce this as far back as I could compile (3.7.17)\nFound on a main non-debug aarch64 musl linux build, isolated on main debug aarch64 musl linux\nreproduced on ubuntu aarch64 (as below)\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.7.17 (tags/v3.7.17:ecd9946c9d1, May 9 2026, 01:29:57) [GCC 15.2.0]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-150023\n* gh-150037\n* gh-150087\n* gh-150088\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/stestagg","@type":"Person","name":"stestagg"},"datePublished":"2026-05-09T03:00:19Z","url":"https://github.com/python/cpython/issues/149590"}
| 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 | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9e3dc1e5-2fd8-65d7-64f0-840d849dccfd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F7E:269AD9:32943B0:438893E:6B01F367 |
| html-safe-nonce | 573e1ea0f9210c4b130bd22171830b7141a70ac7a4f2178a7c551934073aa525 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjdFOjI2OUFEOTozMjk0M0IwOjQzODg5M0U6NkIwMUYzNjciLCJ2aXNpdG9yX2lkIjoiNjQ5ODk0OTU2OTgzMTg5MTgxNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fe545410c4717143ac4834b1e93fabd76b86015109927ff96d2b057e4286b5c7 |
| hovercard-subject-tag | issue:4410818006 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/python/cpython/149590/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b153ba1f3ae424fca202aeee714ebbc4a643933efa16118fc984dbe814eef10c/python/cpython/issues/149590 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b153ba1f3ae424fca202aeee714ebbc4a643933efa16118fc984dbe814eef10c/python/cpython/issues/149590 |
| og:image:alt | Crash report What happened? In a DEBUG build: import sys, faulthandler, gc faulthandler.enable() del sys.modules["faulthandler"] # we still hold a ref to faulthandler above import faulthandler rais... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stestagg |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| 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 |
| react-app-name | issues |
Links:
Viewport: width=device-width