gh-128639: Don't assume one thread in subinterpreter finalization (gh-128640)

Incidentally, this also fixed the warning not showing up if a subinterpreter wasn't
cleaned up via _interpreters.destroy. I had to update some of the tests as a result.
This commit is contained in:
Peter Bierma 2025-05-19 12:24:08 -04:00 committed by GitHub
parent c4ad92e155
commit 9859791f9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 99 additions and 38 deletions

View file

@ -647,6 +647,59 @@ class TestInterpreterClose(TestBase):
self.interp_exists(interpid))
def test_remaining_threads(self):
r_interp, w_interp = self.pipe()
FINISHED = b'F'
# It's unlikely, but technically speaking, it's possible
# that the thread could've finished before interp.close() is
# reached, so this test might not properly exercise the case.
# However, it's quite unlikely and I'm too lazy to deal with it.
interp = interpreters.create()
interp.exec(f"""if True:
import os
import threading
import time
def task():
time.sleep(1)
os.write({w_interp}, {FINISHED!r})
threads = [threading.Thread(target=task) for _ in range(3)]
for t in threads:
t.start()
""")
interp.close()
self.assertEqual(os.read(r_interp, 1), FINISHED)
def test_remaining_daemon_threads(self):
interp = _interpreters.create(
types.SimpleNamespace(
use_main_obmalloc=False,
allow_fork=False,
allow_exec=False,
allow_threads=True,
allow_daemon_threads=True,
check_multi_interp_extensions=True,
gil='own',
)
)
_interpreters.exec(interp, f"""if True:
import threading
import time
def task():
time.sleep(100)
threads = [threading.Thread(target=task, daemon=True) for _ in range(3)]
for t in threads:
t.start()
""")
_interpreters.destroy(interp)
class TestInterpreterPrepareMain(TestBase):
def test_empty(self):
@ -755,7 +808,10 @@ class TestInterpreterExec(TestBase):
spam.eggs()
interp = interpreters.create()
interp.exec(script)
try:
interp.exec(script)
finally:
interp.close()
""")
stdout, stderr = self.assert_python_failure(scriptfile)
@ -764,7 +820,7 @@ class TestInterpreterExec(TestBase):
# File "{interpreters.__file__}", line 179, in exec
self.assertEqual(stderr, dedent(f"""\
Traceback (most recent call last):
File "{scriptfile}", line 9, in <module>
File "{scriptfile}", line 10, in <module>
interp.exec(script)
~~~~~~~~~~~^^^^^^^^
{interpmod_line.strip()}