summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-04-24 23:23:57 (GMT)
committerGitHub <noreply@github.com>2023-04-24 23:23:57 (GMT)
commitdf3173d28ef25a0f97d2cca8cf4e64e062a08d06 (patch)
treef2b6f378f81ceee48a9e710154b9d6c4b0f959a2 /Python/pylifecycle.c
parent01be52e42eac468b6511b56ee60cd1b99baf3848 (diff)
downloadcpython-df3173d28ef25a0f97d2cca8cf4e64e062a08d06.zip
cpython-df3173d28ef25a0f97d2cca8cf4e64e062a08d06.tar.gz
cpython-df3173d28ef25a0f97d2cca8cf4e64e062a08d06.tar.bz2
gh-101659: Isolate "obmalloc" State to Each Interpreter (gh-101660)
This is strictly about moving the "obmalloc" runtime state from `_PyRuntimeState` to `PyInterpreterState`. Doing so improves isolation between interpreters, specifically most of the memory (incl. objects) allocated for each interpreter's use. This is important for a per-interpreter GIL, but such isolation is valuable even without it. FWIW, a per-interpreter obmalloc is the proverbial canary-in-the-coalmine when it comes to the isolation of objects between interpreters. Any object that leaks (unintentionally) to another interpreter is highly likely to cause a crash (on debug builds at least). That's a useful thing to know, relative to interpreter isolation.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index a510c9b..ebf1a0b 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -547,11 +547,21 @@ pycore_init_runtime(_PyRuntimeState *runtime,
}
-static void
+static PyStatus
init_interp_settings(PyInterpreterState *interp, const _PyInterpreterConfig *config)
{
assert(interp->feature_flags == 0);
+ if (config->use_main_obmalloc) {
+ interp->feature_flags |= Py_RTFLAGS_USE_MAIN_OBMALLOC;
+ }
+ else if (!config->check_multi_interp_extensions) {
+ /* The reason: PyModuleDef.m_base.m_copy leaks objects between
+ interpreters. */
+ return _PyStatus_ERR("per-interpreter obmalloc does not support "
+ "single-phase init extension modules");
+ }
+
if (config->allow_fork) {
interp->feature_flags |= Py_RTFLAGS_FORK;
}
@@ -570,6 +580,8 @@ init_interp_settings(PyInterpreterState *interp, const _PyInterpreterConfig *con
if (config->check_multi_interp_extensions) {
interp->feature_flags |= Py_RTFLAGS_MULTI_INTERP_EXTENSIONS;
}
+
+ return _PyStatus_OK();
}
@@ -622,7 +634,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
}
const _PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
- init_interp_settings(interp, &config);
+ status = init_interp_settings(interp, &config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
PyThreadState *tstate = _PyThreadState_New(interp);
if (tstate == NULL) {
@@ -1668,6 +1683,8 @@ finalize_interp_types(PyInterpreterState *interp)
_PyFloat_FiniType(interp);
_PyLong_FiniTypes(interp);
_PyThread_FiniType(interp);
+ // XXX fini collections module static types (_PyStaticType_Dealloc())
+ // XXX fini IO module static types (_PyStaticType_Dealloc())
_PyErr_FiniTypes(interp);
_PyTypes_FiniTypes(interp);
@@ -1936,6 +1953,7 @@ Py_FinalizeEx(void)
}
_Py_FinalizeRefTotal(runtime);
#endif
+ _Py_FinalizeAllocatedBlocks(runtime);
#ifdef Py_TRACE_REFS
/* Display addresses (& refcnts) of all objects still alive.
@@ -2036,7 +2054,10 @@ new_interpreter(PyThreadState **tstate_p, const _PyInterpreterConfig *config)
goto error;
}
- init_interp_settings(interp, config);
+ status = init_interp_settings(interp, config);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto error;
+ }
status = init_interp_create_gil(tstate);
if (_PyStatus_EXCEPTION(status)) {