Title: Segmentation fault during interpreter finalization in PyTraceMalloc_Untrack · Issue #129185 · python/cpython · GitHub
Open Graph Title: Segmentation fault during interpreter finalization in PyTraceMalloc_Untrack · Issue #129185 · python/cpython
X Title: Segmentation fault during interpreter finalization in PyTraceMalloc_Untrack · Issue #129185 · python/cpython
Description: Crash report What happened? _PyTraceMalloc_Fini is called before _PyImport_Fini in _Py_Finalize. This can lead to crashes when using tracemalloc if calls to PyTraceMalloc_Untrack are performed during finalization of module (e.g in the de...
Open Graph Description: Crash report What happened? _PyTraceMalloc_Fini is called before _PyImport_Fini in _Py_Finalize. This can lead to crashes when using tracemalloc if calls to PyTraceMalloc_Untrack are performed duri...
X Description: Crash report What happened? _PyTraceMalloc_Fini is called before _PyImport_Fini in _Py_Finalize. This can lead to crashes when using tracemalloc if calls to PyTraceMalloc_Untrack are performed duri...
Opengraph URL: https://github.com/python/cpython/issues/129185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segmentation fault during interpreter finalization in PyTraceMalloc_Untrack","articleBody":"# Crash report\n\n### What happened?\n\n`_PyTraceMalloc_Fini` is called before `_PyImport_Fini` in `_Py_Finalize`. This can lead to crashes when using tracemalloc if calls to `PyTraceMalloc_Untrack` are performed during finalization of module (e.g in the destructor of some global variable).\n\nCommit 6b47499510e47c0401d1f6cca2660fc12c496e39 moved the call to `TABLES_LOCK` before the check `tracemalloc_config.tracing` so now the issue whether or not tracemalloc is started.\n\nThe following extension module reproduces the issue:\n```c\n#define PY_SSIZE_T_CLEAN\n#include \u003cPython.h>\n#include \u003cnumpy/arrayobject.h>\n\nstatic PyObject* arr = NULL;\n\nstatic struct PyModuleDef repromodule = {\n PyModuleDef_HEAD_INIT,\n \"repro\",\n NULL,\n -1,\n NULL\n};\n\nPyMODINIT_FUNC\nPyInit_repro(void)\n{\n PyObject* m;\n\n m = PyModule_Create(&repromodule);\n if (!m)\n return NULL;\n\n import_array();\n\n arr = PyArray_SimpleNew(1, (npy_intp[]){0}, NPY_DOUBLE);\n if (!arr || PyModule_AddObject(m, \"arr\", arr) \u003c 0) {\n Py_XDECREF(arr);\n Py_DECREF(m);\n return NULL;\n }\n\n return m;\n}\n```\n\n```sh\n$ python -c 'import repro'\n```\nIt declares a global NumPy array and `array_dealloc` calls `PyTraceMalloc_Untrack`, creating a segfault.\n\nFor context, I initially ran into the issue in a pybind11 extension declaring functions with numpy array as defaults arguments.\n\n### CPython versions tested on:\n\nCPython main branch, 3.13\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.13.1+ (heads/3.13:a3797492179, Jan 22 2025, 11:46:15) [GCC 11.4.0]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-129188\n* gh-129191\n* gh-129217\n* gh-129218\n* gh-129221\n* gh-129246\n* gh-129256\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/rostan-t","@type":"Person","name":"rostan-t"},"datePublished":"2025-01-22T16:06:19Z","url":"https://github.com/python/cpython/issues/129185"}
| 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:f53f8039-8fd8-c4e3-36cb-99593d04c7c1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC9C:D4E59:65208A:7FA066:6B020100 |
| html-safe-nonce | e74f566ceaafb26e5e4bb824318a323be8ad3e85785b8397fa82a55146caab75 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzlDOkQ0RTU5OjY1MjA4QTo3RkEwNjY6NkIwMjAxMDAiLCJ2aXNpdG9yX2lkIjoiMjE1NTc3NDkzODEzMTMzMzM3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bcb8f67d5865b0543a57605c18bb7172786eab34854fc1b1cc875b07fef64645 |
| hovercard-subject-tag | issue:2804781869 |
| 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/129185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/feeaa216b252c26a6bf39a9caf3d74780122b89eda13d2bad101c2aa35c82f52/python/cpython/issues/129185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/feeaa216b252c26a6bf39a9caf3d74780122b89eda13d2bad101c2aa35c82f52/python/cpython/issues/129185 |
| og:image:alt | Crash report What happened? _PyTraceMalloc_Fini is called before _PyImport_Fini in _Py_Finalize. This can lead to crashes when using tracemalloc if calls to PyTraceMalloc_Untrack are performed duri... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rostan-t |
| 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