Clean up stale cached avatars automatically - #3635
Conversation
|
Hey @faisalahammad 👋 Thanks for your contribution, but I think we should find the source of the issue, to prevent that a second entry is even added, instead of fighting (only) the symptoms. |
Address review feedback to prevent stale avatar copies at the source rather than only sweeping them afterwards. Previously the write-time prune sat on the hot render path inside maybe_cache(), adding a redundant filesystem check to every cache hit. Override Avatar::cache() instead so the prune runs only when a fresh download actually writes a new file. When an actor changes their icon URL, the previous hash file is removed in the same call, so no second copy accumulates. Media and Emoji caches are unaffected since they do not override cache(). maybe_cache() returns to a single get_or_cache() call: cache hits stay one directory read, with zero prune cost. The daily cron still drains the existing backlog on live sites and reclaims orphaned actor directories that write-time pruning cannot reach. Add a regression test proving that caching a new avatar URL removes the previous version while keeping the new one.
|
Thanks for the review. I moved the stale-file cleanup to the moment a new avatar is actually written, so a second copy is never added in the first place. When a remote actor changes their icon URL, the cache used to leave the previous hash file orphaned. The fix overrides Avatar::cache() so any older avatar version is removed in the same call the new one is downloaded. It is avatar-specific because media entity directories legitimately hold multiple distinct images. The hot render path (a maybe_cache() hit) stays a single get_or_cache() call. The daily cron now only drains the backlog already on disk and reclaims orphaned actor directories. A regression test (test_new_avatar_url_prunes_previous_version) proves the old version is removed when a new URL is cached. I pushed the updated branch. Happy to adjust if you would like it done differently. |
Fixes #3583
Proposed changes:
Other information:
New unit tests added for the hash resolver, the file prune, write-time pruning when a new avatar replaces an old one, and the cron cleanup (orphan handling, batching, and the re-entrancy lock). A changelog entry file is included in the branch.
Testing instructions:
wp-content/uploads/activitypub/actors/.wp cron event list | grep activitypub_cleanup_actor_cache(recurrencedaily).wp cron event run activitypub_cleanup_actor_cache --due-now); confirm its directory is removed.Changelog entry
Changelog Entry Details
Significance
Type
Message
Prevented unused copies of remote profile pictures from accumulating on your server and added automatic cleanup for leftover cached avatars.