Skip to content

Commit 89f72ee

Browse files
authored
Fix UPath pickling bug (#835)
* Resolve issue where `segy_to_mdio` ingestion failed in trace ingestion phase if `output` contained a `UPath` with `storage_options` * Ignore false positive for hardcoded "secret"
1 parent b6fe55a commit 89f72ee

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/mdio/api/io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def _normalize_path(path: UPath | Path | str) -> UPath:
3030

3131

3232
def _normalize_storage_options(path: UPath) -> dict[str, Any] | None:
33-
return None if len(path.storage_options) == 0 else path.storage_options
33+
# UPath.storage_options returns a read-only mappingproxy which cannot be pickled. Copy it into a
34+
# plain dict so callers can safely pass it across process boundaries (e.g. spawned workers).
35+
return None if len(path.storage_options) == 0 else dict(path.storage_options)
3436

3537

3638
def open_mdio(input_path: UPath | Path | str, chunks: T_Chunks = None) -> xr_Dataset:

tests/unit/test_io.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Tests for low-level MDIO API I/O helpers."""
2+
3+
from __future__ import annotations
4+
5+
from types import MappingProxyType
6+
7+
from upath import UPath
8+
9+
from mdio.api.io import _normalize_storage_options
10+
11+
12+
def test_normalize_storage_options_is_not_mappingproxy() -> None:
13+
"""Storage options must not be a mappingproxy.
14+
15+
`UPath.storage_options` returns a read-only ``mappingproxy`` that cannot be pickled. Blocked-I/O
16+
ingestion passes these options into ``ProcessPoolExecutor`` initargs, so a mappingproxy breaks
17+
spawned workers with ``TypeError: cannot pickle 'mappingproxy' object``.
18+
"""
19+
storage_options = _normalize_storage_options(UPath("s3://bucket/key", key="access", secret="secret")) # noqa: S106
20+
21+
assert not isinstance(storage_options, MappingProxyType)

0 commit comments

Comments
 (0)