Skip to content

Commit 134000b

Browse files
greglucasngoldbaum
authored andcommitted
Merge pull request matplotlib#30697 from ngoldbaum/fix-plotting-on-worker-threads
BUG: raise when creating a MacOS FigureManager outside the main thread (cherry picked from commit 94def4e)
1 parent 5b8e219 commit 134000b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/matplotlib/tests/test_backend_macosx.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import threading
23
from pathlib import Path
34

45
import pytest
@@ -84,3 +85,25 @@ def _test_save_figure_return():
8485
def test_save_figure_return():
8586
subprocess_run_helper(_test_save_figure_return, timeout=_test_timeout,
8687
extra_env={"MPLBACKEND": "macosx"})
88+
89+
90+
def _test_create_figure_on_worker_thread_fails():
91+
def create_figure():
92+
warn_msg = "Matplotlib GUI outside of the main thread will likely fail."
93+
err_msg = "Cannot create a GUI FigureManager outside the main thread"
94+
with pytest.warns(UserWarning, match=warn_msg):
95+
with pytest.raises(RuntimeError, match=err_msg):
96+
plt.gcf()
97+
98+
worker = threading.Thread(target=create_figure)
99+
worker.start()
100+
worker.join()
101+
102+
103+
@pytest.mark.backend('macosx', skip_on_importerror=True)
104+
def test_create_figure_on_worker_thread_fails():
105+
subprocess_run_helper(
106+
_test_create_figure_on_worker_thread_fails,
107+
timeout=_test_timeout,
108+
extra_env={"MPLBACKEND": "macosx"}
109+
)

src/_macosx.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,16 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name)
580580
static PyObject*
581581
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
582582
{
583+
if (![NSThread isMainThread]) {
584+
PyErr_SetString(
585+
PyExc_RuntimeError,
586+
"Cannot create a GUI FigureManager outside the main thread "
587+
"using the MacOS backend. Use a non-interactive "
588+
"backend like 'agg' to make plots on worker threads."
589+
);
590+
return NULL;
591+
}
592+
583593
lazy_init();
584594
Window* window = [Window alloc];
585595
if (!window) { return NULL; }

0 commit comments

Comments
 (0)