Fatal crash after deep-copy of data outside context manager
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided MVCE with xarray's netCDF4 engine on Windows and confirm the access-violation crash after deep-copying outside the context manager. Trace the deep-copy and resource-lifecycle paths involved in open_dataarray, then add a regression test or behavior check showing that reopening the file either succeeds or raises a Python exception instead of crashing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100