Skip to content

[Bug]: calling pyplot.gca() outside the main thread crashes the interpreter with the MacOS backend #30666

Description

@ngoldbaum

Bug summary

I think #28678 and #28513 are related.

I discovered this while testing matplotlib-venn on the free-threaded build using pytest-run-parallel. I ultimately tracked it down to the plt.gca() call here:

https://github.com/konstantint/matplotlib-venn/blob/7a69a549579faa0286c8de57f6cc3216a2ac4f5e/matplotlib_venn/_venn2.py#L191

Here is a faulthandler report from me triggering this using the matplotlib-venn test suite. You should be able to trigger it yourself by installing matplotlib-venn from source with pip install -e . then run their test suite with cd tests; pytest --parallel-threads=4 and you'll probably trigger this on a Mac.

Details
Fatal Python error: Aborted

Thread 0x0000000177e5b000 [Thread-8 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1713 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 43 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x0000000176e4f000 [Thread-7 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 86 in _wrapreduction
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 3164 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/transforms.py", line 328 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1968 in get_width_height
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 153 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x0000000175e43000 [Thread-6 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 86 in _wrapreduction
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 3164 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/transforms.py", line 328 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1968 in get_width_height
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 43 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x0000000174e37000 [Thread-5 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 47 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x0000000173e2b000 [Thread-4 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 86 in _wrapreduction
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 3164 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/transforms.py", line 328 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1968 in get_width_height
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 43 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Current thread 0x0000000172e1f000 [Thread-3 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 153 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x0000000171e13000 [Thread-2 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1946 in get_width_height
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 153 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x0000000170e07000 [Thread-1 (closure)] (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 86 in _wrapreduction
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/numpy/_core/fromnumeric.py", line 3164 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/transforms.py", line 328 in max
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1968 in get_width_height
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/backend_macosx.py", line 43 in __init__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 2656 in create_with_canvas
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 1785 in new_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3509 in new_figure_manager_given_figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backend_bases.py", line 3504 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 551 in new_figure_manager
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1041 in figure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 1107 in gcf
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/pyplot.py", line 2784 in gca
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 191 in _render_layout
  File "/Users/goldbaum/Documents/matplotlib-venn/matplotlib_venn/_venn2.py", line 173 in venn2
  File "<string>", line 2 in <module>
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/utils.py", line 94 in exec_ipynb
  File "/Users/goldbaum/Documents/matplotlib-venn/tests/functional_test.py", line 18 in test_venn2
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 80 in closure
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1023 in run
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1081 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1043 in _bootstrap

Thread 0x000000020a4860c0 (most recent call first):
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/threading.py", line 1132 in join
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 112 in inner
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/python.py", line 194 in pytest_pyfunc_call
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/python.py", line 1788 in runtest
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 169 in pytest_runtest_call
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 262 in <lambda>
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 341 in from_call
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 261 in call_runtest_hook
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 222 in call_and_report
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 133 in runtestprotocol
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/runner.py", line 114 in pytest_runtest_protocol
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pytest_run_parallel/plugin.py", line 224 in pytest_runtestloop
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/main.py", line 324 in _main
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/main.py", line 270 in wrap_session
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/main.py", line 317 in pytest_cmdline_main
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/config/__init__.py", line 166 in main
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/_pytest/config/__init__.py", line 189 in console_main
  File "/Users/goldbaum/.pyenv/versions/3.14.0t/bin/pytest", line 7 in <module>

Current thread's C stack trace (most recent call first):
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _Py_DumpStack+0x44 [0x10099bb50]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at faulthandler_fatal_error+0x240 [0x1009af968]
  Binary file "/usr/lib/system/libsystem_platform.dylib", at _sigtramp+0x38 [0x19c52d6a4]
  Binary file "/usr/lib/system/libsystem_pthread.dylib", at pthread_kill+0x128 [0x19c4f388c]
  Binary file "/usr/lib/system/libsystem_c.dylib", at abort+0x7c [0x19c3fca3c]
  Binary file "/usr/lib/libc++abi.dylib", at _ZN10__cxxabiv130__aligned_malloc_with_fallbackEm+0x0 [0x19c4a9384]
  Binary file "/usr/lib/libc++abi.dylib", at _ZL28demangling_terminate_handlerv+0x158 [0x19c497cf4]
  Binary file "/usr/lib/libobjc.A.dylib", at _ZL15_objc_terminatev+0x9c [0x19c11cdd4]
  Binary file "/usr/lib/libc++abi.dylib", at _ZSt11__terminatePFvvE+0x10 [0x19c4a8698]
  Binary file "/usr/lib/libc++abi.dylib", at __cxa_get_exception_ptr+0x0 [0x19c4abc30]
  Binary file "/usr/lib/libc++abi.dylib", at _ZN10__cxxabiv1L12failed_throwEPNS_15__cxa_exceptionE+0x0 [0x19c4abbd8]
  Binary file "/usr/lib/libobjc.A.dylib", at objc_exception_throw+0x1c0 [0x19c112cf8]
  Binary file "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation", at _CFBundleGetValueForInfoKey+0x0 [0x19c674154]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSWindow _initContent:styleMask:backing:defer:contentView:]+0x104 [0x1a05131c8]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSWindow initWithContentRect:styleMask:backing:defer:]+0x30 [0x1a05130b8]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/_macosx.cpython-314t-darwin.so", at -[Window initWithContentRect:styleMask:backing:defer:withManager:]+0x34 [0x1032cf4a8]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/python3.14t/site-packages/matplotlib/backends/_macosx.cpython-314t-darwin.so", at FigureManager_init+0x144 [0x1032d2480]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at wrap_init+0x14 [0x10085f920]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at wrapperdescr_call+0x180 [0x1007b52ec]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyObject_MakeTpCall+0x13c [0x1007a5464]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyEval_EvalFrameDefault+0x235c [0x1008f7f1c]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyEval_Vector+0x2cc [0x1008f5918]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyObject_VectorcallDictTstate+0xcc [0x1007a52c4]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyObject_Call_Prepend+0x98 [0x1007a64dc]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at call_method+0x7c [0x100857430]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at slot_tp_init+0x2c [0x10085f7f4]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at type_call+0x1c0 [0x10084f240]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyObject_MakeTpCall+0x13c [0x1007a5464]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyEval_EvalFrameDefault+0x235c [0x1008f7f1c]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyEval_Vector+0x2cc [0x1008f5918]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at method_vectorcall+0xb4 [0x1007a8f64]
  Binary file "/Users/goldbaum/.pyenv/versions/3.14.0t/lib/libpython3.14t.dylib", at _PyObject_Call+0x7c [0x1007a5d40]
  <truncated rest of calls>

Extension modules: numpy._core._multiarray_umath, numpy.linalg._umath_linalg, PIL._imaging, kiwisolver._cext, cython.cimports.libc.math, scipy._lib._ccallback_, scipy.linalg._fblas, scipy.linalg._flapack, _cyutility, scipy._cyutility, scipy.linalg.cython_lapack, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._pcg64, numpy.random._mt19937, numpy.random._generator, numpy.random._philox, numpy.random._sfc64, numpy.random.mtrand, scipy.linalg._cythonized_array_utils, scipy.linalg._solve_toeplitz, scipy.linalg._decomp_lu_cython, scipy.linalg._matfuncs_schur_sqrtm, scipy.linalg._matfuncs_expm, scipy.linalg._linalg_pythran, scipy.linalg.cython_blas, scipy.linalg._decomp_update, scipy.sparse._sparsetools, _csparsetools, scipy.sparse._csparsetools, scipy.sparse.linalg._dsolve._superlu, scipy.sparse.linalg._eigen.arpack._arpack, scipy.sparse.linalg._propack._spropack, scipy.sparse.linalg._propack._dpropack, scipy.sparse.linalg._propack._cpropack, scipy.sparse.linalg._propack._zpropack, scipy.optimize._group_columns, scipy._lib.messagestream, scipy.optimize._trlib._trlib, scipy.optimize._lbfgsb, _moduleTNC, scipy.optimize._moduleTNC, scipy.optimize._slsqplib, scipy.optimize._minpack, scipy.optimize._lsq.givens_elimination, scipy.optimize._zeros, scipy._lib._uarray._uarray, scipy.special._ufuncs_cxx, scipy.special._ellip_harm_2, scipy.special._special_ufuncs, scipy.special._gufuncs, scipy.special._ufuncs, scipy.special._specfun, scipy.special._comb, scipy.linalg._decomp_interpolative, scipy.optimize._bglu_dense, scipy.optimize._lsap, scipy.spatial._ckdtree, scipy.spatial._qhull, scipy.spatial._voronoi, scipy.spatial._hausdorff, scipy.spatial._distance_wrap, scipy.spatial.transform._rotation, scipy.spatial.transform._rigid_transform, scipy.optimize._direct, matplotlib.backends._macosx (total: 67)

Code for reproduction

from concurrent.futures import ThreadPoolExecutor
import threading

from matplotlib import pyplot as plt

def run_threaded(
    func,
    num_threads=8,
):
    """Runs a function many times in parallel"""
    with ThreadPoolExecutor(max_workers=num_threads) as tpe:
        barrier = threading.Barrier(num_threads)
        all_args = [(func, barrier) for i in range(num_threads)]
        try:
            futures = []
            for arg in all_args:
                futures.append(tpe.submit(*arg))
        finally:
            if len(futures) < num_threads:
                barrier.abort()
            for f in futures:
                f.result()

def closure(b):
    b.wait()
    plt.gca()

run_threaded(closure)

Actual outcome

uncaught C++ exception:

○  python test.py
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow should only be instantiated on the main thread!'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000019c64fae0 __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x000000019c112b90 objc_exception_throw + 88
	2   CoreFoundation                      0x000000019c674154 _CFBundleGetValueForInfoKey + 0
	3   AppKit                              0x00000001a05131c8 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 260
	4   AppKit                              0x00000001a05130b8 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
	5   _macosx.cpython-312-darwin.so       0x0000000105c67960 -[Window initWithContentRect:styleMask:backing:defer:withManager:] + 52
	6   _macosx.cpython-312-darwin.so       0x0000000105c6a540 FigureManager_init + 220
	7   libpython3.12.dylib                 0x0000000104a80e54 wrap_init + 20
	8   libpython3.12.dylib                 0x0000000104a1b47c wrapperdescr_call + 380
	9   libpython3.12.dylib                 0x0000000104a0f8b4 _PyObject_MakeTpCall + 308
	10  libpython3.12.dylib                 0x0000000104b163b8 _PyEval_EvalFrameDefault + 17752
	11  libpython3.12.dylib                 0x0000000104a0f6ec _PyObject_FastCallDictTstate + 272
	12  libpython3.12.dylib                 0x0000000104a10730 _PyObject_Call_Prepend + 152
	13  libpython3.12.dylib                 0x0000000104a80d38 slot_tp_init + 208
	14  libpython3.12.dylib                 0x0000000104a790d4 type_call + 432
	15  libpython3.12.dylib                 0x0000000104a0f8b4 _PyObject_MakeTpCall + 308
	16  libpython3.12.dylib                 0x0000000104b163b8 _PyEval_EvalFrameDefault + 17752
	17  libpython3.12.dylib                 0x0000000104a12794 method_vectorcall + 180
	18  libpython3.12.dylib                 0x0000000104a0fe90 _PyVectorcall_Call + 116
	19  libpython3.12.dylib                 0x0000000104b16e28 _PyEval_EvalFrameDefault + 20424
	20  libpython3.12.dylib                 0x0000000104a1281c method_vectorcall + 316
	21  libpython3.12.dylib                 0x0000000104be3b1c thread_run + 92
	22  libpython3.12.dylib                 0x0000000104b824e4 pythread_wrapper + 28
	23  libsystem_pthread.dylib             0x000000019c4f3c0c _pthread_start + 136
	24  libsystem_pthread.dylib             0x000000019c4eeb80 thread_start + 8
)
libc++abi: terminating due to uncaught exception of type NSException
[1]    2332 abort      python test.py

Note that this is with python 3.12 but I can reproduce this with 3.14 and 3.14t as well. Just seems to need a Mac.

Expected outcome

No crash.

Additional information

I'm guessing the MacOS backend needs some sort of locking to prevent threads from corrupting each other's state like this.

Operating system

MacOS

Matplotlib Version

3.10.7

Matplotlib Backend

macosx

Python version

Python 3.12.10 (main, Apr 28 2025, 11:48:32) [Clang 17.0.0 (clang-1700.0.13.3)]

Jupyter version

N/A

Installation

pip

Activity

changed the title [-][Bug]: pyplot.gca() segfaults in a multithreaded context with the MacOS backend[/-] [+][Bug]: calling pyplot.gca() simultaneously in several threads crashes with the MacOS backend[/+] on Oct 21, 2025

timhoffm commented on Oct 21, 2025

@timhoffm
Member

Matplotlib, and in particular pyplot are generally not thread-safe. See https://matplotlib.org/devdocs/users/faq.html#work-with-threads. I’m quite sure you can’t reasonably run plt.gca() in multiple threads simultaneously.

ngoldbaum commented on Oct 21, 2025

@ngoldbaum
ContributorAuthor

My reading of the docs that you link to don't read to me that you expect a crash to be acceptable, just races. I agree that it's a nonsense thing to do, but I'd prefer to see a runtime error instead of a crash. And I think a runtime error is possible without adding much (if any) overhead for the single-threaded case.

ngoldbaum commented on Oct 21, 2025

@ngoldbaum
ContributorAuthor

I could also see closing this in favor of konstantint/matplotlib-venn#87 - feel free to comment over there if you have suggestions for how to structure a matplotlib extension to avoid use of pyplot.

timhoffm commented on Oct 21, 2025

@timhoffm
Member

The extensions uses one of the recommended ways to define plotting functions

def plot_something(data, ..., ax=None, ....):
    ...
    if ax is None:
        ax = plt.gca()

This allows both a pyplot-like style

plot_something(data)
plt.show()

and working with Axes objects

fig, ax = plt.subplots()
plot_something(data, ax=ax)
...

In tests, you could consider not using pyplot at all to create figures. You can instead instantiate the Figure class directly (See also https://matplotlib.org/devdocs/users/faq.html#generate-images-without-having-a-window-appear)

from matplotlib.figure import Figure

def test_something():
    ax = Figure()
    ax = fig.subplots()
    plot_something(data, ax=ax)

Because this does not rely on the pyplot global state, it should be safe to simultaneously run multiple figures/tests in threads.

timhoffm commented on Oct 21, 2025

@timhoffm
Member

My reading of the docs that you link to don't read to me that you expect a crash to be acceptable, just races.

But race conditions can fundamentally lead to a crash, e.g. though a use-after-free.

ngoldbaum commented on Oct 21, 2025

@ngoldbaum
ContributorAuthor

A race in condition in Python code using only builtins (e.g. with a list) may lead to a loss of sequential consistency or determinism, but not a crash in the interpreter.

IMO a crash like that should be considered a bug, even in third-party packages. If only because it's a better user experience to fix the crash, if it's reasonable to do so. A Python exception telling me I'm doing something unsupported is much more friendly than crashing the interpreter with a mysterious error message.

There are several approaches you can take to implement something like that and I'm happy to take on the work to implement, test and benchmark a fix.

timhoffm commented on Oct 21, 2025

@timhoffm
Member

Actually reading the message more closely, the issue is

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow should only be instantiated on the main thread!'

The MacOS backend (or more precisely the FigureManager of that backend) will try to create a window, but that's only possible on the main thread.

The breaking code is here

self->window = [self->window initWithContentRect: rect

We're happy to take a PR to error out more nicely.

changed the title [-][Bug]: calling pyplot.gca() simultaneously in several threads crashes with the MacOS backend[/-] [+][Bug]: calling pyplot.gca() outside the main thread crashes the interpreter with the MacOS backend[/+] on Oct 28, 2025

ngoldbaum commented on Oct 28, 2025

@ngoldbaum
ContributorAuthor

We're happy to take a PR to error out more nicely.

See #30697

added this to the v3.10.8 milestone on Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions