Skip to content

Commit 245acae

Browse files
d-v-bclaude
andcommitted
fix(array): leave a valid stored document as written on a stale handle's first write
A handle read from an upgraded document re-reads the stored document before its first chunk write. If that document no longer needs an upgrade, store nothing: re-encoding it could differ from how another implementation wrote it, and nothing about it needs fixing. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 632ac2b commit 245acae

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎src/zarr/core/array.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,21 +1617,22 @@ async def _save_metadata(self, metadata: ArrayMetadata, ensure_parents: bool = F
16171617
await save_metadata(self.store_path, metadata, ensure_parents=ensure_parents)
16181618

16191619
async def _store_upgraded_document(self) -> None:
1620-
"""Store the upgrade of this array's current stored document, if it differs from
1621-
what the store holds.
1620+
"""Store the upgrade of this array's current stored document, if it needs one.
16221621
16231622
Only for metadata read from a document that had to be upgraded (see
16241623
`zarr.core.metadata.upgrades`). The document is read again, because the store may
16251624
hold a newer one than this handle's metadata: if that one needs no upgrade (the
1626-
array was re-saved or resized since), nothing is stored. Storing the same upgrade
1627-
twice is harmless, so concurrent callers need no coordination.
1625+
array was re-saved or resized since, possibly by another implementation), it is
1626+
left as written. Storing the same upgrade twice is harmless, so concurrent
1627+
callers need no coordination.
16281628
"""
16291629
if not self.metadata._stored_document_upgraded:
16301630
return
16311631
zarr_format = self.metadata.zarr_format
16321632
stored = await get_array_metadata(self.store_path, zarr_format=zarr_format)
1633-
upgraded, _ = upgrade_array_document(stored, zarr_format)
1634-
await upsert_metadata(self.store_path, parse_array_metadata(upgraded))
1633+
upgraded, readings = upgrade_array_document(stored, zarr_format)
1634+
if readings:
1635+
await upsert_metadata(self.store_path, parse_array_metadata(upgraded))
16351636
object.__setattr__(self.metadata, "_stored_document_upgraded", False)
16361637

16371638
async def _set_selection(

‎tests/test_metadata/test_upgrades.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,33 @@ def test_stale_handle_write_keeps_newer_metadata(
553553
np.testing.assert_array_equal(reopened[...], [9, 0, 0, 1, 2, 3, 4, 5, 6])
554554

555555

556+
@pytest.mark.parametrize("zarr_format", [2, 3])
557+
def test_stale_handle_write_keeps_valid_document_as_written(
558+
tmp_path: Path, zarr_format: Literal[2, 3]
559+
) -> None:
560+
"""If the document the store holds when a handle read from an upgraded document
561+
first writes chunks needs no upgrade, it is left as written, even where zarr would
562+
encode the same metadata differently (as another implementation may have written it)."""
563+
path = tmp_path / "legacy.zarr"
564+
_legacy_array(path, zarr_format)
565+
with pytest.warns(ZarrUserWarning, match="is read as"):
566+
stale = zarr.open_array(store=path, mode="r+")
567+
# Valid metadata for the same array, as another writer might store it: chunk size
568+
# 3, without the optional members zarr writes, as compact JSON.
569+
doc_path = path / (".zarray" if zarr_format == 2 else "zarr.json")
570+
doc = json.loads(doc_path.read_text())
571+
for optional in ("dimension_separator", "attributes", "storage_transformers"):
572+
doc.pop(optional, None)
573+
_stored_chunks(doc)[0] = 3
574+
doc_path.write_text(json.dumps(doc, separators=(",", ":")))
575+
written = doc_path.read_bytes()
576+
577+
stale[0] = 9
578+
579+
assert doc_path.read_bytes() == written
580+
np.testing.assert_array_equal(_open_strictly(path)[...], [9, 0, 0])
581+
582+
556583
@pytest.mark.parametrize("zarr_format", [2, 3])
557584
def test_empty_write_stores_no_metadata(tmp_path: Path, zarr_format: Literal[2, 3]) -> None:
558585
"""A write of an empty selection stores no chunks, so it stores no metadata either."""

0 commit comments

Comments
 (0)