Skip to content

BUG: Fix some bugs found via valgrind#30680

Merged
ngoldbaum merged 3 commits intonumpy:mainfrom
seberg:valgrind-fixes
Jan 22, 2026
Merged

BUG: Fix some bugs found via valgrind#30680
ngoldbaum merged 3 commits intonumpy:mainfrom
seberg:valgrind-fixes

Conversation

@seberg
Copy link
Member

@seberg seberg commented Jan 20, 2026

A hodgepodge of reference leaks, mostly small or rather irrelevant stuff (although the unique one is large).

Maybe valgrind has it's ups, it did find a Python issue as well, of course that issue was already fixed in 3.14.2 :).

EDIT: Also note, I have not run valgrind on f2py tests here.

EDIT3: There is also a small leak in numpy/_core/tests/test_dtype.py::TestUserDType::test_custom_structured_dtype_errors. I am ignoring it (names is not cleaned up on the last error, because user dtypes are never fully cleaned up and it is too late, but I don't feel like hard-coding partial cleanup there, if would have to do the full cleanup there).

Since I have no shame, I used this ridiculous script to parallelize. Your favorite LLM will write a prettier one though easily :).

Details
#!/usr/bin/env python

"""
NOTE: To open the files with actual failures, try something like:

find . -type f ! -name "*valgrind-out.txt" | xargs -I {} sh -c "grep -q -E '(FAILURES|ERRORS)' {} && gnome-text-editor {} &"
"""

from concurrent.futures import ProcessPoolExecutor
import os
import subprocess

os.environ["PYTHONMALLOC"] = "malloc"  # for the spawned processes

test_files = []

DIR = os.getcwd()
FOLDER = "/home/sebastianb/forks/numpy/"

for root, dirs, files in os.walk(FOLDER + "numpy/"):
    files = [f for f in files if f.startswith("test_") and f.endswith(".py")]
    root = root.removeprefix(FOLDER + "numpy/")
    test_files.extend([root + "/" + f for f in files])

for f in test_files[:]:
    # These are particularly slow, so start early.
    if "test_multiarray" in f or "ma/test_core" in f:
        test_files.remove(f)
        test_files.insert(0, f)


setup = """
export PYTHONPATH="/home/sebastianb/forks/numpy/build-install/usr/lib/python3.14/site-packages"
/home/sebastianb/miniforge3/envs/numpy-dev/bin/python3.14 -P -c 'import numpy'
"""

cmd = "PYTHONMALLOC=malloc valgrind --errors-for-leak-kinds=definite --show-leak-kinds=definite --log-file={log_file} "
cmd += '''python -P -mpytest -vv {test_file} -m "not slow"'''
cmd += " --valgrind --valgrind-log={log_file}"


def process(file):
    # Yeah, should use proper pathlib paths :)
    os.chdir(DIR)
    output = os.path.abspath("output/" + file.replace("/", "_").replace(".py", ".txt"))
    log = output.replace(".txt", "-valgrind-out.txt")

    with open(output, "w") as out_file:
        os.chdir("/home/sebastianb/forks/numpy/build-install/usr/lib/python3.14/site-packages")
        actual_command = cmd.format(log_file=log, test_file="numpy/" + file, FOLDER=FOLDER)
        print(actual_command)
        subprocess.run(setup, shell=True)
        subprocess.run(actual_command, shell=True, stdout=out_file, stderr=out_file)
        print("    done file:", file)


def run():
    with ProcessPoolExecutor(16) as executor:
        result = executor.map(process, test_files)

    print(list(result))

if __name__ == "__main__":
    run()

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Jan 20, 2026
@charris
Copy link
Member

charris commented Jan 20, 2026

@kumaraditya303 If backported there is still 3.11 to consider.

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found myself with some time between meetings and did a code review pass. Mostly looks great but I spotted a couple issues.

@ngoldbaum
Copy link
Member

Thanks @seberg!

@ngoldbaum ngoldbaum merged commit 070954a into numpy:main Jan 22, 2026
75 checks passed
@seberg seberg deleted the valgrind-fixes branch January 22, 2026 15:49
charris pushed a commit to charris/numpy that referenced this pull request Jan 22, 2026
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 22, 2026
charris added a commit that referenced this pull request Jan 22, 2026
BUG: Fix some bugs found via valgrind (#30680)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants