Skip to content

fix: publish cached ZIP writes on explicit filesystem close - #2312

Merged
martindurant merged 3 commits into
fsspec:masterfrom
Dcastroro:fix/zip-close-owned-file
Oct 20, 2026
Merged

martindurant merged 3 commits into
fsspec:masterfrom
Dcastroro:fix/zip-close-owned-file

Conversation

@Dcastroro

@Dcastroro Dcastroro commented Oct 16, 2026 •

Copy link
Copy Markdown

Closing a ZipFileSystem backed by simplecache currently finalizes the ZIP but leaves its OpenFile context open. The destination archive may remain absent until garbage collection, even though close() returned successfully. Release that context after the ZIP closes so cached writes are published immediately and close/upload failures reach the caller.

Only OpenFile contexts 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 OpenFile input, 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:

  • Linux, Python 3.12: core tests plus archive, ZIP, cached, memory, local and TAR suites — 1,889 passed, 116 skipped, 2 xfailed.
  • Windows, Python 3.12: complete 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 --check passed; 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.

Comment on lines +94 to +95
if isinstance(self.of, fsspec.core.OpenFile):
self.of.close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this happen before self.zip.close()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adenzhou1350

Copy link
Copy Markdown
Contributor

Independently checked 5668d93 on Windows/Python 3.14 and Linux/Python 3.12: all 49 ZIP tests passed on each. Caller-owned buffer/file checks pass the base and your head, but fail our #2313; I’ve closed ours in favor of this. This was local ZIP validation, not the full CI or live-storage matrix.

@martindurant

Copy link
Copy Markdown
Member

Still WIP?

@Dcastroro
Dcastroro marked this pull request as ready for review October 20, 2026 00:55
Copilot AI balanced review requested due to automatic review settings October 20, 2026 00:55
@Dcastroro

Copy link
Copy Markdown
Author

Ready now — thanks for merging the current master into the branch. I have revalidated a1e664de0c48298422e0c5638d4dc21b4fc2b675 after that update:

  • Linux / Python 3.12: core tests plus archive, ZIP, cached, memory, local and TAR suites: 1,889 passed, 116 skipped, 2 xfailed.
  • Windows / Python 3.12: the complete ZIP suite: 61 passed.
  • All applicable pre-commit hooks on the three PR files passed, including Ruff 0.14.3 and codespell.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 calls self.of.close() when self.of is an fsspec.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 OpenFile input, 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

@martindurant
martindurant merged commit 884ca31 into fsspec:master Oct 20, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZipFileSystem.close() does not publish simplecache writes until garbage collection

4 participants