[3.14] gh-135729: Store reference to globals in Interpreter._decref (GH-139104) (GH-139112)

* gh-135729: Store reference to globals in `Interpreter._decref` (GH-139104)
(cherry picked from commit 571210b8f3)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-10-07 20:18:15 +02:00 committed by GitHub
parent 5b64b59618
commit 070993b8ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 2 deletions

View file

@ -149,12 +149,17 @@ def __del__(self):
def __reduce__(self): def __reduce__(self):
return (type(self), (self._id,)) 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: if not self._ownsref:
return return
self._ownsref = False self._ownsref = False
try: try:
_interpreters.decref(self._id) _interp_decref(self._id)
except InterpreterNotFoundError: except InterpreterNotFoundError:
pass pass

View file

@ -419,6 +419,21 @@ def test_pickle(self):
unpickled = pickle.loads(data) unpickled = pickle.loads(data)
self.assertEqual(unpickled, interp) 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.assertNotIn(b"Traceback", stdout)
class TestInterpreterIsRunning(TestBase): class TestInterpreterIsRunning(TestBase):

View file

@ -0,0 +1,2 @@
Fix unraisable exception during finalization when using
:mod:`concurrent.interpreters` in the REPL.