8000 gh-76785: Handle Legacy Interpreters Properly by ericsnowcurrently · Pull Request #117490 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-76785: Handle Legacy Interpreters Properly #117490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Fix destroying a not-ready interpreter.
  • Loading branch information
ericsnowcurrently committed Apr 11, 2024
commit e38f141d5653bafc9fae65a170d81400a3a1f61e
2 changes: 1 addition & 1 deletion Lib/test/test_interpreters/test_api.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ def test_whence(self):
self.assertEqual(whence, _interpreters.WHENCE_STDLIB)

for orig, name in {
# XXX Also check WHENCE_UNKNOWN.
_interpreters.WHENCE_UNKNOWN: 'not ready',
_interpreters.WHENCE_LEGACY_CAPI: 'legacy C-API',
_interpreters.WHENCE_CAPI: 'C-API',
_interpreters.WHENCE_XI: 'cross-interpreter C-API',
Expand Down
27 changes: 15 additions & 12 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,20 @@ void
_PyXI_EndInterpreter(PyInterpreterState *interp,
PyThreadState *tstate, PyThreadState **p_save_tstate)
{
long whence = _PyInterpreterState_GetWhence(interp);
assert(whence != _PyInterpreterState_WHENCE_RUNTIME);

if (!_PyInterpreterState_IsReady(interp)) {
assert(whence == _PyInterpreterState_WHENCE_UNKNOWN);
// PyInterpreterState_Clear() requires the GIL,
// which a not-ready does not have, so we don't clear it.
// That means there may be leaks here until clearing the
// interpreter is fixed.
PyInterpreterState_Delete(interp);
return;
}
assert(whence != _PyInterpreterState_WHENCE_UNKNOWN);

PyThreadState *save_tstate = NULL;
PyThreadState *cur_tstate = PyThreadState_GET();
if (tstate == NULL) {
Expand All @@ -1893,18 +1907,7 @@ _PyXI_EndInterpreter(PyInterpreterState *interp,
}
}

long whence = _PyInterpreterState_GetWhence(interp);
assert(whence != _PyInterpreterState_WHENCE_RUNTIME);
if (whence == _PyInterpreterState_WHENCE_UNKNOWN) {
assert(!_PyInterpreterState_IsReady(interp));
PyThreadState *tstate = PyThreadState_New(interp);
save_tstate = PyThreadState_Swap(tstate);
_PyInterpreterState_Clear(tstate);
PyInterpreterState_Delete(interp);
}
else {
Py_EndInterpreter(tstate);
}
Py_EndInterpreter(tstate);

if (p_save_tstate != NULL) {
save_tstate = *p_save_tstate;
Expand Down
0