Skip to content

BUG: scipy.special.stdtrit is not thread-safe for df.size > 500 #18678

@hallmeier

Description

@hallmeier

I encountered a very weird issue, but I can reproduce it reliably. I encountered it first on a virtual Ubuntu machine and can reproduce it on my AArch64 Mac, so I am hopeful you will be able to reproduce it as well. I could not build scipy from source, but the issue is the same from versions 1.9.2 to 1.11.0rc on Python 3.11. It is reproducible as follows:

import numpy as np
import scipy.special
import threading

q = 0.95
df = np.arange(5, 506)


def calculation():
    for i in range(100):
        print(f"Calculation number {i}.")
        x = scipy.special.stdtrit(df, q)


if __name__ == "__main__":
    thread1 = threading.Thread(target=calculation)
    thread2 = threading.Thread(target=calculation)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()

This always hangs Python completely. Output is for example:

Calculation number 0.
Calculation number 1.
Calculation number 2.
Calculation number 3.
Calculation number 4.
Calculation number 0.

With df = np.arange(5, 505) the program always finishes successfully. My workaround now is to guard calculations involving stdtrit (relevant for me is scipy.stats.t.ppf) with a threading.Lock, which works. When replacing the threads with processes, the issue is also not present.

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.special

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions