Skip to content

BUG: segfault in PyArray_Repeat on free-threaded build #28197

@ngoldbaum

Description

@ngoldbaum

This script will sometimes segfault on the free-threaded build:

import functools
import numpy as np
import threading

def somersd(x, y=None, alternative='two-sided'):
    return 

x0 = np.arange(10)
x1 = np.arange(10)
partialfunc = functools.partial(somersd, alternative='two-sided')

n_workers = 10
n_iterations = 100
barrier = threading.Barrier(n_workers)

def closure(*args, **kwargs):
    barrier.wait()
    for _ in range(n_iterations):
        x = np.repeat(x0, 2, axis=0)[::2]
        y = np.repeat(x1, 2, axis=0)[::2]
        partialfunc(x, y)

workers = []
for _ in range(0, n_workers):
    worker_kwargs = {}
    workers.append(
                threading.Thread(target=closure, args=tuple(), kwargs=worker_kwargs)
                    )

for worker in workers:
    worker.start()

for worker in workers:
    worker.join()
python3.13(7926,0x171eff000) malloc: Incorrect checksum for freed object 0x15482d400: probably modified after being freed.
Corrupt value: 0x0
python3.13(7926,0x171eff000) malloc: *** set a breakpoint in malloc_error_break to debug

lldb gives me the following C backtrace for one of the threads that segfaulted:

  * frame #0: 0x0000000100d91864 _multiarray_umath.cpython-313t-darwin.so`PyArray_Repeat at item_selection.c:796:21 [opt]
    frame #1: 0x0000000100d91810 _multiarray_umath.cpython-313t-darwin.so`PyArray_Repeat [inlined] npy_fastrepeat(n_outer=1, n=<unavailable>, nel=1, chunk=8, broadcast=<unavailable>, counts=0x0000600002dd08c0, new_data=<unavailable>, old_data=<unavailable>, elsize=8, cast_info=0x0000000170e06428, needs_custom_copy=0) at item_selection.c:842:20 [opt]
    frame #2: 0x0000000100d91740 _multiarray_umath.cpython-313t-darwin.so`PyArray_Repeat(aop=<unavailable>, op=<unavailable>, axis=<unavailable>) at item_selection.c:963:9 [opt]
    frame #3: 0x0000000100da652c _multiarray_umath.cpython-313t-darwin.so`array_repeat(self=0x0000053226ab8500, args=<unavailable>, kwds=<unavailable>) at methods.c:1224:44 [opt]
    frame #4: 0x000000010076b5e0 libpython3.13t.dylib`cfunction_call + 92
    frame #5: 0x000000010070772c libpython3.13t.dylib`_PyObject_Call + 244
    frame #6: 0x00000001008467d0 libpython3.13t.dylib`_PyEval_EvalFrameDefault + 13328
    frame #7: 0x000000010070756c libpython3.13t.dylib`PyObject_Vectorcall + 88
    frame #8: 0x0000000100d4fe90 _multiarray_umath.cpython-313t-darwin.so`dispatcher_vectorcall(self=0x0000053226a58570, args=<unavailable>, len_args=<unavailable>, kwnames=0x00000532260b9510) at arrayfunction_override.c:580:18 [opt]
    frame #9: 0x000000010070756c libpython3.13t.dylib`PyObject_Vectorcall + 88
    frame #10: 0x0000000100846f20 libpython3.13t.dylib`_PyEval_EvalFrameDefault + 15200
    frame #11: 0x000000010070a3e0 libpython3.13t.dylib`method_vectorcall + 328
    frame #12: 0x00000001009384b0 libpython3.13t.dylib`thread_run + 128
    frame #13: 0x00000001008ce538 libpython3.13t.dylib`pythread_wrapper + 28
    frame #14: 0x000000018d51c2e4 libsystem_pthread.dylib`_pthread_start + 136

Originally posted by @serge-sans-paille in #2271

Metadata

Metadata

Assignees

No one assigned

    Labels

    39 - free-threadingPRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions