Skip to content

gmpy2 backend is not thread-safe on the free-threaded build #974

Description

@ngoldbaum

See e.g. gmpy2/gmpy2#584.

If gmpy2 is installed, that means you can use the threading module to trivially segfault the interpreter on the free-threaded build using mpmath:

goldbaum at Nathans-MBP in ~/Documents/test
○  PYTHON_GIL=0 python test.py
nt
python(30978,0x16da33000) malloc: Heap corruption detected, free list is damaged at 0x6000020f4230
*** Incorrect guard value: 467503348
python(30978,0x16ca27000) malloc: Heap corruption detected, free list is damaged at 0x6000020f22e0
*** Incorrect guard value: 1235206128
python(30978,0x16da33000) malloc: *** set a breakpoint in malloc_error_break to debug
python(30978,0x16ea3f000) malloc: *** error for object 0x6000020f2290: pointer being freed was not allocated
python(30978,0x16ea3f000) malloc: *** set a breakpoint in malloc_error_break to debug
[3]    30978 abort      PYTHON_GIL=0 python test.py

goldbaum at Nathans-MBP in ~/Documents/test
○  cat test.py
from concurrent.futures import ThreadPoolExecutor
from mpmath import mp

def worker():
    mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2

tpe = ThreadPoolExecutor(max_workers=4)
futures = [None]*10_000


for i in range(10_000):
    futures[i] = tpe.submit(worker)

[f.result() for f in futures]

At least until gmpy2/gmpy2#584 is fixed, it might be a good idea to detect if the GIL is disabled and blocklist the gmpy2 backend in that case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorinvalidmark that a problem is no longer relevant

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions