Skip to content

gh-100227: Make the Global PyModuleDef Cache Safe for Isolated Interpreters #102938

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
546b934
Factor out add_threadstate().
ericsnowcurrently Mar 21, 2023
0bcd136
Add _PyThreadState_InitDetached().
ericsnowcurrently Mar 21, 2023
35d5310
Add _PyThreadState_ClearDetached().
ericsnowcurrently Mar 21, 2023
cba9e34
Add _PyRuntime.cached_objects.main_tstate.
ericsnowcurrently Mar 21, 2023
3b1bb8b
Add _Py_AcquireGlobalObjectsState() and _Py_ReleaseGlobalObjectsState().
ericsnowcurrently Mar 22, 2023
eb42aa1
Add _Py_AddToGlobalDict().
ericsnowcurrently Mar 22, 2023
4e9da2d
Drop _Py_AcquireGlobalObjectsState() and _Py_ReleaseGlobalObjectsStat…
ericsnowcurrently Mar 22, 2023
6216207
Add acquire_global_objects_lock() and release_global_objects_lock().
ericsnowcurrently Mar 22, 2023
3c007c0
Add some TODO comments.
ericsnowcurrently Mar 13, 2023
7d95514
Factor out store_interned().
ericsnowcurrently Mar 20, 2023
5c20b84
Store a thread state to use just for interned strings.
ericsnowcurrently Mar 20, 2023
a3ae02a
Always use the main interpreter when possibly resizing the interned d…
ericsnowcurrently Mar 20, 2023
d5fbc37
Use _PyRuntime.cached_objects.main_tstate instead.
ericsnowcurrently Mar 21, 2023
459325f
Add _PyThreadState_IsBound() and _PyThreadState_Unbind().
ericsnowcurrently Mar 21, 2023
4f25244
Make sure the one-off tstate is bound before using it.
ericsnowcurrently Mar 21, 2023
e68535a
Use _Py_AcquireGlobalObjectsState() in store_interned().
ericsnowcurrently Mar 22, 2023
22753b3
Use _Py_AddToGlobalDict().
ericsnowcurrently Mar 22, 2023
5c7bfd7
Move the extensions dict to _PyRuntime.cached_objects.
ericsnowcurrently Mar 22, 2023
f1a33ce
Add _Py_GetFromGlobalDict().
ericsnowcurrently Mar 22, 2023
10e6d69
Add _Py_PopFromGlobalDict().
ericsnowcurrently Mar 22, 2023
7dbae68
Adjust _Py_AddToGlobalDict().
ericsnowcurrently Mar 22, 2023
3e08c1f
Use _Py_PopFromGlobalDict() in unicode_dealloc().
ericsnowcurrently Mar 22, 2023
1039a60
Use the global dict API in import.c.
ericsnowcurrently Mar 22, 2023
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
14 changes: 14 additions & 0 deletions Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@ extern "C" {
_PyRuntime.cached_objects.NAME

struct _Py_cached_objects {
/* A thread state tied to the main interpreter,
used exclusively for when a global object (e.g. interned strings)
is resized (i.e. deallocated + allocated) from an arbitrary thread. */
PyThreadState main_tstate;

/* The dict of interned strings. */
PyObject *interned_strings;

/* A dict mapping (filename, name) to PyModuleDef for modules.
Only legacy (single-phase init) extension modules are added
and only if they support multiple initialization (m_size >- 0)
or are imported in the main interpreter.
This is initialized lazily in _PyImport_FixupExtensionObject().
Modules are added there and looked up in _imp.find_extension(). */
PyObject *extensions;
};

#define _Py_GLOBAL_OBJECT(NAME) \
Expand Down
9 changes: 2 additions & 7 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ struct _import_runtime_state {
which is just about every time an extension module is imported.
See PyInterpreterState.modules_by_index for more info. */
Py_ssize_t last_module_index;
/* A dict mapping (filename, name) to PyModuleDef for modules.
Only legacy (single-phase init) extension modules are added
and only if they support multiple initialization (m_size >- 0)
or are imported in the main interpreter.
This is initialized lazily in _PyImport_FixupExtensionObject().
Modules are added there and looked up in _imp.find_extension(). */
PyObject *extensions;
/* Package context -- the full module name for package imports */
const char * pkgcontext;
/* The dict of cached module defs is over at
_PyRuntime.cached_objects.extensions. */
};

struct _import_state {
Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ PyAPI_FUNC(void) _PyThreadState_Init(
PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);

extern void _PyThreadState_InitDetached(PyThreadState *, PyInterpreterState *);
extern void _PyThreadState_ClearDetached(PyThreadState *);

extern PyObject * _Py_GetFromGlobalDict(PyObject *, PyObject *);
extern PyObject * _Py_AddToGlobalDict(PyObject *, PyObject *, PyObject *);
extern PyObject * _Py_PopFromGlobalDict(PyObject *, PyObject *);


static inline void
_PyThreadState_UpdateTracingState(PyThreadState *tstate)
Expand Down
3 changes: 3 additions & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ extern PyTypeObject _PyExc_MemoryError;
.types = { \
.next_version_tag = 1, \
}, \
.cached_objects = { \
.main_tstate = _PyThreadState_INIT, \
}, \
.static_objects = { \
.singletons = { \
.small_ints = _Py_small_ints_INIT, \
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct _Py_unicode_runtime_ids {

struct _Py_unicode_runtime_state {
struct _Py_unicode_runtime_ids ids;
/* The interned dict is at _PyRuntime.cached_objects.interned_strings. */
};

/* fs_codec.encoding is initialized to NULL.
Expand Down
16 changes: 6 additions & 10 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,8 @@ unicode_dealloc(PyObject *unicode)
PyDict_DelItem(). */
assert(Py_REFCNT(unicode) == 0);
Py_SET_REFCNT(unicode, 3);
if (PyDict_DelItem(interned, unicode) != 0) {
if (_Py_PopFromGlobalDict(interned, unicode) == NULL
&& PyErr_Occurred()) {
_PyErr_WriteUnraisableMsg("deletion of interned string failed",
NULL);
}
Expand Down Expand Up @@ -14609,16 +14610,11 @@ PyUnicode_InternInPlace(PyObject **p)
}

PyObject *interned = get_interned_dict();
assert(interned != NULL);

PyObject *t = PyDict_SetDefault(interned, s, s);
if (t == NULL) {
PyErr_Clear();
return;
}

PyObject *t = _Py_AddToGlobalDict(interned, s, s);
if (t != s) {
Py_SETREF(*p, Py_NewRef(t));
if (t != NULL) {
Py_SETREF(*p, Py_NewRef(t));
}
return;
}

Expand Down
25 changes: 14 additions & 11 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static struct _inittab *inittab_copy = NULL;

#define INITTAB _PyRuntime.imports.inittab
#define LAST_MODULE_INDEX _PyRuntime.imports.last_module_index
#define EXTENSIONS _PyRuntime.imports.extensions
#define EXTENSIONS _Py_CACHED_OBJECT(extensions)

#define PKGCONTEXT (_PyRuntime.imports.pkgcontext)

Expand Down Expand Up @@ -889,7 +889,7 @@ _extensions_cache_get(PyObject *filename, PyObject *name)
if (key == NULL) {
return NULL;
}
PyModuleDef *def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key);
PyModuleDef *def = (PyModuleDef *)_Py_GetFromGlobalDict(extensions, key);
Py_DECREF(key);
return def;
}
Expand All @@ -909,9 +909,15 @@ _extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def)
if (key == NULL) {
return -1;
}
int res = PyDict_SetItem(extensions, key, (PyObject *)def);
PyObject *existing = _Py_AddToGlobalDict(extensions, key, (PyObject *)def);
Py_DECREF(key);
if (res < 0) {
if (existing == NULL) {
return -1;
}
if (existing != (PyObject *)def) {
assert(!PyErr_Occurred());
PyErr_Format(PyExc_ImportError,
"could not add moduledef for %s to the cache", name);
return -1;
}
return 0;
Expand All @@ -928,14 +934,11 @@ _extensions_cache_delete(PyObject *filename, PyObject *name)
if (key == NULL) {
return -1;
}
if (PyDict_DelItem(extensions, key) < 0) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
Py_DECREF(key);
return -1;
}
PyErr_Clear();
}
PyObject *value = _Py_PopFromGlobalDict(extensions, key);
Py_DECREF(key);
if (value == NULL && PyErr_Occurred()) {
return -1;
}
return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return status;
}

_PyThreadState_InitDetached(&runtime->cached_objects.main_tstate, interp);

*tstate_p = tstate;
return _PyStatus_OK();
}
Expand Down Expand Up @@ -1928,6 +1930,8 @@ Py_FinalizeEx(void)
// XXX Do this sooner during finalization.
// XXX Ensure finalizer errors are handled properly.

_PyThreadState_ClearDetached(&runtime->cached_objects.main_tstate);

finalize_interp_clear(tstate);
finalize_interp_delete(tstate->interp);

Expand Down
Loading