Skip to content

bpo-32604: Clean up created subinterpreters before runtime finalization. #5709

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
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
Next Next commit
Track an ID refcount on PyInterpreterState.
  • Loading branch information
ericsnowcurrently committed Feb 16, 2018
commit a764edba106e7566edeb79da2b5bd9222f062cdd
4 changes: 4 additions & 0 deletions Include/internal/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);

PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(PY_INT64_T);

PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);


/* cross-interpreter data */

Expand Down
4 changes: 4 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
extern "C" {
#endif

#include "pythread.h"

/* This limitation is for performance and simplicity. If needed it can be
removed (with effort). */
#define MAX_CO_EXTRA_USERS 255
Expand Down Expand Up @@ -111,6 +113,8 @@ typedef struct _is {
struct _ts *tstate_head;

int64_t id;
int64_t id_refcount;
PyThread_type_lock id_mutex;

PyObject *modules;
PyObject *modules_by_index;
Expand Down
58 changes: 57 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ PyInterpreterState_New(void)
return NULL;
}


interp->id_refcount = -1;
interp->id_mutex = NULL;
interp->modules = NULL;
interp->modules_by_index = NULL;
interp->sysdict = NULL;
Expand Down Expand Up @@ -247,6 +248,9 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
}
HEAD_UNLOCK();
if (interp->id_mutex != NULL) {
PyThread_free_lock(interp->id_mutex);
}
PyMem_RawFree(interp);
}

Expand Down Expand Up @@ -284,6 +288,58 @@ _PyInterpreterState_LookUpID(PY_INT64_T requested_id)
return NULL;
}


int
_PyInterpreterState_IDInitref(PyInterpreterState *interp)
{
if (interp->id_mutex != NULL) {
return 0;
}
interp->id_mutex = PyThread_allocate_lock();
if (interp->id_mutex == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"failed to create init interpreter ID mutex");
return -1;
}
interp->id_refcount = 0;
return 0;
}


void
_PyInterpreterState_IDIncref(PyInterpreterState *interp)
{
if (interp->id_mutex == NULL) {
return;
}
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
interp->id_refcount += 1;
PyThread_release_lock(interp->id_mutex);
}


void
_PyInterpreterState_IDDecref(PyInterpreterState *interp)
{
if (interp->id_mutex == NULL) {
return;
}
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
assert(interp->id_refcount != 0);
interp->id_refcount -= 1;
int64_t refcount = interp->id_refcount;
PyThread_release_lock(interp->id_mutex);

if (refcount == 0) {
PyThreadState *tstate, *save_tstate;
tstate = PyInterpreterState_ThreadHead(interp);
save_tstate = PyThreadState_Swap(tstate);
Py_EndInterpreter(tstate);
PyThreadState_Swap(save_tstate);
}
}


/* Default implementation for _PyThreadState_GetFrame */
static struct _frame *
threadstate_getframe(PyThreadState *self)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy