Skip to content

Memory corruption bug in Jupyter for NumPy arrays >= 2^15 elements #143645

@syigzaw

Description

@syigzaw

Bug report

Bug description:

In a Jupyter environment, for NumPy arrays of size 2^15 elements or larger, running func(arr) can potentially corrupt the data within the array, while running func.call(arr) will not.

# Python 3.14.0 + IPython Memory Corruption Bug
# Intermediate arithmetic results corrupt local variables for arrays >= 2^15 elements

import numpy as np
from scipy.ndimage import uniform_filter

def func(arr):
    x = uniform_filter(arr, size=7)
    x_before = x.copy()
    y = 2 * x * x  # This line corrupts x!
    x_after = x.copy()
    return x_before[0, 0], x_after[0, 0], y[0, 0], np.array_equal(x_before, x_after)

# Test with array size below threshold (2^15 - 1 = 32767)
small = np.ones((32767, 1))
before1, after1, y1, same1 = func(small)
before2, after2, y2, same2 = func.__call__(small)
print(f"32767 elements | func():        x_before={before1}, x_after={after1}, y={y1} | Same={same1}")
print(f"32767 elements | func.__call__: x_before={before2}, x_after={after2}, y={y2} | Same={same2}")

# Test with array size at threshold (2^15 = 32768)
large = np.ones((32768, 1))
before3, after3, y3, same3 = func(large)
before4, after4, y4, same4 = func.__call__(large)
print(f"32768 elements | func():        x_before={before3}, x_after={after3}, y={y3} | Same={same3}")
print(f"32768 elements | func.__call__: x_before={before4}, x_after={after4}, y={y4} | Same={same4}")

# Output
# 32767 elements | func():        x_before=1.0, x_after=1.0, y=2.0 | Same=True
# 32767 elements | func.__call__: x_before=1.0, x_after=1.0, y=2.0 | Same=True
# 32768 elements | func():        x_before=1.0, x_after=2.0, y=4.0 | Same=False
# 32768 elements | func.__call__: x_before=1.0, x_after=1.0, y=2.0 | Same=True

numpy 2.2.6
scipy 1.16.3
ipykernel 7.1.0
ipython 9.7.0
jupyter-client 8.6.3
jupyter-core 5.9.1

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions