fix: _load raises on valid-but-non-object JSON

a store file holding valid JSON that is not an object (null/number/string/array) returned a non-dict, breaking get/set/delete/get_all. now raises ValueError, matching the documented corrupt-file contract.

Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
disqualifier 2026-06-28 17:18:28 -04:00
parent 8f60f6e17b
commit e29c523c12

View File

@ -100,14 +100,21 @@ class AioKV:
async def _load(self) -> Dict[str, Any]:
"""load the store from disk, returning {} if the file is absent or empty
a truncated/corrupt file raises JSONDecodeError surfaced to the caller
rather than silently masking a real corruption.
a truncated/corrupt file raises JSONDecodeError, and a file holding valid
JSON that is not an object (e.g. a bare list, number, or null) raises
ValueError both surfaced to the caller rather than silently masking a
real corruption or returning a non-dict that breaks every other method.
"""
if not await asyncio.to_thread(os.path.exists, self.file):
return {}
async with aiofiles.open(self.file, mode="r") as f:
data = await f.read()
return json.loads(data) if data else {}
if not data:
return {}
loaded = json.loads(data)
if not isinstance(loaded, dict):
raise ValueError(f"store file {self.file} does not hold a JSON object")
return loaded
async def _save(self, cache: Dict[str, Any]) -> None:
"""write the store atomically: temp file in the same dir, then os.replace