Skip to content

Fatal crash after deep-copy of data outside context manager #10431

Open
@RubendeBruin

Description

@RubendeBruin

What happened?

If I want to open a the same file to read data from it the interpreter crashes. Probably other situations where the code would crash as well.

The bug seems to originate from the fact the copy(deep=True) is executed outside the context manager. If the code is move into the context manager then it runs fine.

What did you expect to happen?

It should either just work,
or an exception should be thrown when making a deep-copy outside the context (if that is indeed the cause).

Minimal Complete Verifiable Example

import xarray as xr

filename = 'dummy.nc'

with xr.open_dataarray(filename, group="damping", engine="netcdf4") as ds:
    damping = ds

# make a deep-copy and interpolate the copy (outside context manager)
new_damping = ds.copy(deep=True)
new_damping = new_damping.interp(omega=[0.1, 0.2, 0.3], method="linear")

# opening "damping" again - ok
with xr.open_dataarray(filename, group="damping", engine="netcdf4") as ds:
    pass


print('opening "mass" again - crashes')
with xr.open_dataarray(filename, group="mass", engine="netcdf4") as ds:
    pass

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Process finished with exit code -1073741819 (0xC0000005)

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.10 (main, Apr 9 2025, 04:06:22) [MSC v.1943 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 11
machine: AMD64
processor: AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD
byteorder: little
LC_ALL: None
LANG: None
LOCALE: ('English_Netherlands', '1252')
libhdf5: 1.14.4
libnetcdf: 4.9.2

xarray: 2025.6.1
pandas: 2.3.0
numpy: 2.3.0
scipy: 1.15.3
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.10.3
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: None
conda: None
pytest: 8.4.1
mypy: None
IPython: None
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triageIssue that has not been reviewed by xarray team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions