Skip to content

fix(caching): skip materialized payloads in FileResultStore.delete_expired - #1834

Open
getBrijendra wants to merge 1 commit into
apache:mainfrom
getBrijendra:fix/file-store-delete-expired-1813
Open

getBrijendra wants to merge 1 commit into
apache:mainfrom
getBrijendra:fix/file-store-delete-expired-1813

Conversation

@getBrijendra

Copy link
Copy Markdown

Fixes FileResultStore.delete_expired() crashing with UnpicklingError when the store contains results saved with a saver/loader (e.g., the built-in JSON saver).

Closes #1813

Changes

FileResultStore.set() with a saver/loader writes two files into the store directory: a pickled loader at <data_version> and the materialized payload at <data_version>.<saver name> (via _materialized_path). delete_expired() iterated every directory entry and passed it to StoredResult.load(), so the payload (e.g., example.json) was unpickled and raised.

  • hamilton/caching/stores/file.py
    • Add _is_result_entry(): result entries are regular files with no suffix; materialized payloads always have a suffix, and some savers write directories.
    • delete_expired() skips non-result entries, and skips entries that fail to unpickle (UnpicklingError / EOFError) instead of aborting cleanup.
  • tests/caching/result_store/test_file.py
    • test_delete_expired_skips_materialized_payloads (json + pickle): cleanup succeeds, both files remain, values stay readable.
    • test_delete_expired_ignores_unreadable_entries: a non-pickle file, an empty file and a suffixed directory don't break cleanup.

How I tested this

  • Ran the reproduction script from FileResultStore.delete_expired crashes on JSON-materialized cache results #1813: raised UnpicklingError before the change, passes after.
  • New tests: 2 of 3 fail without the fix (UnpicklingError, EOFError); all pass with it. (The pickle-format case passes either way since that payload is valid pickle; it's kept for coverage.)
  • pytest tests/caching/result_store tests/caching/test_result_store.py: 33 passed.
  • pytest tests/ --ignore tests/integrations on Windows / Python 3.12: 1721 passed, 12 failed. The failures are unrelated to this change and environment-specific: Graphviz dot not installed (6), MultiProcessingExecutor BrokenProcessPool on Windows (3), <unhashable> being an invalid Windows filename (1), and cp1252 decoding / h_async subprocess in test_deprecation_schedules.py (2). I confirmed the caching-integration failures also occur without this change.
  • ruff check / ruff format --check (0.16.9) clean on the changed files.

Notes

Kept deliberately narrow, per the issue. Related problems noticed while investigating, which could be follow-ups:

  • StoredResult.save() doesn't persist expires_at and StoredResult.load() rebuilds with no expiry, so delete_expired() currently never removes anything.
  • delete_expired() loads every value (running loaders) just to check expiry.
  • delete() / expiry remove only the pointer file, leaving the materialized payload behind.

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

🤖 Generated with Claude Code

…pired

`FileResultStore.set()` with a saver/loader writes a pickled loader at
`<data_version>` and the payload at `<data_version>.<saver name>` in the
same directory. `delete_expired()` iterated every entry and unpickled it,
so a JSON (or any non-pickle) payload raised `UnpicklingError`.

Only treat suffix-less files as result entries, and skip entries that
can't be unpickled instead of crashing cleanup.

Closes apache#1813

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getBrijendra

Copy link
Copy Markdown
Author

I saw @ashutosh8021 offered to take this on in #1813. I ran into it while exploring the caching code and put this up in case it helps. Happy to close this in favour of their PR, or add them as co-author, whichever the maintainers and @ashutosh8021 prefer.

This branch has not been deployed

No deployments
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.

FileResultStore.delete_expired crashes on JSON-materialized cache results

1 participant