Skip to content

Deletion of autoTSSkey during runtime finalization is not safe #131185

Closed

Description

@colesbury

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.

cpython/Python/pystate.c

Lines 486 to 501 in e9d210b

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

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-bugAn unexpected behavior, bug, or error
    on Mar 13, 2025
  2. ZeroIntensity commented on Mar 13, 2025

    @ZeroIntensity
    Member

    Making it a thread-local sounds reasonable, but I think we also need to deal with NULL auto interpreter states in order to get PyGILState_Ensure fully working (as in, hanging the thread) post-finalization.

    My other concern (which might be an issue regardless of autoTSSkey) is what happens to the thread state when the thread is hung. Does it just get leaked?

  3. colesbury commented on Mar 14, 2025

    @colesbury
    ContributorAuthor

    ...what happens to the thread state when the thread is hung. Does it just get leaked?

    No, the thread state gets deleted:

    decref_threadstate(tstate_impl);

    But I'm not really sure it matters because:

    1. The process is about to exit, so anything you "leak" is going to disappear soon
    2. The native thread will still be leaked
    3. Any Python objects referenced by the thread's stack will be leaked. This is true regardless of whether we kill the thread via pthread_exit() or block it via PyThread_hang_thread().
  4. ZeroIntensity commented on Mar 14, 2025

    @ZeroIntensity
    Member

    The process is about to exit, so anything you "leak" is going to disappear soon

    I'm thinking about embedders that don't necessarily exit after Py_Finalize, but yeah, I don't think it matters much either. I would assume that most embedders keep the runtime alive for the duration of their app as well.

  5. added a commit that references this issue on May 21, 2025
  6. ericsnowcurrently commented on May 21, 2025

    @ericsnowcurrently
    Member

    The broader issue of unprotected runtime state at finalization is handled in gh-134307.

  7. ericsnowcurrently commented on May 21, 2025

    @ericsnowcurrently
    Member

    Thanks for tackling this, @ZeroIntensity!

  8. added a commit that references this issue on May 26, 2025
  9. added a commit that references this issue on Jul 12, 2025
  10. added a commit that references this issue on Aug 4, 2025
  11. added a commit that references this issue on Dec 2, 2025
  12. added a commit that references this issue on Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions