Skip to content

Commit

Permalink
workaround main sub interp crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
neonene committed Jun 3, 2024
1 parent e78cecc commit 6e761d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 7 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7130,14 +7130,19 @@ callback_for_interp_exit(void *Py_UNUSED(data))

assert(_globals.interp_count > 0);
PyMutex_Lock(&_globals.mutex);
int64_t final = !_globals.interp_count;
_globals.interp_count -= 1;
int final = !_globals.interp_count;
PyMutex_Unlock(&_globals.mutex);

/* Finish from parent to child */
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
PyTypeObject *type = capi_types[i];
_PyStaticType_FiniForExtension(interp, type, final);
}
/* Clear indexes all at once */
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
capi_types[i]->tp_subclasses = NULL;;
}
}

static int
Expand All @@ -7153,6 +7158,7 @@ init_static_types(PyInterpreterState *interp, int reloading)
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;

/* Init from parent to child */
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
PyTypeObject *type = capi_types[i];
if (_PyStaticType_InitForExtension(interp, type) < 0) {
Expand Down
18 changes: 13 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
state->type = NULL;
assert(state->tp_weaklist == NULL); // It was already cleared out.

if (final) {
if (final && isbuiltin) {
managed_static_type_index_clear(self);
}

Expand Down Expand Up @@ -400,7 +400,9 @@ set_tp_bases(PyTypeObject *self, PyObject *bases)
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
// XXX tp_bases can probably be statically allocated for each
// static builtin type.
assert(_Py_IsMainInterpreter(_PyInterpreterState_GET()));
PyInterpreterState *interp = _PyInterpreterState_GET();
managed_static_type_state *state = managed_static_type_state_get(interp, self);
assert(!state->isbuiltin || _Py_IsMainInterpreter(interp));
assert(self->tp_bases == NULL);
if (PyTuple_GET_SIZE(bases) == 0) {
assert(self->tp_base == NULL);
Expand Down Expand Up @@ -473,7 +475,9 @@ set_tp_mro(PyTypeObject *self, PyObject *mro)
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
// XXX tp_mro can probably be statically allocated for each
// static builtin type.
assert(_Py_IsMainInterpreter(_PyInterpreterState_GET()));
PyInterpreterState *interp = _PyInterpreterState_GET();
managed_static_type_state *state = managed_static_type_state_get(interp, self);
assert(!state->isbuiltin || _Py_IsMainInterpreter(interp));
assert(self->tp_mro == NULL);
/* Other checks are done via set_tp_bases. */
_Py_SetImmortal(mro);
Expand Down Expand Up @@ -7832,7 +7836,9 @@ static int
type_ready_set_bases(PyTypeObject *type)
{
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) {
PyInterpreterState *interp = _PyInterpreterState_GET();
managed_static_type_state *state = managed_static_type_state_get(interp, type);
if (state->isbuiltin && !_Py_IsMainInterpreter(interp)) {
assert(lookup_tp_bases(type) != NULL);
return 0;
}
Expand Down Expand Up @@ -7966,7 +7972,9 @@ type_ready_mro(PyTypeObject *type)
ASSERT_TYPE_LOCK_HELD();

if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) {
PyInterpreterState *interp = _PyInterpreterState_GET();
managed_static_type_state *state = managed_static_type_state_get(interp, type);
if (state->isbuiltin && !_Py_IsMainInterpreter(interp)) {
assert(lookup_tp_mro(type) != NULL);
return 0;
}
Expand Down

0 comments on commit 6e761d7

Please sign in to comment.