summaryrefslogtreecommitdiffstats
path: root/Python/crossinterp.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/crossinterp.c')
-rw-r--r--Python/crossinterp.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/Python/crossinterp.c b/Python/crossinterp.c
index fb0dae0..367e29d 100644
--- a/Python/crossinterp.c
+++ b/Python/crossinterp.c
@@ -1822,7 +1822,7 @@ _PyXI_FiniTypes(PyInterpreterState *interp)
/*************/
PyInterpreterState *
-_PyXI_NewInterpreter(PyInterpreterConfig *config,
+_PyXI_NewInterpreter(PyInterpreterConfig *config, long *maybe_whence,
PyThreadState **p_tstate, PyThreadState **p_save_tstate)
{
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
@@ -1845,7 +1845,11 @@ _PyXI_NewInterpreter(PyInterpreterConfig *config,
assert(tstate != NULL);
PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);
- _PyInterpreterState_SetWhence(interp, _PyInterpreterState_WHENCE_XI);
+ long whence = _PyInterpreterState_WHENCE_XI;
+ if (maybe_whence != NULL) {
+ whence = *maybe_whence;
+ }
+ _PyInterpreterState_SetWhence(interp, whence);
if (p_tstate != NULL) {
// We leave the new thread state as the current one.
@@ -1868,6 +1872,22 @@ void
_PyXI_EndInterpreter(PyInterpreterState *interp,
PyThreadState *tstate, PyThreadState **p_save_tstate)
{
+#ifndef NDEBUG
+ long whence = _PyInterpreterState_GetWhence(interp);
+#endif
+ 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) {
@@ -1889,18 +1909,7 @@ _PyXI_EndInterpreter(PyInterpreterState *interp,
}
}
- long whence = _PyInterpreterState_GetWhence(interp);
- assert(whence != _PyInterpreterState_WHENCE_RUNTIME);
- if (whence == _PyInterpreterState_WHENCE_UNKNOWN) {
- assert(!interp->_ready);
- 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;