Repository navigation
fix: publish cached ZIP writes on explicit filesystem close - #2312
Conversation
| if isinstance(self.of, fsspec.core.OpenFile): | ||
| self.of.close() |
There was a problem hiding this comment.
Shouldn't this happen before self.zip.close()?
There was a problem hiding this comment.
It needs to happen after self.zip.close(): ZipFile.close() still writes the central directory/end-of-archive records, whereas OpenFile.close() closes the underlying handle (and, for simplecache, publishes its cached contents).
I tested the proposed reverse order on this PR's head, 5668d9350ff0ad20037d607668d067cc39bc8dc3. All four selected cases failed: both cached archive backends, releasing the OpenFile context, and retrying close with an open member. Restoring the current order made all four pass. The reverse order closes the handle before ZipFile can finish writing; it also loses the retry path when a member is still open.
The tests are test_close_publishes_cached_archive, test_close_releases_open_file_context and test_close_with_open_member_can_be_retried. No code changes were needed for this check.
|
Still WIP? |
|
Ready now — thanks for merging the current master into the branch. I have revalidated
The diff against current master remains the same scoped fix, eight regression/control cases and changelog entry. No further code changes were needed after your merge. I have marked the PR ready for review and updated the validation counts in its description. Optional remote-backend integrations and the full upstream Python/OS matrix were not run locally. |
There was a problem hiding this comment.
🔵 Needs a closer look
It changes the externally observable semantics of the public ZipFileSystem.close() method for a widely used filesystem, so a maintainer should confirm the behavioral change is acceptable for downstream callers.
No open issues 🎉
What changed in this PR
This PR fixes #2311, where closing a ZipFileSystem that itself opened a simplecache-backed archive would finalize the ZIP but leave the outer OpenFile context open, so the destination archive was only published on garbage collection rather than on close(). The fix releases the OpenFile context during close(), publishing cached writes immediately and surfacing close/upload failures to the caller.
Changes:
close()now callsself.of.close()whenself.ofis anfsspec.core.OpenFile, so ZIP-owned contexts are released on explicit close while caller-provided raw file handles stay open.- Adds eight regression/control tests covering immediate publication, explicit
OpenFileinput, read-handle cleanup, caller-owned buffers/files, retry after an open member, and upload-failure propagation. - Adds a changelog entry.
| File | Description |
|---|---|
fsspec/implementations/zip.py |
Closes the ZIP-entered OpenFile context in close() so cached writes are committed immediately and failures propagate. |
fsspec/implementations/tests/test_zip.py |
Adds regression/control tests across memory/local/simplecache backends for the new close behavior. |
docs/source/changelog.rst |
Documents the fix under the Fixes section. |
Review effort: Balanced
Closing a
ZipFileSystembacked bysimplecachecurrently finalizes the ZIP but leaves itsOpenFilecontext open. The destination archive may remain absent until garbage collection, even thoughclose()returned successfully. Release that context after the ZIP closes so cached writes are published immediately and close/upload failures reach the caller.Only
OpenFilecontexts entered by the ZIP filesystem are closed. Caller-provided file handles remain open on explicit close. Repeated close remains safe, and an attempt to close with an open ZIP writing handle still raises without closing the underlying file, allowing the member to finish and the close to be retried.Fixes #2311.
Added eight regression/control cases using real memory/local/simplecache backends: immediate publication, explicit
OpenFileinput, read-context cleanup, caller-owned buffers/files, retry after an open member, and propagation of an upload failure. Six fail against the unchanged implementation; both caller-owned-file controls already pass. A changelog entry is included.Validation re-run after the maintainer's master merge, at commit
a1e664de0c48298422e0c5638d4dc21b4fc2b675:test_zip.py— 61 passed.pre-commit run --files docs/source/changelog.rst fsspec/implementations/zip.py fsspec/implementations/tests/test_zip.py— all applicable hooks passed, including Ruff 0.14.3 formatting/lint and codespell.git diff --checkpassed; no credentials or external storage accounts are required by the added tests.Optional backend integrations and the full upstream Python/OS matrix were not run locally. The Linux selection reports two warnings in existing async/fork tests.
AI assistance was used to investigate, implement and test this contribution.