Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/concurrent/interpreters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ def create():

def list_all():
"""Return all existing interpreters."""
return [Interpreter(id, _whence=whence)
for id, whence in _interpreters.list_all(require_ready=True)]
interps = []
for id, whence in _interpreters.list_all(require_ready=True):
try:
interps.append(Interpreter(id, _whence=whence))
except InterpreterNotFoundError:
# It was destroyed after it was listed.
pass
return interps


def get_current():
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ def test_idempotent(self):
for interp1, interp2 in zip(actual, expected):
self.assertIs(interp1, interp2)

def test_destroyed_by_gc(self):
# gh-155997: the interpreter is destroyed while list_all() runs.
interp = interpreters.create()
interpid = interp.id
cycle = []
cycle.append(cycle)
cycle.append(interp)
# The cycle holds the only reference, so only the collector frees it.
with support.disable_gc():
del interp, cycle

with support.gc_threshold(1):
ids = [i.id for i in interpreters.list_all()]
self.assertNotIn(interpid, ids)

def test_created_with_capi(self):
mainid, *_ = _interpreters.get_main()
interpid1 = _interpreters.create()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`concurrent.interpreters.list_all`. It failed if an interpreter
was destroyed during the call, in particular by a garbage collection which
finalized the object owning the last reference to it.
Loading