Skip to content

Commit 68da9da

Browse files
Fix _PyType_InitCache().
1 parent 355d1c1 commit 68da9da

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

Objects/typeobject.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ type_cache_clear(struct type_cache *cache, PyObject *value)
309309
void
310310
_PyType_InitCache(PyInterpreterState *interp)
311311
{
312-
int is_main = _Py_IsMainInterpreter(interp);
313312
struct type_cache *cache = &interp->types.type_cache;
314313
for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
315314
struct type_cache_entry *entry = &cache->hashtable[i];
@@ -318,21 +317,21 @@ _PyType_InitCache(PyInterpreterState *interp)
318317
entry->version = 0;
319318
// Set to None so _PyType_Lookup() can use Py_SETREF(),
320319
// rather than using slower Py_XSETREF().
320+
// (See _PyType_FixCacheRefcounts() about the refcount.)
321321
entry->name = Py_None;
322-
// Currently, this happens before the GIL has been created,
323-
// which is a problem without "immortality" (PEP 683).
324-
// Until we have immortal objects, we must avoid an INCREF
325-
// in the main interpreter here. Instead, we fix the refcount
326-
// in the main interpreer as soon as the GIL has been created.
327-
// (See pycore_create_interpreter() in pylifecycle.c.)
328-
if (!is_main) {
329-
Py_INCREF(Py_None);
330-
}
331322
entry->value = NULL;
332323
}
333324
}
334325

335-
// This is the temporary fix used by pycore_create_interpreter().
326+
// This is the temporary fix used by pycore_create_interpreter(),
327+
// in pylifecycle.c. _PyType_InitCache() is called before the GIL
328+
// has been created (for the main interpreter) and without the
329+
// "current" thread state set. This causes crashes when the
330+
// reftotal is updated, so we don't modify the refcount in
331+
// _PyType_InitCache(), and instead do it later by calling
332+
// _PyType_FixCacheRefcounts().
333+
// XXX This workaround should be removed once we have immortal
334+
// objects (PEP 683).
336335
void
337336
_PyType_FixCacheRefcounts(void)
338337
{

Python/pylifecycle.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,6 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
635635
return status;
636636
}
637637

638-
// This is a temporary fix until we have immortal objects.
639-
// (See _PyType_InitCache() in typeobject.c.)
640-
extern void _PyType_FixCacheRefcounts(void);
641-
_PyType_FixCacheRefcounts();
642-
643638
*tstate_p = tstate;
644639
return _PyStatus_OK();
645640
}
@@ -807,6 +802,11 @@ pycore_interp_init(PyThreadState *tstate)
807802
PyStatus status;
808803
PyObject *sysmod = NULL;
809804

805+
// This is a temporary fix until we have immortal objects.
806+
// (See _PyType_InitCache() in typeobject.c.)
807+
extern void _PyType_FixCacheRefcounts(void);
808+
_PyType_FixCacheRefcounts();
809+
810810
// Create singletons before the first PyType_Ready() call, since
811811
// PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
812812
// and the empty tuple singletons (tp_bases).

0 commit comments

Comments
 (0)