Is it possible to deadlock a cython.pymutex by taking the GIL?
19:03 22 Nov 2026

Cython has a built-in lock, called cython.pymutex. This lock is implemented using PyThread_type_lock() in Python 3.12, and PyMutex in Python 3.13+.

The Cython documentation warns against acquiring the GIL while holding a pymutex.

acquire will avoid deadlocks if the GIL is held (only relevant in non-freethreading versions of Python). However, you are at risk of deadlock if you attempt to acquire the GIL while holding a cython.pymutex lock. Be aware that it is also possible for Cython to acquire the GIL implicitly (for example by raising an exception) and this is also a deadlock risk.

I read this to mean that you are safe from deadlocks if you either

  1. mark your function nogil and noexcept, or
  2. acquire the GIL before you enter a pymutex locked section.

But there is a third case: it is verboten to acquire the pymutex, then acquire the GIL, as this creates a risk of deadlocks.

I can see a problem if there were two lock orderings:

  1. Acquire pymutex A, then acquire GIL.
  2. Acquire GIL, then acquire pymutex A.

This seems like it would deadlock. But it actually doesn't: the code for #2 will repeatedly release the GIL, retry the lock, and require the GIL. So it will never wait forever for the pymutex without releasing the GIL, so there is no lock ordering cycle in the lock ordering graph. I believe this is what the documentation is referring to in the passage, when it says that "acquire will avoid deadlocks if the GIL is held."

It seems that because the Cython developers have defused lock ordering #2, they have also defused lock ordering #1. But the Cython developers do not think so. In the PR adding the warning above, they say:

Acquiring a lock while holding the GIL shouldn't be able to deadlock (although the inverse isn't true).

Source.

However, I have not been able to construct an example of a Cython program which deadlocks as a result of acquiring the GIL inside a pymutex locked section, despite many attempts across Python 3.10-3.13, with and without freethreading. Here is one such attempt:

# Cython file, named funcs.pyx
cimport cython
cdef cython.pymutex l
# incrementing this Python int requires holding the GIL
# on non ft builds of Python
python_interaction = 0

def gil_in_lock():
    _gil_in_lock()


cdef int _gil_in_lock() noexcept nogil:
    global python_interaction
    # Take l, then take gil.
    with l:
        with gil:
            python_interaction += 1
    return 0

def lock_in_gil():
    global python_interaction
    # We begin with the gil (function is not marked nogil)
    # Then we take l.
    with l:
        python_interaction += 1
# Python driver for funcs.pyx
import threading
from funcs import gil_in_lock, lock_in_gil

N = 1000000

def run(fn):
    for _ in range(N):
        fn()

threads = [threading.Thread(target=run, args=(f,))
           for f in ([gil_in_lock] * 2 + [lock_in_gil] * 2)]

for t in threads:
    t.start()
for t in threads:
    t.join()
print("Success! no deadlock")

Is it impossible to deadlock Python in the manner the documentation warns against? If so, why? If not, can you give an example of a program that deadlocks?

python multithreading cython