Repository navigation
PyThreadState_Next not thread safe #40857
Description
Activity
I can't convince myself that PyThreadState_Next is
thread safe even if the GIL is held because
PyGILState_Release can delete a thread state after
releasing the GIL if the gilstate_counter reaches 0.
So another thread could be deleting a thread state my
thread is referencing.If the GIL was held when the thread state was deleted,
the I think the next function would be thread safe
because new thread states are always added to the front
of the list.If these functions should work when the GIL is not
held, the head_mutex in pystate.c probably needs to be
exposed in some manner so code can lock it, iterate
through the threads, and then unlock it.- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Sep 2, 2004 Can someone take a look a this and either close or propose what to do?
No reply to msg82072.
This is actually true and it's quite easy to produce a bug that triggers a segmentation fault when iterating over a large list of threads create dynamically.
That makes PyThreadState_Next barely usable for any program that has a few threads. The head mutex needs to be locked to be able to iterate safely over the list of thread.
- added3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life
on Dec 10, 2019 - changed the title
[-]PyThreadState_Next not thread safe?[/-][+]PyThreadState_Next not thread safe[/+]on Dec 10, 2019 - added3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life
on Dec 10, 2019 10 remaining items
We'd simply need to provide a new API to lock/unlock the interpreter's mutex.
Are you talking about this lock?
#define HEAD_LOCK(runtime) \ PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK) #define HEAD_UNLOCK(runtime) \ PyThread_release_lock((runtime)->interpreters.mutex)
Yes, that's the one 👍
Victor, do you have an idea of how you would like this mutex to be exposed?
I see it only recently changed in Python 3.7 to be part of a structure, though this structure is not public AFAIU.
Is adding 2 new lock/unlock C functions good enough?
I'm not sure of what is the best approach. Currently, bpo-10915 is blocking different projects: "Make the PyGILState API compatible with multiple interpreters". We may have to fix this API first, and clarify the scope of the different locks.
On Wed, Dec 11, 2019 at 7:02 AM STINNER Victor <report@bugs.python.org> wrote:
We may have to fix this API first, and clarify the scope of the different locks.
+1
See also bpo-35370: "Add _PyEval_SetTrace(tstate, func, arg) function".
I marked bpo-26461 as a duplicate of this issue.
- added3.12only security fixesonly security fixesand removed3.9 (EOL)end of lifeend of life
on Jun 15, 2022 Ping. Is this issue still relevant?
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: