From 3476f451fed353961e81236c00a447709e01b796 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 18 Sep 2025 07:17:51 -0400 Subject: [PATCH 1/2] gh-135729: Store reference to globals in `Interpreter._decref` (GH-139104) (cherry picked from commit 571210b8f34a54922e5eb11d65060d7a77b8bdf0) Co-authored-by: Peter Bierma --- Lib/concurrent/interpreters/__init__.py | 9 +++++++-- Lib/test/test_interpreters/test_api.py | 16 ++++++++++++++++ ...025-09-18-05-32-18.gh-issue-135729.8AmMza.rst | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-09-18-05-32-18.gh-issue-135729.8AmMza.rst diff --git a/Lib/concurrent/interpreters/__init__.py b/Lib/concurrent/interpreters/__init__.py index aa46a2b37a48d5..ea4147ee9a25da 100644 --- a/Lib/concurrent/interpreters/__init__.py +++ b/Lib/concurrent/interpreters/__init__.py @@ -149,12 +149,17 @@ def __del__(self): def __reduce__(self): return (type(self), (self._id,)) - def _decref(self): + # gh-135729: Globals might be destroyed by the time this is called, so we + # need to keep references ourself + def _decref(self, *, + InterpreterNotFoundError=InterpreterNotFoundError, + _interp_decref=_interpreters.decref, + ): if not self._ownsref: return self._ownsref = False try: - _interpreters.decref(self._id) + _interp_decref(self._id) except InterpreterNotFoundError: pass diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index 289e607ad3fad3..facb4531528bcf 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -418,6 +418,22 @@ def test_pickle(self): unpickled = pickle.loads(data) self.assertEqual(unpickled, interp) + @support.requires_subprocess() + @force_not_colorized + def test_cleanup_in_repl(self): + # GH-135729: Using a subinterpreter in the REPL would lead to an unraisable + # exception during finalization + repl = script_helper.spawn_python("-i") + script = b"""if True: + from concurrent import interpreters + interpreters.create() + exit()""" + stdout, stderr = repl.communicate(script) + self.assertIsNone(stderr) + self.assertIn(b"remaining subinterpreters", stdout) + self.assertNotIn(b"Traceback", stdout) + + class TestInterpreterIsRunning(TestBase): diff --git a/Misc/NEWS.d/next/Library/2025-09-18-05-32-18.gh-issue-135729.8AmMza.rst b/Misc/NEWS.d/next/Library/2025-09-18-05-32-18.gh-issue-135729.8AmMza.rst new file mode 100644 index 00000000000000..1777839a1bf633 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-18-05-32-18.gh-issue-135729.8AmMza.rst @@ -0,0 +1,2 @@ +Fix unraisable exception during finalization when using +:mod:`concurrent.interpreters` in the REPL. From d9934da9e8db5d4b34d419c61332e3f28b5d9318 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Thu, 18 Sep 2025 08:07:01 -0400 Subject: [PATCH 2/2] Update Lib/test/test_interpreters/test_api.py --- Lib/test/test_interpreters/test_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index facb4531528bcf..f96cab5b5757a0 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -430,7 +430,6 @@ def test_cleanup_in_repl(self): exit()""" stdout, stderr = repl.communicate(script) self.assertIsNone(stderr) - self.assertIn(b"remaining subinterpreters", stdout) self.assertNotIn(b"Traceback", stdout)