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

Commit a648813

Browse files
gh-128639: Don't assume one thread in subinterpreter finalization with fixed daemon thread support (GH-134606)
This reapplies GH-128640.
1 parent 299de38 commit a648813

6 files changed

Lines changed: 114 additions & 39 deletions

File tree

Lib/test/test_interpreters/test_api.py

Lines changed: 68 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 concurrent 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
@@ -1794,10 +1794,7 @@ def f():
17941794
17951795
_testcapi.run_in_subinterp(%r)
17961796
""" % (subinterp_code,)
1797-
with test.support.SuppressCrashReport():
1798-
rc, out, err = assert_python_failure("-c", script)
1799-
self.assertIn("Fatal Python error: Py_EndInterpreter: "
1800-
"not the last thread", err.decode())
1797+
assert_python_ok("-c", script)
18011798

18021799
def _check_allowed(self, before_start='', *,
18031800
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
@@ -1388,9 +1388,12 @@ static int test_audit_subinterpreter(void)
13881388
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
13891389
_testembed_initialize();
13901390

1391-
Py_NewInterpreter();
1392-
Py_NewInterpreter();
1393-
Py_NewInterpreter();
1391+
PyThreadState *tstate = PyThreadState_Get();
1392+
for (int i = 0; i < 3; ++i)
1393+
{
1394+
Py_EndInterpreter(Py_NewInterpreter());
1395+
PyThreadState_Swap(tstate);
1396+
}
13941397

13951398
Py_Finalize();
13961399

Python/pylifecycle.c

Lines changed: 33 additions & 29 deletions

0 commit comments

Comments
 (0)