Skip to content

Fix Scheduler::cleanup_remote_actors to re-fetch with cache bypassed (#3980) - #4072

Open
noruzzamans wants to merge 1 commit into
Automattic:trunkfrom
noruzzamans:fix/3980-cleanup-remote-actors-refetch
Open

noruzzamans wants to merge 1 commit into
Automattic:trunkfrom
noruzzamans:fix/3980-cleanup-remote-actors-refetch

Conversation

@noruzzamans

Copy link
Copy Markdown
Contributor

Fixes #3980

Proposed changes:

In Scheduler::cleanup_remote_actors(), get_remote_metadata_by_actor() was previously called, which short-circuits to the locally stored ap_actor record whenever one exists via Remote_Actors::fetch_by_various(). Because of this, faulty actors always ended up in the success branch, rewriting stale local data and clearing errors, while the Tombstone and 5-error branches never executed.

This PR addresses all aspects outlined in #3980:

  1. Re-fetch with cache bypassed: Replaced get_remote_metadata_by_actor() with Proxy::get( $guid, array( 'cached' => false ) ) to force a genuine remote metadata fetch.
  2. Tombstone vs 404 distinction: Only delete immediately on HTTP 410 Gone or a confirmed Tombstone object. HTTP 404 is now treated as a regular error and recorded via Remote_Actors::add_error(), ensuring transient server misconfigurations do not prematurely purge follower relations.
  3. Guard against identity drift: If Proxy::get() returns a document declaring an id that differs from $actor->guid, the cleanup skips the record instead of colliding or corrupting the identity.
  4. In-place record update: When valid metadata is returned, updates the known actor via Remote_Actors::update( $actor->ID, $meta ) rather than upsert(), eliminating redundant get_by_uri() database queries.
  5. Scheduled cleanup arguments: Passed the actor post ID ($actor->ID) instead of $actor->guid to activitypub_delete_remote_actor_interactions and activitypub_delete_remote_actor_posts, matching what both action handlers have expected since Refactor avatar handling to use remote actor meta #2373.
  6. Unit test coverage: Added comprehensive tests in Test_Scheduler covering:
    • Error threshold (>= 5 errors) scheduling interaction and post deletions with post ID and deleting the actor.
    • HTTP 404 error incrementation without immediate deletion.
    • HTTP 410 immediate actor deletion and deletion event scheduling.
    • Tombstone object immediate deletion.
    • Successful refetch updating actor data and clearing errors.
    • Identity drift (ID mismatch) skip behavior.

Other information:

  • Have you written new tests for your changes, if applicable?

Testing instructions:

  1. Run PHPUnit tests:
    vendor/bin/phpunit --filter Test_Scheduler
  2. Verify PHPCS and PHPStan analysis:
    vendor/bin/phpcs includes/class-scheduler.php tests/phpunit/tests/includes/class-test-scheduler.php
    composer run-script analyze
  3. Verify via WP-CLI:
    • Create or mark a remote actor as faulty.
    • Simulate an HTTP 404 response on remote refetch; run wp cron event run activitypub_cleanup_remote_actors and confirm error count increments while actor remains.
    • Simulate an HTTP 410 response or Tombstone object; run the cleanup cron and confirm actor is deleted and interactions/posts deletion events are scheduled with the post ID.

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch

Type

  • Fixed

Message

Ensure remote actor cleanup re-fetches metadata with cache bypassed, deletes only on 410 or confirmed tombstones, and schedules cleanups with post ID.

…utomattic#3980)

In Scheduler::cleanup_remote_actors(), replace get_remote_metadata_by_actor() with Proxy::get( $guid, array( 'cached' => false ) ) so that faulty actors actually re-fetch remote data instead of short-circuiting to stale cached ap_actor posts.

Also address related cleanup behaviors per Automattic#3980:
- Only delete immediately on HTTP 410 Gone or a confirmed Tombstone object. Treat HTTP 404 as a regular error so transient network errors do not prematurely purge followers.
- Guard against identity drift if the remote response returns an id differing from the actor guid.
- Update the known actor in place with Remote_Actors::update() instead of upsert(), avoiding redundant queries.
- Pass the actor post ID ($actor->ID) instead of the guid to the scheduled deletion events (activitypub_delete_remote_actor_interactions and activitypub_delete_remote_actor_posts), matching their expected integer post ID.
- Add unit test coverage in Test_Scheduler for 404 error incrementation, 410/Tombstone immediate deletion, threshold deletion with post IDs, successful recovery, and ID mismatch guards.

Fixes Automattic#3980

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remote actor cleanup never re-fetches, so faulty actors are never refreshed or removed

1 participant