From 5193789d03f5b5b8978f203ffa643d158eb5377b Mon Sep 17 00:00:00 2001 From: udayraj1238 Date: Sat, 5 Dec 2026 11:03:11 +0530 Subject: [PATCH] fix(storage): avoid passing asynchronous=True to sync fsspec filesystems Closes zarr-developers/zarr-python#3576. Assisted-by: Antigravity --- changes/3576.bugfix.md | 1 + src/zarr/storage/_fsspec.py | 13 ++++- tests/test_store/test_fsspec.py | 99 +++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 changes/3576.bugfix.md diff --git a/changes/3576.bugfix.md b/changes/3576.bugfix.md new file mode 100644 index 0000000000..3bd819a600 --- /dev/null +++ b/changes/3576.bugfix.md @@ -0,0 +1 @@ +Fixed `FsspecStore.from_url` passing `asynchronous=True` to synchronous `fsspec` filesystems (such as `ssh://` / `SFTPFileSystem`), which do not have `async_impl = True` and are subsequently wrapped with `_make_async`. diff --git a/src/zarr/storage/_fsspec.py b/src/zarr/storage/_fsspec.py index 137043c901..de1754d211 100644 --- a/src/zarr/storage/_fsspec.py +++ b/src/zarr/storage/_fsspec.py @@ -248,8 +248,17 @@ def from_url( # before fsspec==2024.3.1 from fsspec.core import url_to_fs - opts = storage_options or {} - opts = {"asynchronous": True, **opts} + from fsspec.core import split_protocol + from fsspec.registry import get_filesystem_class + + opts = dict(storage_options) if storage_options else {} + if "::" in url: + head = url.split("::", 1)[0] + protocol = split_protocol(head)[0] or head + else: + protocol = opts.get("protocol") or split_protocol(url)[0] or "file" + if getattr(get_filesystem_class(protocol), "async_impl", False): + opts.setdefault("asynchronous", True) fs, path = url_to_fs(url, **opts) if not fs.async_impl: diff --git a/tests/test_store/test_fsspec.py b/tests/test_store/test_fsspec.py index faea1caf0f..c395d7dc16 100644 --- a/tests/test_store/test_fsspec.py +++ b/tests/test_store/test_fsspec.py @@ -783,3 +783,102 @@ async def test_memory_scheme() -> None: store = await make_store("memory://test") assert isinstance(store, FsspecStore) assert store.fs.protocol == "memory" + + +@pytest.mark.skipif( + parse_version(fsspec.__version__) < parse_version("2024.12.0"), + reason="No AsyncFileSystemWrapper", +) +def test_from_url_ssh_does_not_pass_asynchronous(monkeypatch: pytest.MonkeyPatch) -> None: + """Regression test for https://github.com/zarr-developers/zarr-python/issues/3576 .""" + import inspect + + paramiko = pytest.importorskip("paramiko") + from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper + from fsspec.implementations.sftp import SFTPFileSystem + + orig_connect = paramiko.SSHClient.connect + connect_sig = inspect.signature(orig_connect) + + def fake_connect(client: Any, hostname: str, *args: Any, **kwargs: Any) -> None: + connect_sig.bind(client, hostname, *args, **kwargs) + + monkeypatch.setattr(paramiko.SSHClient, "connect", fake_connect) + monkeypatch.setattr(paramiko.SSHClient, "open_sftp", lambda _self: object()) + + storage_options: dict[str, Any] = {"username": "tester"} + store = FsspecStore.from_url( + "ssh://example.invalid/tmp/test.zarr", storage_options=storage_options + ) + + assert storage_options == {"username": "tester"} + assert isinstance(store.fs, AsyncFileSystemWrapper) + assert isinstance(store.fs.sync_fs, SFTPFileSystem) + assert store.fs.asynchronous + assert store.path == "/tmp/test.zarr" + + +@pytest.mark.skipif( + parse_version(fsspec.__version__) < parse_version("2024.12.0"), + reason="No AsyncFileSystemWrapper", +) +def test_from_url_sync_and_async_protocols(monkeypatch: pytest.MonkeyPatch) -> None: + """``from_url`` only defaults ``asynchronous=True`` for async-capable filesystems.""" + import importlib + + from fsspec.asyn import AsyncFileSystem + from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper + from fsspec.spec import AbstractFileSystem + + reg_mod = importlib.import_module("fsspec.registry") + reg_dict = getattr(reg_mod, "_registry", reg_mod.registry) + + sync_proto = "_test_sync_proto" + async_proto = "_test_async_proto" + + class _SyncStrictFS(AbstractFileSystem): # type: ignore[misc] + protocol = sync_proto + async_impl = False + + def __init__(self, **kwargs: Any) -> None: + if "asynchronous" in kwargs: + raise TypeError("unexpected keyword argument 'asynchronous'") + super().__init__(**kwargs) + + class _AsyncFS(AsyncFileSystem): # type: ignore[misc] + protocol = async_proto + async_impl = True + + monkeypatch.setitem(reg_dict, sync_proto, _SyncStrictFS) + monkeypatch.setitem(reg_dict, async_proto, _AsyncFS) + + store_direct = FsspecStore.from_url(f"{sync_proto}://bucket/data.zarr") + assert isinstance(store_direct.fs, AsyncFileSystemWrapper) + assert isinstance(store_direct.fs.sync_fs, _SyncStrictFS) + assert store_direct.fs.asynchronous + + store_chained = FsspecStore.from_url(f"simplecache::{sync_proto}://bucket/data.zarr") + assert isinstance(store_chained.fs, AsyncFileSystemWrapper) + assert store_chained.fs.asynchronous + + opts_proto: dict[str, Any] = {"protocol": sync_proto} + store_by_option = FsspecStore.from_url("bucket/data.zarr", storage_options=opts_proto) + assert opts_proto == {"protocol": sync_proto} + assert isinstance(store_by_option.fs, AsyncFileSystemWrapper) + assert isinstance(store_by_option.fs.sync_fs, _SyncStrictFS) + + opts_async: dict[str, Any] = {} + store_async = FsspecStore.from_url( + f"{async_proto}://bucket/data.zarr", storage_options=opts_async + ) + assert opts_async == {} + assert isinstance(store_async.fs, _AsyncFS) + assert store_async.fs.asynchronous + + with pytest.warns(ZarrUserWarning, match=r".* was not created with `asynchronous=True`.*"): + store_explicit_sync = FsspecStore.from_url( + f"{async_proto}://bucket/data.zarr", + storage_options={"asynchronous": False}, + ) + assert isinstance(store_explicit_sync.fs, _AsyncFS) + assert not store_explicit_sync.fs.asynchronous