Skip to content

Commit 5e00e8c

Browse files
authored
Fix infinite loop in _guess_num_chunks_per_axis_shard for 0-dimensional arrays (#4305)
* Fix infinite loop in shard size guessing for 0-dimensional arrays _guess_num_chunks_per_axis_shard looped forever for rank-0 arrays because neither loop condition depends on the counter when chunk_shape is empty. Return 1 immediately in that case, which makes the budget path produce shards=() just like the no-budget path. Closes #4304 * Add changelog fragment
1 parent 5b5f3a3 commit 5e00e8c

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎changes/4305.bugfix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an infinite loop when creating a 0-dimensional array with `shards="auto"` while the `array.target_shard_size_bytes` config option is set. Such arrays now resolve to `shards=()`, matching the behavior when no shard size target is configured.

‎src/zarr/core/chunk_grids.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ def _guess_num_chunks_per_axis_shard(
865865
if max_bytes < bytes_per_chunk:
866866
return 1
867867
num_axes = len(chunk_shape)
868+
if num_axes == 0:
869+
return 1
868870
chunks_per_shard = 1
869871
# First check for byte size, second check to make sure we don't go bigger than the array shape
870872
while (bytes_per_chunk * ((chunks_per_shard + 1) ** num_axes)) <= max_bytes and all(

‎tests/test_chunk_grids.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import numpy as np
44
import pytest
55

6+
import zarr
67
from tests.conftest import Expect, ExpectFail
78
from zarr.core.chunk_grids import (
89
ChunkLayout,
10+
_guess_num_chunks_per_axis_shard,
911
_guess_regular_chunks,
1012
normalize_chunks_1d,
1113
normalize_chunks_nd,
1214
resolve_outer_and_inner_chunks,
1315
)
16+
from zarr.errors import ZarrUserWarning
1417

1518

1619
def _assert_chunks_equal(
@@ -277,3 +280,26 @@ def test_normalize_chunks_1d_returns_int64_array(
277280
assert result.dtype == np.int64
278281
assert result.ndim == 1
279282
assert result.tolist() == case.output
283+
284+
285+
def test_guess_num_chunks_per_axis_shard_0d() -> None:
286+
"""Regression test for https://github.com/zarr-developers/zarr-python/issues/4304."""
287+
assert (
288+
_guess_num_chunks_per_axis_shard(
289+
chunk_shape=(), item_size=8, max_bytes=128 * 1024 * 1024, array_shape=()
290+
)
291+
== 1
292+
)
293+
294+
295+
def test_create_0d_array_auto_shards_with_target_shard_size() -> None:
296+
"""A 0-dimensional array with shards="auto" and a shard size budget must not hang.
297+
298+
Regression test for https://github.com/zarr-developers/zarr-python/issues/4304.
299+
"""
300+
with (
301+
zarr.config.set({"array.target_shard_size_bytes": 128 * 1024 * 1024}),
302+
pytest.warns(ZarrUserWarning, match="Automatic shard shape inference is experimental"),
303+
):
304+
arr = zarr.create_array(store={}, shape=(), dtype="int64", shards="auto")
305+
assert arr.shards == ()

0 commit comments

Comments
 (0)