Skip to content

Commit 6e761d7

Browse files
committed
workaround main sub interp crashes
1 parent e78cecc commit 6e761d7

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Modules/_datetimemodule.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -7130,14 +7130,19 @@ callback_for_interp_exit(void *Py_UNUSED(data))
71307130

71317131
assert(_globals.interp_count > 0);
71327132
PyMutex_Lock(&_globals.mutex);
7133-
int64_t final = !_globals.interp_count;
71347133
_globals.interp_count -= 1;
7134+
int final = !_globals.interp_count;
71357135
PyMutex_Unlock(&_globals.mutex);
71367136

7137+
/* Finish from parent to child */
71377138
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
71387139
PyTypeObject *type = capi_types[i];
71397140
_PyStaticType_FiniForExtension(interp, type, final);
71407141
}
7142+
/* Clear indexes all at once */
7143+
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
7144+
capi_types[i]->tp_subclasses = NULL;;
7145+
}
71417146
}
71427147

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

7161+
/* Init from parent to child */
71567162
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
71577163
PyTypeObject *type = capi_types[i];
71587164
if (_PyStaticType_InitForExtension(interp, type) < 0) {

Objects/typeobject.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
252252
state->type = NULL;
253253
assert(state->tp_weaklist == NULL); // It was already cleared out.
254254

255-
if (final) {
255+
if (final && isbuiltin) {
256256
managed_static_type_index_clear(self);
257257
}
258258

@@ -400,7 +400,9 @@ set_tp_bases(PyTypeObject *self, PyObject *bases)
400400
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
401401
// XXX tp_bases can probably be statically allocated for each
402402
// static builtin type.
403-
assert(_Py_IsMainInterpreter(_PyInterpreterState_GET()));
403+
PyInterpreterState *interp = _PyInterpreterState_GET();
404+
managed_static_type_state *state = managed_static_type_state_get(interp, self);
405+
assert(!state->isbuiltin || _Py_IsMainInterpreter(interp));
404406
assert(self->tp_bases == NULL);
405407
if (PyTuple_GET_SIZE(bases) == 0) {
406408
assert(self->tp_base == NULL);
@@ -473,7 +475,9 @@ set_tp_mro(PyTypeObject *self, PyObject *mro)
473475
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
474476
// XXX tp_mro can probably be statically allocated for each
475477
// static builtin type.
476-
assert(_Py_IsMainInterpreter(_PyInterpreterState_GET()));
478+
PyInterpreterState *interp = _PyInterpreterState_GET();
479+
managed_static_type_state *state = managed_static_type_state_get(interp, self);
480+
assert(!state->isbuiltin || _Py_IsMainInterpreter(interp));
477481
assert(self->tp_mro == NULL);
478482
/* Other checks are done via set_tp_bases. */
479483
_Py_SetImmortal(mro);
@@ -7832,7 +7836,9 @@ static int
78327836
type_ready_set_bases(PyTypeObject *type)
78337837
{
78347838
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
7835-
if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) {
7839+
PyInterpreterState *interp = _PyInterpreterState_GET();
7840+
managed_static_type_state *state = managed_static_type_state_get(interp, type);
7841+
if (state->isbuiltin && !_Py_IsMainInterpreter(interp)) {
78367842
assert(lookup_tp_bases(type) != NULL);
78377843
return 0;
78387844
}
@@ -7966,7 +7972,9 @@ type_ready_mro(PyTypeObject *type)
79667972
ASSERT_TYPE_LOCK_HELD();
79677973

79687974
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
7969-
if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) {
7975+
PyInterpreterState *interp = _PyInterpreterState_GET();
7976+
managed_static_type_state *state = managed_static_type_state_get(interp, type);
7977+
if (state->isbuiltin && !_Py_IsMainInterpreter(interp)) {
79707978
assert(lookup_tp_mro(type) != NULL);
79717979
return 0;
79727980
}

0 commit comments

Comments
 (0)