-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly closes zarr groups in zarr store #8425
Conversation
If the zarr backend is using a store which needs closing, e.g., a zip store, it should be closed with the xarray zarrstore backend.
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Thanks for the PR @CarlAndersson ! I notice that a bunch of tests are failing. What's the motivating case for where the existing code was failing? |
The code I had that was failing can be replicated as original_da = xr.DataArray(np.arange(12).reshape((3, 4)))
original_da.to_zarr('tmp.zarr.zip', mode='w')
loaded_data = xr.load_dataarray('tmp.zarr.zip', engine='zarr') which fails with a The fix was to add a original_da = xr.DataArray(np.arange(12).reshape((3, 4)))
original_da.to_zarr('tmp.zarr.zip', mode='w').zarr_group.store.close()
loaded_data = xr.load_dataarray('tmp.zarr.zip', engine='zarr') In my mind, calling original_da = xr.DataArray(np.arange(12).reshape((3, 4)))
original_da.to_zarr('tmp.zarr.zip', mode='w').close()
loaded_data = xr.load_dataarray('tmp.zarr.zip', engine='zarr') This seems to fail since that the default I haven't looked at the implementations of |
Thanks for the explanation. I don't know this code that well, so I'm still a bit confused, sorry. To the extent the existing code closes the store after writing, why is the zip file in a bad state? To the extent it doesn't close the store, why does the proposed code generate errors about closing things that are already closed? Or are there two entities, and the existing code only closes one? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @CarlAndersson for looking into this!
Would love to see some tests toward this change as well.
xarray/backends/zarr.py
Outdated
@@ -751,7 +751,7 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No | |||
writer.add(v.data, zarr_array, region) | |||
|
|||
def close(self): | |||
pass | |||
self.zarr_group.store.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We likely want to do this here only when the Zarr backend was responsible for opening the ZipStore. You may need to add some logic/state in open_group
to manage this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable, yes. I added some logic for it and a test case that would have failed before this change.
Adds some logic to make sure to only close zarr stores when they were created as part of the `open_group` method on `ZarrStore`.
This got lost during a merge conflict.
This should be working now. There's a test failing, but I'm fairly sure that it's got nothing to do with my changes - it's not using zarr at all as far as I can see. |
yeah, ignore that one, it's flaky |
@jhamman @max-sixty Is there anything additional that I should do here? I'm not so sure about the process... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for this contribution, @CarlAndersson
Thanks a lot @CarlAndersson ! |
If the zarr backend is using a store which needs closing, e.g., a zip store, it should be closed with the xarray zarrstore backend.
This is occurred when i tried to save a dataset to a zarr file while giving the .zip file extension.
I have not done extensive tests on this small piece of code, so it might not cover all use cases.
Let me know if I need to add tests or more documentation!
whats-new.rst