You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Segmentation fault on macOS when using matplotlib.pyplot.ion() in IPython #27808
When using matplotlib.pyplot.ion() in an instance of IPython, after around 15-30 minutes the IPython instance will exit and "Segmentation fault 11" is printed in the terminal. This occurs with or without additional code execution or interaction with the terminal, and seems to simply happen after some amount of time.
Code for reproduction
importmatplotlib.pyplotaspltplt.ion()
Actual outcome
In [1]: import matplotlib.pyplot as plt
In [2]: plt.ion()
Out[2]: <contextlib.ExitStack at 0x102af0b50>
In [3]: Segmentation fault: 11
To be clear, IPython does not immediately segfault, it happens after upward of 15 minutes of being idle (or being used).
Expected outcome
I would expect the terminal to remain open indefinitely.
Additional information
I have reproduced this bug with both: Python 3.11.7, IPython 8.21.0, matplotlib 3.8.3
and Python 3.9.18, IPython 8.18.1, matplotlib 3.8.2
Both versions of Python were installed using homebrew.
I do not recall this happening with the previous version of Sonoma I was using (14.3).
I am using an M1 MacBook Pro.
I have tried uninstalling and reinstalling matplotlib, which made no difference.
My IPython instances that have not used plt.ion() do not experience this behaviour and will remain open indefinitely.
it happens after upward of 15 minutes of being idle
Oof... that makes it quite a bit harder to test/dig into...
My mac is currently on Sonoma 14.3 (with the 14.3.1 update available)... I can try to validate that the macos version is part of the equation, but the length and uncertainty of testing makes that a little hard to be confident (and AFAIK I can only really go forward, so can't exactly iterate back and forth)
Sonoma 14.3 introduced a deadlock condition (or perhaps rather exposed/made likely) which was reported in #27720 and fixed for the 3.8.3 release in #27755. I mention this mostly just to highlight that there is recent precedence that macos version is in fact an important variable to consider. A quick perusal of the release notes shows a) nothing obviously related to either problem in 14.3 (or 14.4 beta) b) no published notes for 14.3.1. The more info in the updater indicates only a bug fix for text while typing.
As you are using 3.8.3, that particular fix is in and I don't think it should introduce any segfault conditions.
I would be curious on a few fronts:
is it backend dependent (@tacaswell's point above, try using qt, etc. since it's ion related, would need to be an interactive backend)
if not, this is likely backend specific to the MacOS backend
Does it happen with python (rather than ipython).
IPython introduces some amount of additional event loop handling, which I am curious to know if it is interfering here (not exactly sure how, but a variable to isolate
Can we get stack traces using import faulthandler; faulthandler.enable() (or python -X faulthandler if IPython is not involved)
This whould at least help identify what is actually segfaulting.
I've got a process waiting (on 14.3, not 14.3.1) but have not yet replicated the problem (which I'd like to do before ruling things out). If it doesn't happen here after waiting some considerable period, I'll update and see if I can reproduce, but if you are able to test some of these cases, that can help focus my efforts once I'm able to get going..
Thanks all for the responses! I've run a few tests over the past hour.
If you use the qt backend does this happen?
After installing PyQt6, and then using import matplotlib; matplotlib.use('QtAgg'), it doesn't seem to crash. So that could be a workaround in the meantime.
Does it happen with python (rather than ipython).
No!
Can we get stack traces using import faulthandler; faulthandler.enable()
Unfortunately I too have as yet been unable to reproduce the issue (I did update macos after ~40 mins of waiting..._
I'm beginning to suspect a few other variables, e.g. compiler version (which can apply to several steps, Python itself, mpl, maybe even numpy)
The env I started with had CPython compiled with clang 14, mpl installed from wheel
After noting your clang version (15) I got a new venv with newly compiled python to test another instance.
That said... None of the threads appear to be actively in MPL code when the segfault happens, which indicates to me that it may in fact be an IPython bug that e.g. requires the OSX input hook to be used, which happens on ion (but not exclusively). My understanding is that IPython will choose which hook to install when a GUI framework is required (and not sooner) and will use their qt hook when qt is used, etc.
The particular line in the "Current Thread" when the segfault occurs is:
This is indeed a ctypes function call, which can conceivably segfault.
So my next question is: can the segfault be reproduced without matplotlib?
I think that ipython --gui osx should install the input hook at startup.
If you want to be extra sure that mpl/numpy are not involved, you could try a fresh venv with only ipython (and its hard dependencies) installed.
If it is the case that it happens without mpl involvement, I don't think there is much for us to do here, but hopefully IPython (or perhaps prompt_toolkit) has some insights for you.
Activity
tacaswell commentedon Feb 21, 2024
If you use the qt backend does this happen?
ksunden commentedon Feb 21, 2024
Oof... that makes it quite a bit harder to test/dig into...
My mac is currently on Sonoma 14.3 (with the 14.3.1 update available)... I can try to validate that the macos version is part of the equation, but the length and uncertainty of testing makes that a little hard to be confident (and AFAIK I can only really go forward, so can't exactly iterate back and forth)
Sonoma 14.3 introduced a deadlock condition (or perhaps rather exposed/made likely) which was reported in #27720 and fixed for the 3.8.3 release in #27755. I mention this mostly just to highlight that there is recent precedence that macos version is in fact an important variable to consider. A quick perusal of the release notes shows a) nothing obviously related to either problem in 14.3 (or 14.4 beta) b) no published notes for 14.3.1. The more info in the updater indicates only a bug fix for text while typing.
As you are using 3.8.3, that particular fix is in and I don't think it should introduce any segfault conditions.
I would be curious on a few fronts:
ionrelated, would need to be an interactive backend)python(rather thanipython).import faulthandler; faulthandler.enable()(orpython -X faulthandlerif IPython is not involved)I've got a process waiting (on 14.3, not 14.3.1) but have not yet replicated the problem (which I'd like to do before ruling things out). If it doesn't happen here after waiting some considerable period, I'll update and see if I can reproduce, but if you are able to test some of these cases, that can help focus my efforts once I'm able to get going..
greglucas commentedon Feb 21, 2024
I just put this on in the background for an hour and could not reproduce the issue from ipython or python prompts.
Edit: I just upgraded to ipython 8.21.0 and that also never segfaulted.
I am on an Intel mac.
mphanderson commentedon Feb 21, 2024
Thanks all for the responses! I've run a few tests over the past hour.
After installing PyQt6, and then using
import matplotlib; matplotlib.use('QtAgg'), it doesn't seem to crash. So that could be a workaround in the meantime.No!
Yes! Here is the output:
ksunden commentedon Feb 21, 2024
Thanks for the added info...
Unfortunately I too have as yet been unable to reproduce the issue (I did update macos after ~40 mins of waiting..._
I'm beginning to suspect a few other variables, e.g. compiler version (which can apply to several steps, Python itself, mpl, maybe even numpy)
That said... None of the threads appear to be actively in MPL code when the segfault happens, which indicates to me that it may in fact be an IPython bug that e.g. requires the OSX input hook to be used, which happens on
ion(but not exclusively). My understanding is that IPython will choose which hook to install when a GUI framework is required (and not sooner) and will use their qt hook when qt is used, etc.The particular line in the "Current Thread" when the segfault occurs is:
https://github.com/ipython/ipython/blob/8b1204b6c4489c786ada294088a3acef7bd53e90/IPython/terminal/pt_inputhooks/osx.py#L135
This is indeed a
ctypesfunction call, which can conceivably segfault.So my next question is: can the segfault be reproduced without matplotlib?
I think that
ipython --gui osxshould install the input hook at startup.If you want to be extra sure that mpl/numpy are not involved, you could try a fresh venv with only ipython (and its hard dependencies) installed.
If it is the case that it happens without mpl involvement, I don't think there is much for us to do here, but hopefully IPython (or perhaps
prompt_toolkit) has some insights for you.mphanderson commentedon Feb 22, 2024
So I did as you suggested @ksunden and tried
ipython --gui osx, and yes that also segfaults:ksunden commentedon Mar 20, 2024
Closing since the problem is replicated without Matplotlib. If you have not done so already, please report to IPython.