gh-100227: Make the Global Interned Dict Safe for Isolated Interprete… · python/cpython@87be8d9 · GitHub
Skip to content

Commit 87be8d9

Browse files
gh-100227: Make the Global Interned Dict Safe for Isolated Interpreters (gh-102925)
This is effectively two changes. The first (the bulk of the change) is where we add _Py_AddToGlobalDict() (and _PyRuntime.cached_objects.main_tstate, etc.). The second (much smaller) change is where we update PyUnicode_InternInPlace() to use _Py_AddToGlobalDict() instead of calling PyDict_SetDefault() directly. Basically, _Py_AddToGlobalDict() is a wrapper around PyDict_SetDefault() that should be used whenever we need to add a value to a runtime-global dict object (in the few cases where we are leaving the container global rather than moving it to PyInterpreterState, e.g. the interned strings dict). _Py_AddToGlobalDict() does all the necessary work to make sure the target global dict is shared safely between isolated interpreters. This is especially important as we move the obmalloc state to each interpreter (gh-101660), as well as, potentially, the GIL (PEP 684). #100227
1 parent 8709697 commit 87be8d9

7 files changed

Lines changed: 204 additions & 30 deletions

File tree

Include/internal/pycore_global_objects.h

Lines changed: 4 additions & 0 deletions

Include/internal/pycore_pystate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ PyAPI_FUNC(void) _PyThreadState_Init(
127127
PyThreadState *tstate);
128128
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
129129

130+
extern void _PyThreadState_InitDetached(PyThreadState *, PyInterpreterState *);
131+
extern void _PyThreadState_ClearDetached(PyThreadState *);
132+
133+
extern PyObject * _Py_AddToGlobalDict(PyObject *, PyObject *, PyObject *);
134+
130135

131136
static inline void
132137
_PyThreadState_UpdateTracingState(PyThreadState *tstate)

Include/internal/pycore_runtime_init.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ extern PyTypeObject _PyExc_MemoryError;
5959
.types = { \
6060
.next_version_tag = 1, \
6161
}, \
62+
.cached_objects = { \
63+
.main_tstate = _PyThreadState_INIT, \
64+
}, \
6265
.static_objects = { \
6366
.singletons = { \
6467
.small_ints = _Py_small_ints_INIT, \

Include/internal/pycore_unicodeobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct _Py_unicode_runtime_ids {
3434

3535
struct _Py_unicode_runtime_state {
3636
struct _Py_unicode_runtime_ids ids;
37+
/* The interned dict is at _PyRuntime.cached_objects.interned_strings. */
3738
};
3839

3940
/* fs_codec.encoding is initialized to NULL.

Objects/unicodeobject.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14609,16 +14609,11 @@ PyUnicode_InternInPlace(PyObject **p)
1460914609
}
1461014610

1461114611
PyObject *interned = get_interned_dict();
14612-
assert(interned != NULL);
14613-
14614-
PyObject *t = PyDict_SetDefault(interned, s, s);
14615-
if (t == NULL) {
14616-
PyErr_Clear();
14617-
return;
14618-
}
14619-
14612+
PyObject *t = _Py_AddToGlobalDict(interned, s, s);
1462014613
if (t != s) {
14621-
Py_SETREF(*p, Py_NewRef(t));
14614+
if (t != NULL) {
14615+
Py_SETREF(*p, Py_NewRef(t));
14616+
}
1462214617
return;
1462314618
}
1462414619

Python/pylifecycle.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
636636
return status;
637637
}
638638

639+
_PyThreadState_InitDetached(&runtime->cached_objects.main_tstate, interp);
640+
639641
*tstate_p = tstate;
640642
return _PyStatus_OK();
641643
}
@@ -1932,6 +1934,8 @@ Py_FinalizeEx(void)
19321934
// XXX Do this sooner during finalization.
19331935
// XXX Ensure finalizer errors are handled properly.
19341936

1937+
_PyThreadState_ClearDetached(&runtime->cached_objects.main_tstate);
1938+
19351939
finalize_interp_clear(tstate);
19361940
finalize_interp_delete(tstate->interp);
19371941

Python/pystate.c

Lines changed: 183 additions & 21 deletions

0 commit comments

Comments
 (0)