Skip to content

BUG: f2py produces wrapper code that are not threadsafe #16513

@sturlamolden

Description

@sturlamolden

f2py produces wrapper code that are not threadsafe and can segfault. See scipy/scipy#9658

Reproducing code example:

The segfault in the code below is caused by the f2py wrapper.

import concurrent.futures
import time
import scipy.optimize

def objective1(x):
    print(x, "1")
    time.sleep(0.1)
    return x[0]**2

def objective2(x):
    print(x, "2")
    time.sleep(0.1)
    return (x[0]-1)**2

min_method = "COBYLA"

def minimizer1():
    return scipy.optimize.minimize(objective1,
                                   [0.0],
                                   method=min_method)

def minimizer2():
    return scipy.optimize.minimize(objective2,
                                   [0.0],
                                   method=min_method)

with concurrent.futures.ThreadPoolExecutor() as pool:
    tasks = []
    tasks.append(pool.submit(minimizer1))
    tasks.append(pool.submit(minimizer2))

    for t in tasks:
        print(t.result())

Error message:

#0 0x00007f5e94d24dc0 in f2py_rout.cobyla_minimize () from /home/XXXX/lib/python3.7/site-packages/scipy/optimize/_cobyla.cpython-37m-x86_64-linux-gnu.so
#1 0x000055b6ce95146b in _PyObject_FastCallKeywords (callable=0x7f5e94d73670, stack=0x7f5e88001680, nargs=1, kwnames=0x7f5e94d74a08) at /tmp/build/80754af9/python_1546061345851/work/Objects/call.c:199
#2 0x000055b6ce9ac378 in call_function (kwnames=0x7f5e94d74a08, oparg=, pp_stack=) at /tmp/build/80754af9/python_1546061345851/work/Python/ceval.c:4619
[....]

Numpy/Python version information:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions