Repository navigation
fix(presences): delete a user's presence when the user is blocked or deleted - #14397
Conversation
…deleted A REST presence outlived its user: blocking or deleting the user left it readable and sent no event until it expired. Blocking a user (server or self) and deleting a user (server or account) now delete their presence and send presences.[presenceId].delete to subscribers.
|
Review complete. 🟡 2 medium 💬 Inline comments (2)
This PR introduces src/Appwrite/Presences/State.php::deleteForUser(), which batch-deletes all presence documents for a user, purges the presence list cache, and fires realtime triggers, then wires it into the legacy account and Users module endpoints so presence state no longer outlives a deleted or blocked user.
Reviewed commit: 0d1f766 |
🔵 Tier A · Mergeable after minor fixes
This PR removes a user's stored REST presences when their account is blocked or deleted, and repeats user-presence cleanup in deferred delete processing; cleanup purges list cache, adjusts usage, and emits per-presence realtime delete events. It also revokes MQTT connections in the relevant block/delete flows and makes a concurrent presence update/delete return Latest changes: The newest commit updates direct
Note Not approving while a bug finding is open: Swallowed cleanup failures leave no retry path Fix with agent prompt### Issue 1
src/Appwrite/Presences/State.php:224
**Swallowed cleanup failures leave no retry path**
If `deleteDocuments()` throws, for example during a transient database failure, this catch lets the block/delete request succeed even though the presence may remain, and it schedules no retry. For a deleted user, this cleanup cannot be retried through the same endpoint, so the presence can stay readable until expiry.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 8
⏳ Still open from earlier reviews · 1
Merged |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
There was a problem hiding this comment.
Adds presence cleanup to user deletion, blocking, and logout paths via a new State::deleteForUser() that purges presence rows, flushes the list cache, and emits realtime triggers.
Key findings
- 🟡 Presence deletes skip the events queue — State.php:213
- 🟡 Harden
deleteForUseragainst purge failures — State.php:189
| $queueForRealtime | ||
| ->reset() | ||
| ->setProject($project) | ||
| ->setUser($user) | ||
| ->setEvent('presences.[presenceId].delete') | ||
| ->setParam('presenceId', $presence->getId()) | ||
| ->setPayload($presence->getArrayCopy()) | ||
| ->trigger(); | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Presence deletes skip the events queue
The new State::deleteForUser publishes only to queueForRealtime (src/Appwrite/Presences/State.php:213-221), bypassing State::triggerEvent, which is how every other presence-delete path notifies consumers — the realtime disconnect cleanup uses it at app/realtime.php:1564, and the Presences REST delete route emits the event through its event/audits labels. As a result, user deletion, account deletion, and user blocking emit the presence delete to realtime subscribers but never to webhooks, functions events, audits, or the events worker. Integrations subscribed to presences.[presenceId].delete will not observe these forced presence removals.
📋 Prompt for AI Agents
In src/Appwrite/Presences/State.php around lines 180-222, make deleteForUser accept an additional Appwrite\Event\Event $queueForEvents parameter and, inside the foreach ($presences ...) loop, replace the direct $queueForRealtime->reset()->...->trigger() chain with $this->triggerEvent($queueForEvents, $queueForRealtime, $project, $user, 'presences.[presenceId].delete', $presence) so the events queue (webhooks, audits, events worker) is notified the same way as app/realtime.php:1564 and the Presences REST delete route. Then update the four callers to pass their queueForEvents dependency: src/Appwrite/Platform/Modules/Users/Http/Users/Delete.php (~line 83), src/Appwrite/Platform/Modules/Users/Http/Users/Status/Update.php (~line 76), and app/controllers/api/account.php (~line 600 and ~line 4095); each action already injects queueForEvents.
There was a problem hiding this comment.
Keeping this realtime-only on purpose. triggerEvent() does not enqueue queueForEvents either; it only uses it to fill queueForRealtime->from(), so the realtime close path is realtime-only too, and the expiry cleanup sends nothing. The user change itself still goes through the events queue as users.[userId].update.status or users.[userId].delete, which is what webhooks and functions receive.
There was a problem hiding this comment.
Thanks for the explanation, but it doesn't fully hold up under scrutiny.
You're right that triggerEvent uses queueForEvents to populate queueForRealtime->from() (for subscriber matching), but triggerEvent also calls $queueForEvents->trigger() — which is what dispatches to webhooks, functions, audits, and the events worker. The realtime disconnect path at app/realtime.php:1564 does call triggerEvent, so it does enqueue the event. The argument that the parent users.[userId].delete or users.[userId].update.status event is sufficient isn't correct for consumers subscribed specifically to presences.[presenceId].delete — those subscribers will never observe forced removals via this path.
The current diff still shows only $queueForRealtime->reset()->...->trigger() with no queueForEvents parameter or triggerEvent call, so the finding remains unresolved.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…he request The user is already blocked or deleted when its presence is cleaned up, so an error there must not turn the request into a 500.
| ->setPayload($presence->getArrayCopy()) | ||
| ->trigger(); | ||
| } | ||
| } catch (Throwable $th) { |
There was a problem hiding this comment.
Swallowed cleanup failures leave no retry path
If deleteDocuments() throws, for example during a transient database failure, this catch lets the block/delete request succeed even though the presence may remain, and it schedules no retry. For a deleted user, this cleanup cannot be retried through the same endpoint, so the presence can stay readable until expiry.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Presences/State.php
Line: 224
Comment:
**Swallowed cleanup failures leave no retry path**
If `deleteDocuments()` throws, for example during a transient database failure, this catch lets the block/delete request succeed even though the presence may remain, and it schedules no retry. For a deleted user, this cleanup cannot be retried through the same endpoint, so the presence can stay readable until expiry.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · error-handling · Reply if this doesn't apply.
There was a problem hiding this comment.
For deleted users, 00b6c60 makes the deletes worker the second attempt: deleteUser() now removes leftover presences, as it does sessions and tokens. Checked with an API on main and a deletes worker on this branch: the 4 of 8 jobs that worker took removed their presences, the 4 the main worker took did not. Blocking has no worker job, so a failed cleanup there leaves the presence until its expiresAt, as before this PR, and is recorded on the span.
There was a problem hiding this comment.
You’re right that the deletes worker retries removing presence rows for deleted users. That does not cover blocking: both status-update paths call deleteForUser(), but schedule no delete job (user status route, account status route). If cleanup fails, the catch only records the error and returns (State::deleteForUser); the blocked user’s presence can therefore remain until expiry unless the status update is explicitly retried. The finding still applies to blocked users.
Deleting a user removes its presence during the request. The deletes worker now removes any presence left behind, as it already does for sessions and tokens, so a failure during the request does not leave it until expiry.
The deletes worker removed leftover presences without purging the presence list cache, updating usage or telling subscribers. It now uses the same PresenceState::deleteForUser(), so a presence it removes also sends presences.[presenceId].delete. The worker injects queueForRealtime for it.
# Conflicts: # app/controllers/api/account.php # src/Appwrite/Platform/Modules/Users/Http/Users/Delete.php # src/Appwrite/Platform/Modules/Users/Http/Users/Status/Update.php # tests/e2e/Services/Presences/PresenceBase.php
…licate 404 check main already has
What does this PR do?
PresenceState::deleteForUser(), which deletes a user's presence, purges the presence list cache, decrements the presence usage metric and sendspresences.[presenceId].deleteto realtime subscribers.PATCH /v1/users/:userId/statuswithstatus: false, andPATCH /v1/account/status) and when a user is deleted (DELETE /v1/users/:userId,DELETE /v1/account). Before, a presence written over REST stayed readable, with no event, until itsexpiresAt.Deletes::deleteUser()also callsdeleteForUser(), so a presence left behind by a failed cleanup during the request is removed by the deletes worker, with the same cache purge, usage update and delete event. The worker now injectsqueueForRealtime; Cloud's Deletes worker callsparent::action()positionally, so its next server bump must inject and forward it.testUpdateUserStatusDeletesPresenceandtestDeleteUserDeletesPresencecover the block and delete paths.Verification
A user upserts a REST presence with
read("any"); an observer is subscribed topresences. Then, on a local stack:GET200, no eventGET404, onepresences.<id>.deletePATCH /v1/account/status)GET200, no eventGET404, one delete eventGET200, no eventGET404, one delete eventA presence held by a realtime connection is also removed when its user is blocked, and the observer gets exactly one delete event (3 of 3 runs), although the realtime server closes that connection at the same time. The deletes worker change was checked with an API on main (no cleanup during the request) and a deletes worker on this branch: the 4 of 8 user deletions that worker took removed their presences and sent one delete event each, the 4 taken by the main worker did neither.
The two new tests fail on main (
GET200) and pass here. E2EtestUpdateUserStatus,testDeleteUser,testBlockedAccount,testSelfBlockedAccount,testDeleteAccountandtestDeleteAccountWith*pass against this branch, as doPresenceCustomServerTestandPresenceCustomClientTest. Pint and PHPStan pass on the changed files.Fixes #14344