Skip to content

Commit 94def4e

Browse files
authored
Merge pull request matplotlib#30697 from ngoldbaum/fix-plotting-on-worker-threads
BUG: raise when creating a MacOS FigureManager outside the main thread
2 parents 53a5bc6 + 3c503df commit 94def4e

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
@@ -582,6 +582,16 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name)
582582
static PyObject*
583583
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
584584
{
585+
if (![NSThread isMainThread]) {
586+
PyErr_SetString(
587+
PyExc_RuntimeError,
588+
"Cannot create a GUI FigureManager outside the main thread "
589+
"using the MacOS backend. Use a non-interactive "
590+
"backend like 'agg' to make plots on worker threads."
591+
);
592+
return NULL;
593+
}
594+
585595
lazy_init();
586596
Window* window = [Window alloc];
587597
if (!window) { return NULL; }

0 commit comments

Comments
 (0)