Skip to content

Deadlock caused by type_lock_prevent_release() #155400

Description

@nascheme

Bug report

Bug description:

The current implementation of type_lock_prevent_release() has a flaw. If the critical section holds both TYPE_LOCK and the type dict mutex (this happens with BEGIN_TYPE_DICT_LOCK), then we can end up deadlocking. This is because only the type lock is prevented from being released and the dict mutex is released. This results in a lock inversion.

The suggested fix is to prevent release of both held by the top critical section.

Reproducer (note it depends on the memory addresses of the mutexes so not 100% reliable).

deftest_concurrent_setattr_deadlock():
# two threads assigning to a special method of the same# class could deadlock. One thread held the type lock and waited for# the type dict mutex, which its critical section had released when it# blocked on the stop-the-world mutex, while the other held the type# dict mutex and waited for the type lock.classBase:
passN=2000done=Falsedefsetter():
func=lambdaself: "x"whilenotdone:
Base.__repr__=functry:
delBase.__repr__exceptAttributeError:
passdefsubclasser():
whilenotdone:
type('Sub', (Base,), {})()
deflister():
whilenotdone:
Base.__subclasses__()
defbasesetter():
nonlocaldoneforiinrange(N):
ifi%100==0:
print(i)
classA:
passclassC:
passclassB(A):
passB.__bases__= (C,)
done=True# The setter threads are the ones that deadlock. The others are there# to keep the type lock and the stop-the-world mutex contended, which# is what gets the setters into the window where it happens.targets= (
setter,
setter,
subclasser,
subclasser,
lister,
lister,
basesetter,
)
threads= [Thread(target=target) fortargetintargets]
fortinthreads:
t.start()
fortinthreads:
t.join()

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions