Deletion of autoTSSkey during runtime finalization is not safe #131185
Copy link
Copy link
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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.
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:
- Convert
autoTssKeyto a_Py_thread_locallike_Py_tss_tstate, which doesn't require deletion - Don't delete
autoTssKeyat runtime finalization
My preference is for the first option.
cc @ZeroIntensity @ericsnowcurrently @gpshead
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
ZeroIntensity commentedon Mar 13, 2025
Making it a thread-local sounds reasonable, but I think we also need to deal with
NULLauto interpreter states in order to getPyGILState_Ensurefully 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?colesbury commentedon Mar 14, 2025
No, the thread state gets deleted:
cpython/Python/pystate.c
Line 3057 in ca7fd81
But I'm not really sure it matters because:
pthread_exit()or block it viaPyThread_hang_thread().ZeroIntensity commentedon Mar 14, 2025
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.gh-131185: Use a proper thread-local for cached thread states (gh-132510
ericsnowcurrently commentedon May 21, 2025
The broader issue of unprotected runtime state at finalization is handled in gh-134307.
ericsnowcurrently commentedon May 21, 2025
Thanks for tackling this, @ZeroIntensity!
pythongh-131185: Use a proper thread-local for cached thread states (p…
pythongh-131185: Use a proper thread-local for cached thread states (p…
pythongh-131185: Use a proper thread-local for cached thread states (p…
pythongh-131185: Use a proper thread-local for cached thread states (p…
pythongh-131185: Use a proper thread-local for cached thread states (p…