fix: implement soft-delete for ZipStore (issue #828) - #4107
fix: implement soft-delete for ZipStore (issue #828)#4107mohammadZuherJaserAsad wants to merge 7 commits into
Conversation
Implement soft-delete via b"" overwrite. supports_deletes=True. Update _get, exists, list, delete, delete_dir.
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
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.
|
Soft delete is the best we can here I think. I have not looked closely at the implementation yet. |
|
Hi @mkitti — following up since it's been about a month since this PR was opened. Since your last comment, the soft-delete approach has been redesigned: Slow Hypothesis CI now passes cleanly on the ZipInfo.comment-based marker (the earlier sentinel-value approach had a real collision bug that Hypothesis caught twice). The remaining failures are on the py3.12/py3.14 minimal-deps matrix in test_array.py, which looks unrelated to the files this PR touches. Would you or another maintainer have a chance to take a look when you get a moment? Happy to make any changes needed. Thanks for your time! |
|
Currently, the soft-delete mechanism is to add a new central directory entry_ with the sentinel marker in the zipinfo comment (the per-entry comment, not the archive comment). If a new entry is added of the same name is subsequently added after a soft delete, it becomes yet another entry in the file. Detecting the existence of a file thus requires the entire central directory to be read. Finding the first soft delete entry is not sufficient because there may be a subsequent entry indicating a new file has been added. While my preference for a soft delete would be just to remove the entry from the central directory completely that would more of a hack. It is possible because There was an issue in the past where some zip clients might only look at the first entry of a zip file. A recent development is that python/cpython#134627 has been merged on June 20th, 2026, a week before this pull request was created. The cpython pull request adds The new Currently, the upstream pull request is not part of a tagged release. It would be part of c-python 3.16 at the earliest. For c-python versions that contain that feature we should probably just use The question then is do we want to do something before that upstream release? |
|
https://github.com/danny0838/zipremove might be used to enable this feature before a Python release which contains |
Summary
Fixes #828 — implements soft-delete for
ZipStoreso thatdelete()anddelete_dir()no longer raiseNotImplementedError.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.pysupports_deletes = True(wasFalse)_SOFT_DELETE_SENTINEL = b""constantdelete(key): writes sentinel to zip entry; no-op if key is absentdelete_dir(prefix): collects live keys under prefix, callsdelete()on each_get(): reads all bytes first; returnsNoneif content equals sentinelexists(): returnsFalsefor entries whose content equals sentinellist(): deduplicates entries (duplicate names can appear after overwrites) and skips sentinel entrieslist_dir(): consumes filteredlist()output for consistencytests/test_store/test_zip.pydelete_dir, andlist()filteringtest_api_integrationupdated: removed twopytest.raises(NotImplementedError)blocks that are no longer correcttests/test_store/test_stateful.pypytest.skip(reason="ZipStore does not support delete")blocksDesign notes
namelist()returns the name twice.NameToInfotracks the last-written entry, so reading always returns the sentinel.list()uses aseenset to yield each name at most once.delete_dir()holds theRLockwhile callinglist_prefix()and thendelete()(which also acquires it). This works becausethreading.RLockis re-entrant for the same thread.set_if_not_existsedge case: After a soft-delete the name is still innamelist(), soset_if_not_existswill 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"