Repository navigation
Add HEAD_ISLOCKED to wrap GIL check #125908
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Oct 24, 2024 - addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Oct 24, 2024 Eh, this seems like the wrong approach. I would be more comfortable with either making the mutex recursive, rather than implicitly releasing it for cases like this.
I wonder whether it's time to change this lock to recursive lock, we have had issues about deadlocks where re-entrant calls can deadlock, ex Py_DECREF.
Maybe we can wait for a solution to the problem.
Maybe we can wait for a solution to the problem.
The solution to that problem is to make it a recursive mutex :)
Reacted by Bénédikt TranI don't think we want to use a recursive mutex for the linked list of thread states (or interpreter states). That pattern is still prone to lock ordering deadlocks with other threads because
PyThreadState_Clear()can call arbitrary code via destructors.(I also think that releasing the mutex implicitly is not the right strategy either)
Reacted by Peter BiermaIs it necessary to introduce the macro I mentioned for safely unlocking HEAD_LOCK? It can safely release the lock and eliminate errors caused by releasing when the context is not held.
I'll defer to Sam, as he knows much more about locking than I do, but I'm -1 on this, because it's generally a bad idea to implicitly release locks.
In fact, the current change solves the competition, but it does not solve the problem of probability deadlock.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Feature or enhancement
Proposal:
This issue is extracted from #125561.
When interpretation_clear is changed, the
PyThreadState_Clearfunction needs to be called whenHEAD_LOCKis not held, but in fact we cannot be sure, but it existed before the change. In fact, PR solves the competition, but does not eliminate the risk of deadlock, so I give the following solution.For
PyThreadState_Clear, I want to add aHEAD_ISLOCKEDmacro to wrapPyMutex_IsLocked. This allows a check to be made when UNLOCK is called. Otherwise, if HEAD_LOCK is not called in the context, an is unlock error will occur when HEAD_UNLOCK is called. But in fact, we cannot be sure whether HEAD_LOCK is called in the context, and HEAD_IS_LOCK can be used to check it.changed
The above code will show no locked when the context does not hold HEAD_LOCK, but this is not certain. So I want to add an if statement in front to unlock it when holding HEAD_LOCK. Ensure that there is no deadlock when calling
PyThreadState_Clear.Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response