Title: Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection) · Issue #140067 · python/cpython · GitHub
Open Graph Title: Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection) · Issue #140067 · python/cpython
X Title: Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection) · Issue #140067 · python/cpython
Description: Bug report Bug description: Bug Description: I've discovered a memory leak when running test_sys with AddressSanitizer enabled. The leak occurs during subinterpreter creation and the allocated memory is never freed. Environment: CPython ...
Open Graph Description: Bug report Bug description: Bug Description: I've discovered a memory leak when running test_sys with AddressSanitizer enabled. The leak occurs during subinterpreter creation and the allocated memo...
X Description: Bug report Bug description: Bug Description: I've discovered a memory leak when running test_sys with AddressSanitizer enabled. The leak occurs during subinterpreter creation and the allocated ...
Opengraph URL: https://github.com/python/cpython/issues/140067
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection)","articleBody":"# Bug report\n\n### Bug description:\n\n**Bug Description:**\n\nI've discovered a memory leak when running `test_sys` with AddressSanitizer enabled. The leak occurs during subinterpreter creation and the allocated memory is never freed.\n\n**Environment:**\n- CPython versions affected: **3.14 and 3.15 (main branch)**\n- Operating System: Linux (Ubuntu-based)\n- Build configuration:\n```bash\nCC=clang CXX=clang++ ./configure \\\n --disable-optimizations \\\n --with-valgrind \\\n --with-pydebug \\\n --enable-pystats \\\n --with-address-sanitizer\n```\n\n**Steps to Reproduce:**\n\n1. Build CPython with the configuration above (tested on both 3.14 and main branches)\n2. Run: `./python -m test test_sys`\n3. Observe AddressSanitizer output at the end\n\n**Expected Behavior:**\nNo memory leaks should be detected by AddressSanitizer.\n\n**Actual Behavior:**\nAddressSanitizer reports approximately 4.5 MB leaked across 20 allocations. All allocations trace back to `_PyInterpreterState_New` → `alloc_interpreter` → `_PyMem_DebugRawCalloc`.\n\nThe test itself passes successfully, but the leak persists:\n```\n== Tests result: SUCCESS ==\n1 test OK.\n=================================================================\nERROR: LeakSanitizer: detected memory leaks\n```\n\n**Reproduction on Multiple Versions:**\n- **3.14 branch**: 4.5 MB leaked (4517100 bytes in 20 allocations)\n- **3.15 (main)**: 4.5 MB leaked (4516460 bytes in 20 allocations)\n\n**Additional Context:**\n\nThe leak appears to be related to subinterpreter lifecycle management. The stack trace shows memory is allocated through:\n- `interp_create` (Modules/_interpretersmodule.c)\n- `_PyXI_NewInterpreter` (Python/crossinterp.c:3204)\n- `Py_NewInterpreterFromConfig` (Python/pylifecycle.c)\n- `new_interpreter` (Python/pylifecycle.c)\n- `_PyInterpreterState_New` (Python/pystate.c)\n- `alloc_interpreter` (Python/pystate.c)\n\nThis issue appears to be related to #110411, which remains open with the latest report from August 2025 stating \"This is still a problem in 3.13.7 and 3.14.0rc2\". Issue #113055 was closed as resolved in February 2025, but the leak still occurs on current 3.14 and main branches.\n\n\u003cdetails\u003e\n\u003csummary\u003eComplete AddressSanitizer Output (3.15 main branch)\u003c/summary\u003e\n\n```\n➜ main git:(main) sudo ./build/main/python -m test test_sys\nUsing random seed: 1335876681\n0:00:00 load avg: 25.72 Run 1 test sequentially in a single process\n0:00:00 load avg: 25.72 [1/1] test_sys\n0:00:20 load avg: 27.57 [1/1] test_sys passed\n\n== Tests result: SUCCESS ==\n\n1 test OK.\n\nTotal duration: 20.6 sec\nTotal tests: run=92 skipped=6\nTotal test files: run=1/1\nResult: SUCCESS\n\n=================================================================\n==544756==ERROR: LeakSanitizer: detected memory leaks\n\nDirect leak of 4290637 byte(s) in 19 object(s) allocated from:\n #0 0x5c153a3eef9d in calloc (/home/shamil/oss/cpython/main/build/main/python+0x33bf9d) (BuildId: c97dc91acec24b8d7d7a9344676b1bb0726ae568)\n #1 0x5c153a777a5e in _PyMem_DebugRawAlloc /home/shamil/oss/cpython/main/build/main/../../Objects/obmalloc.c:2884:24\n #2 0x5c153a777a5e in _PyMem_DebugRawCalloc /home/shamil/oss/cpython/main/build/main/../../Objects/obmalloc.c:2929:12\n #3 0x5c153abd8578 in alloc_interpreter /home/shamil/oss/cpython/main/build/main/../../Python/pystate.c:462:17\n #4 0x5c153abd8578 in _PyInterpreterState_New /home/shamil/oss/cpython/main/build/main/../../Python/pystate.c:660:18\n #5 0x5c153ab8e818 in new_interpreter /home/shamil/oss/cpython/main/build/main/../../Python/pylifecycle.c:2425:14\n #6 0x5c153ab8e592 in Py_NewInterpreterFromConfig /home/shamil/oss/cpython/main/build/main/../../Python/pylifecycle.c:2521:12\n #7 0x5c153aab1b58 in _PyXI_NewInterpreter /home/shamil/oss/cpython/main/build/main/../../Python/crossinterp.c:3204:23\n #8 0x726f325c23ae in _interpreters_create_impl /home/shamil/oss/cpython/main/build/main/../../Modules/_interpretersmodule.c:880:13\n #9 0x726f325c23ae in _interpreters_create /home/shamil/oss/cpython/main/build/main/../../Modules/clinic/_interpretersmodule.c.h:91:20\n #10 0x5c153a5e91c2 in _PyObject_VectorcallTstate /home/shamil/oss/cpython/main/build/main/../../Include/internal/pycore_call.h:169:11\n #11 0x5c153aa11ff6 in _PyEval_EvalFrameDefault /home/shamil/oss/cpython/main/build/main/../../Python/generated_cases.c.h:3188:35\n [... rest of stack trace truncated for brevity ...]\n\nDirect leak of 225823 byte(s) in 1 object(s) allocated from:\n #0 0x5c153a3eef9d in calloc (/home/shamil/oss/cpython/main/build/main/python+0x33bf9d) (BuildId: c97dc91acec24b8d7d7a9344676b1bb0726ae568)\n #1 0x5c153a777a5e in _PyMem_DebugRawAlloc /home/shamil/oss/cpython/main/build/main/../../Objects/obmalloc.c:2884:24\n #2 0x5c153a777a5e in _PyMem_DebugRawCalloc /home/shamil/oss/cpython/main/build/main/../../Objects/obmalloc.c:2929:12\n #3 0x5c153abd8578 in alloc_interpreter /home/shamil/oss/cpython/main/build/main/../../Python/pystate.c:462:17\n #4 0x5c153abd8578 in _PyInterpreterState_New /home/shamil/oss/cpython/main/build/main/../../Python/pystate.c:660:18\n [... rest of stack trace truncated for brevity ...]\n\nSUMMARY: AddressSanitizer: 4516460 byte(s) leaked in 20 allocation(s).\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eComplete AddressSanitizer Output (3.14 branch)\u003c/summary\u003e\n\n```\n➜ 3.14 git:(3.14) sudo ./python -m test test_sys\nUsing random seed: 641571276\n0:00:00 load avg: 2.97 Run 1 test sequentially in a single process\n0:00:00 load avg: 2.97 [1/1] test_sys\n0:00:06 load avg: 2.66 [1/1] test_sys passed\n\n== Tests result: SUCCESS ==\n\n1 test OK.\n\nTotal duration: 6.7 sec\nTotal tests: run=97 skipped=7\nTotal test files: run=1/1\nResult: SUCCESS\n\n=================================================================\n==570353==ERROR: LeakSanitizer: detected memory leaks\n\nDirect leak of 4291245 byte(s) in 19 object(s) allocated from:\n #0 0x5ea8303c6f8d in calloc (/home/shamil/oss/cpython/3.14/python+0x325f8d) (BuildId: bd4e3657446f867dffe5f140bfa56307be2c714a)\n #1 0x5ea83074b5be in _PyMem_DebugRawAlloc /home/shamil/oss/cpython/3.14/Objects/obmalloc.c:2869:24\n #2 0x5ea83074b5be in _PyMem_DebugRawCalloc /home/shamil/oss/cpython/3.14/Objects/obmalloc.c:2914:12\n #3 0x5ea830b8f5ec in alloc_interpreter /home/shamil/oss/cpython/3.14/Python/pystate.c:570:17\n #4 0x5ea830b8f5ec in _PyInterpreterState_New /home/shamil/oss/cpython/3.14/Python/pystate.c:757:18\n #5 0x5ea830b5a2c8 in new_interpreter /home/shamil/oss/cpython/3.14/Python/pylifecycle.c:2313:14\n #6 0x5ea830b5a042 in Py_NewInterpreterFromConfig /home/shamil/oss/cpython/3.14/Python/pylifecycle.c:2409:12\n #7 0x5ea830a7cd78 in _PyXI_NewInterpreter /home/shamil/oss/cpython/3.14/Python/crossinterp.c:3204:23\n #8 0x7be02a35a2a8 in interp_create /home/shamil/oss/cpython/3.14/./Modules/_interpretersmodule.c:862:13\n [... rest of stack trace truncated for brevity ...]\n\nDirect leak of 225855 byte(s) in 1 object(s) allocated from:\n #0 0x5ea8303c6f8d in calloc (/home/shamil/oss/cpython/3.14/python+0x325f8d) (BuildId: bd4e3657446f867dffe5f140bfa56307be2c714a)\n #1 0x5ea83074b5be in _PyMem_DebugRawAlloc /home/shamil/oss/cpython/3.14/Objects/obmalloc.c:2869:24\n #2 0x5ea83074b5be in _PyMem_DebugRawCalloc /home/shamil/oss/cpython/3.14/Objects/obmalloc.c:2914:12\n #3 0x5ea830b8f5ec in alloc_interpreter /home/shamil/oss/cpython/3.14/Python/pystate.c:570:17\n #4 0x5ea830b8f5ec in _PyInterpreterState_New /home/shamil/oss/cpython/3.14/Python/pystate.c:757:18\n [... rest of stack trace truncated for brevity ...]\n\nSUMMARY: AddressSanitizer: 4517100 byte(s) leaked in 20 allocation(s).\n```\n\u003c/details\u003e\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-140111\n* gh-140118\n* gh-140140\n* gh-140148\n* gh-140261\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ashm-dev","@type":"Person","name":"ashm-dev"},"datePublished":"2025-10-13T22:37:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":17},"url":"https://github.com/140067/cpython/issues/140067"}
| 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:747497a8-8877-a1e0-2f1f-bef6bc8b1bca |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8CA4:275F42:828CD7:B4A9B3:696A8066 |
| html-safe-nonce | b9876ffb01e1c2158d070c6f6b9deff7c9981d69bc513373dd081170b0c2f55b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Q0E0OjI3NUY0Mjo4MjhDRDc6QjRBOUIzOjY5NkE4MDY2IiwidmlzaXRvcl9pZCI6IjE3MDYxNzE5MTYxMDg4NTc0NDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9a682959b20f965893d0b65dae86486cead45b675926712c347422a93e940490 |
| hovercard-subject-tag | issue:3511682633 |
| 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/140067/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0f01c3c914ad899605f1712e290d90c38c2ef57adf3eef662d99d9b2c31cd8a2/python/cpython/issues/140067 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0f01c3c914ad899605f1712e290d90c38c2ef57adf3eef662d99d9b2c31cd8a2/python/cpython/issues/140067 |
| og:image:alt | Bug report Bug description: Bug Description: I've discovered a memory leak when running test_sys with AddressSanitizer enabled. The leak occurs during subinterpreter creation and the allocated memo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ashm-dev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 913560fa317c3c5a71e34f9b19253c9f09d02b4b958a86c2a56f4c8541116377 |
| 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 | 5998c30593994bf2589055aef7b22d368a499367 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width