gh-128639: Don't assume one thread in subinterpreter finalization (gh… · python/cpython@9859791 · GitHub
Skip to content

Commit 9859791

Browse files
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.
1 parent c4ad92e commit 9859791

6 files changed

Lines changed: 99 additions & 38 deletions

File tree

Lib/test/test_interpreters/test_api.py

Lines changed: 58 additions & 2 deletions

Lib/test/test_interpreters/test_lifecycle.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def test_sys_path_0(self):
132132
'sub': sys.path[0],
133133
}}, indent=4), flush=True)
134134
""")
135+
interp.close()
135136
'''
136137
# <tmp>/
137138
# pkg/
@@ -172,7 +173,10 @@ def test_gh_109793(self):
172173
argv = [sys.executable, '-c', '''if True:
173174
from test.support import interpreters
174175
interp = interpreters.create()
175-
raise Exception
176+
try:
177+
raise Exception
178+
finally:
179+
interp.close()
176180
''']
177181
proc = subprocess.run(argv, capture_output=True, text=True)
178182
self.assertIn('Traceback', proc.stderr)

Lib/test/test_threading.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,10 +1689,7 @@ def f():
16891689
16901690
_testcapi.run_in_subinterp(%r)
16911691
""" % (subinterp_code,)
1692-
with test.support.SuppressCrashReport():
1693-
rc, out, err = assert_python_failure("-c", script)
1694-
self.assertIn("Fatal Python error: Py_EndInterpreter: "
1695-
"not the last thread", err.decode())
1692+
assert_python_ok("-c", script)
16961693

16971694
def _check_allowed(self, before_start='', *,
16981695
allowed=True,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash when using threads inside of a subinterpreter.

Programs/_testembed.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,9 +1395,12 @@ static int test_audit_subinterpreter(void)
13951395
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
13961396
_testembed_initialize();
13971397

1398-
Py_NewInterpreter();
1399-
Py_NewInterpreter();
1400-
Py_NewInterpreter();
1398+
PyThreadState *tstate = PyThreadState_Get();
1399+
for (int i = 0; i < 3; ++i)
1400+
{
1401+
Py_EndInterpreter(Py_NewInterpreter());
1402+
PyThreadState_Swap(tstate);
1403+
}
14011404

14021405
Py_Finalize();
14031406

Python/pylifecycle.c

Lines changed: 28 additions & 28 deletions

0 commit comments

Comments
 (0)