From 41bf7cc1a17daf23d9eb711698b28b47f443f7c5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 13 Feb 2024 10:53:09 -0500 Subject: [PATCH] FIX: be careful about communicating with subprocess On windows on py312 we can not use `.communicate` during process shutdown because it internally uses Python threads which can no longer be created during interpreter shutdown. Closes #27437 --- lib/matplotlib/backends/backend_pgf.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 78d15fe29b88..ba01622bd068 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -257,7 +257,8 @@ def _setup_latex_process(self, *, expect_reply=True): # Open LaTeX process for real work; register it for deletion. On # Windows, we must ensure that the subprocess has quit before being # able to delete the tmpdir in which it runs; in order to do so, we - # must first `kill()` it, and then `communicate()` with it. + # must first `kill()` it, and then `communicate()` with or `wait()` on + # it. try: self.latex = subprocess.Popen( [mpl.rcParams["pgf.texsystem"], "-halt-on-error"], @@ -274,7 +275,10 @@ def _setup_latex_process(self, *, expect_reply=True): def finalize_latex(latex): latex.kill() - latex.communicate() + try: + latex.communicate() + except RuntimeError: + latex.wait() self._finalize_latex = weakref.finalize( self, finalize_latex, self.latex)