From 7379bf38f08006987a87a85b81cb1dea43e81045 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 20 Jul 2022 19:41:50 -0400 Subject: [PATCH] Backport PR #23445: Compare thread native ids when checking whether running on main thread. --- lib/matplotlib/pyplot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 31719b458807..1f4a34dd4f66 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -311,8 +311,11 @@ class backend_mod(matplotlib.backend_bases._Backend): def _warn_if_gui_out_of_main_thread(): - if (_get_required_interactive_framework(_get_backend_mod()) - and threading.current_thread() is not threading.main_thread()): + # This compares native thread ids because even if python-level Thread + # objects match, the underlying OS thread (which is what really matters) + # may be different on Python implementations with green threads. + if (_get_required_interactive_framework(_get_backend_mod()) and + threading.get_native_id() != threading.main_thread().native_id): _api.warn_external( "Starting a Matplotlib GUI outside of the main thread will likely " "fail.")