colesbury · GitHub

Bug report

The autoTssKey is deleted during _PyRuntimeState_Fini by gilstate_tss_fini. This isn't safe because other threads may try calling PyGILState_Ensure() or PyGILState_GetThisThreadState() concurrently during shutdown.

void
_PyRuntimeState_Fini(_PyRuntimeState *runtime)
{
#ifdef Py_REF_DEBUG
/* The count is cleared by _Py_FinalizeRefTotal(). */
assert(runtime->object_state.interpreter_leaks == 0);
#endif
if (gilstate_tss_initialized(runtime)) {
gilstate_tss_fini(runtime);
}
if (PyThread_tss_is_created(&runtime->trashTSSkey)) {
PyThread_tss_delete(&runtime->trashTSSkey);
}
}

We can:

  1. Convert autoTssKey to a _Py_thread_local like _Py_tss_tstate, which doesn't require deletion
  2. Don't delete autoTssKey at runtime finalization

My preference is for the first option.

cc @ZeroIntensity @ericsnowcurrently @gpshead

Linked PRs

Read the original on github.com ↗