-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Creating a subinterpreter when Python is run with -X tracemalloc leads to segfault #134604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Looked into this a bit more. I'm not very familiar with subinterpreters internals or the Python runtime lifecycle, so apologies if the following is wrong. It seems that tracemalloc holds references to frame information, which is not safe with multiple interpreters that may be destroyed before tracemalloc drops it's references. I was able to make the minimal reproducer pass by moving Lines 2128 to 2159 in 2fd09b0
This works because frame(s) created by the However, that won't work in the general case. Since subinterpreters can be created and destroyed at will, tracemalloc could always potentially hang on to dead references. For example, the following leads to a segfault even after re-ordering finalization steps: import _interpreters
import tracemalloc
tracemalloc.start()
interpid = _interpreters.create()
_interpreters.destroy(interpid) I think a potential solution might be to copy the filename from the Python frame to the tracemalloc frame? That way the information is not lost when a subinterpreter is destroyed. If that does sound like a good idea, I can go ahead and work on a PR to do that. |
Hm, copying is tricky because making a new unicode object can deadlock the interpreter, I guess we could store the filename as a |
See also #126315, seems like tracemalloc needs more work to be safe with multiple threads/interpreters. |
Uh oh!
There was an error while loading. Please reload this page.
Crash report
What happened?
Minimal reproducer:
python -X tracemalloc subtest.py
This also fails if one submits a task to
concurrent.futures.InterpreterPoolExecutor
.Example output when run with
-X tracemalloc
:That points here:
cpython/Python/tracemalloc.c
Lines 701 to 706 in f478331
I presume the issue is that the filename is not populated for the subinterpreter?
I'm on Ubuntu 24.04 on an x86_64 machine if that makes a difference. My latest configure command line is:
Note that this also reproduces with the GIL enabled, (but it just outputs an unhelpful "segmentation fault").
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.15.0a0 experimental free-threading build (heads/main:f478331f989, May 23 2025, 16:17:11) [Clang 20.1.5 (++20250430014901+ebfae55af454-1
exp120250430014920.111)]Linked PRs
The text was updated successfully, but these errors were encountered: