Page MenuHomePhabricator

Make API cache writes atomic and failure-safe
Closed, ResolvedPublic

Description

Update CachedRequest._write_cache() in pywikibot/data/api/_requests.py to:

  • Serialize into a unique temporary file in the cache directory, preserving the existing format and pickle protocol.
  • Close the temporary file before publishing it with os.replace().
  • On failure, clean up only the temporary file, preserving any existing destination entry.
  • Warn on filesystem failures while allowing successful API responses to return. Preserve serialization exceptions and interruptions without masking them with cleanup errors.
  • Ensure the cache maintenance script skips temporary files.

Benefits

  • Prevent readers from encountering partially written cache entries.
  • Prevent failed writers from deleting existing entries or another writer’s completed entry.
  • Avoid secondary cleanup exceptions failing otherwise successful API requests.
  • Make cache persistence failures visible and diagnosable.

Limitations

  • Atomic replacement does not prevent concurrent cache misses from issuing duplicate API requests.
  • Use unique temporary filenames; a shared .tmp path introduces writer races.
  • Verify Windows behavior and file permissions.
  • Keep read-side exception changes, corrupt-entry deletion, and cache-key memoization outside this task.

Details

Event Timeline

Aruotes subscribed.

Hey, I would like to work on this task.
Thank you!!

Change #1348534 had a related patch set uploaded (by Aruotes; author: Aruotes):

[pywikibot/core@master] [api] Make API cache writes atomic and failure-safe

https://gerrit.wikimedia.org/r/1348534

Change #1348534 merged by jenkins-bot:

[pywikibot/core@master] [api] Make API cache writes atomic and failure-safe

https://gerrit.wikimedia.org/r/1348534

Mahveotm triaged this task as Medium priority.