Skip to content

fix: implement soft-delete for ZipStore (issue #828)#4107

Open
mohammadZuherJaserAsad wants to merge 6 commits into
zarr-developers:mainfrom
mohammadZuherJaserAsad:fix-issue-828-1
Open

fix: implement soft-delete for ZipStore (issue #828)#4107
mohammadZuherJaserAsad wants to merge 6 commits into
zarr-developers:mainfrom
mohammadZuherJaserAsad:fix-issue-828-1

Conversation

@mohammadZuherJaserAsad

Copy link
Copy Markdown

Summary

Fixes #828 — implements soft-delete for ZipStore so that delete() and delete_dir() no longer raise NotImplementedError.

The ZIP format has no native entry-removal API (a known CPython limitation: python/cpython#51067). This PR uses the soft-delete approach suggested by @dcherian in the issue: overwrite the entry with an empty byte sentinel (b"") and filter it out in all read, exists, and list paths.

Changes

src/zarr/storage/_zip.py

  • supports_deletes = True (was False)
  • Added module-level _SOFT_DELETE_SENTINEL = b"" constant
  • delete(key): writes sentinel to zip entry; no-op if key is absent
  • delete_dir(prefix): collects live keys under prefix, calls delete() on each
  • _get(): reads all bytes first; returns None if content equals sentinel
  • exists(): returns False for entries whose content equals sentinel
  • list(): deduplicates entries (duplicate names can appear after overwrites) and skips sentinel entries
  • list_dir(): consumes filtered list() output for consistency

tests/test_store/test_zip.py

  • 7 new targeted tests covering: basic delete, no-op cases, sibling-key safety, double-delete, delete_dir, and list() filtering
  • test_api_integration updated: removed two pytest.raises(NotImplementedError) blocks that are no longer correct

tests/test_store/test_stateful.py

  • Removed two pytest.skip(reason="ZipStore does not support delete") blocks

Design notes

  • Why soft-delete? The only alternative that preserves persistence is rebuilding the entire zip on every delete, which is O(n) in archive size. The maintainer's own suggestion in the issue was the sentinel approach; prior PRs zipstore delitems via override #1184 and Implement soft delete for zip store #2838 validated it.
  • Duplicate zip entries: When an entry is overwritten (including soft-deleted), namelist() returns the name twice. NameToInfo tracks the last-written entry, so reading always returns the sentinel. list() uses a seen set to yield each name at most once.
  • Lock re-entrancy: delete_dir() holds the RLock while calling list_prefix() and then delete() (which also acquires it). This works because threading.RLock is re-entrant for the same thread.
  • set_if_not_exists edge case: After a soft-delete the name is still in namelist(), so set_if_not_exists will not re-write it. This matches the documented semantics and existing zarr usage patterns.

Testing

pytest tests/test_store/test_zip.py -v
pytest tests/test_store/test_stateful.py -v -k "not slow_hypothesis"

@github-actions github-actions Bot added the needs release notes Automatically applied to PRs which haven't added release notes label Jun 27, 2026
@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 44.18605% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.30%. Comparing base (b9d3964) to head (2b784fe).
⚠️ Report is 52 commits behind head on main.

Files with missing lines Patch % Lines
src/zarr/storage/_zip.py 44.18% 24 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4107      +/-   ##
==========================================
- Coverage   93.53%   84.30%   -9.24%     
==========================================
  Files          88       90       +2     
  Lines       11894    15415    +3521     
==========================================
+ Hits        11125    12995    +1870     
- Misses        769     2420    +1651     
Files with missing lines Coverage Δ
src/zarr/storage/_zip.py 82.27% <44.18%> (-15.42%) ⬇️

... and 45 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@d-v-b

d-v-b commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

@mkitti

Hypothesis's stateful property tests caught two real bugs:

- Setting a key to b"" (a legitimate zero-length value) was indistinguishable from a soft-deleted key, since the sentinel was also b"". list()/exists()/get() incorrectly treated it as missing. Switched to a long, specific sentinel that can't plausibly collide with real payload bytes.
- delete() writes the sentinel to an already-existing zip entry, which zipfile.writestr() flags with a "Duplicate name" UserWarning every time. This project's pytest config turns warnings into errors, so any delete of an existing key failed the test suite. The warning is suppressed specifically in delete(), since it's expected/intentional there (unlike set() overwrites, which intentionally keep warning per test_api_integration).

Also fixes the ruff lint failures (unused ZipStore import in test_stateful.py, one reformatted line) and adds filterwarnings markers to the stateful tests to account for the pre-existing, expected duplicate-name warning on ZipStore overwrites.
test: remove unused ZipStore import; expect duplicate-name warning in stateful tests
The previous soft-delete used a fixed sentinel byte string as entry data, which Slow Hypothesis CI proved can still collide with a legitimate value (Hypothesis found a falsifying example that set a key to the literal sentinel bytes). This replaces it with a ZipInfo.comment flag, which is metadata never exposed through set()/get() and therefore cannot collide with any data a caller stores, while still persisting the deletion to disk in the zip central directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs release notes Automatically applied to PRs which haven't added release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implementing ZipStore's __delitem__ via overwrite

2 participants