Skip to content
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

ReadOnlyError when opening a v3 store #1382

Closed
jhamman opened this issue Apr 6, 2023 · 1 comment · Fixed by #1383
Closed

ReadOnlyError when opening a v3 store #1382

jhamman opened this issue Apr 6, 2023 · 1 comment · Fixed by #1383
Labels
bug Potential issues with the zarr-python library

Comments

@jhamman
Copy link
Member

jhamman commented Apr 6, 2023

Zarr version

v2.14.2

Numcodecs version

v0.11.0

Python Version

3.10.8

Operating System

Mac (M2)

Installation

pip

Description

When opening a v3 store, it is possible to trigger zarr.errors.ReadOnlyError: object is read-only when mode='r'.

Steps to reproduce

The reproducer here is a bit tough because it requires using Fsspec+Kerchunk+ZarrV3 but I'll show a traceback and point to a traceback and a where the offending code is in Zarr:

Traceback:

/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/xarray/backends/api.py:526: in open_dataset
    backend_ds = backend.open_dataset(
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/xarray/backends/zarr.py:891: in open_dataset
    store = ZarrStore.open_group(
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/xarray/backends/zarr.py:430: in open_group
    zarr_group = zarr.open_group(store, **open_kwargs)
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/zarr/hierarchy.py:1416: in open_group
    store = _normalize_store_arg(
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/zarr/hierarchy.py:1288: in _normalize_store_arg
    return normalize_store_arg(store,
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/zarr/storage.py:181: in normalize_store_arg
    return normalize_store(store, storage_options, mode)
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/zarr/_storage/v3.py:622: in _normalize_store_arg_v3
    store['zarr.json'] = store._metadata_class.encode_hierarchy_metadata(None)
/Users/jhamman/miniforge3/envs/zarr-testlib/python3.10/site-packages/zarr/_storage/v3.py:115: in __setitem__
    super().__setitem__(key, value)

This happens because Zarr is trying to add a key to a store that is opened with mode='w':

# add default zarr.json metadata
store['zarr.json'] = store._metadata_class.encode_hierarchy_metadata(None)

Additional output

A localized reproducer can be triggered like this:

out = {'version': 1, 'refs': {'zarr.json': '{"extensions":[],"metadata_encoding":"https:\\/\\/purl.org\\/zarr\\/spec\\/protocol\\/core\\/3.0","metadata_key_suffix":".json","zarr_format":"https:\\/\\/purl.org\\/zarr\\/spec\\/protocol\\/core\\/3.0"}', 'meta/root.group.json': '{"attributes":{"_zroot":"<zarr.hierarchy.Group \'\\/\'>","attr0":"3"}}', 'meta/root/data.array.json': '{"attributes":{"_ARRAY_DIMENSIONS":["x","y"]},"chunk_grid":{"chunk_shape":[10,10],"separator":"\\/","type":"regular"},"chunk_memory_layout":"C","data_type":">f8","extensions":[],"fill_value":null,"shape":[10,10]}', 'data/root/data/c0/0': ['memory://data.nc3', 152, 800]}}
_normalize_store_arg_v3("reference://", storage_options={"fo": out, "remote_protocol": "memory"})

The full reproducer is a test failure in Kerchunk:

Proposal

This could be a fairly simple fix to _normalize_store_arg_v3 where the 'zarr.json' key is only added when the store is writable:

if mode != 'r':
    store['zarr.json'] = store._metadata_class.encode_hierarchy_metadata(None) 

cc @martindurant

@jhamman jhamman added the bug Potential issues with the zarr-python library label Apr 6, 2023
@rabernat
Copy link
Contributor

rabernat commented Apr 6, 2023

There have been a ton of these "read only" errors recently.

I thought we fixed it in 2.14.2. But maybe not fully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Potential issues with the zarr-python library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants