Skip to content

fix: release locks and delete preferences owned by deleted users - #18202

Open
santichausis wants to merge 1 commit into
payloadcms:mainfrom
santichausis:fix/delete-user-locks-and-preferences
Open

santichausis wants to merge 1 commit into
payloadcms:mainfrom
santichausis:fix/delete-user-locks-and-preferences

Conversation

@santichausis

Copy link
Copy Markdown

What?

Deleting a user now releases the document locks that user holds and deletes their preferences, in deleteByID and in both bulk delete paths.

Why?

Two things were left behind when an auth collection document was deleted (#18200):

  1. Preferences were never deleted on SQL adapters. deleteUserPreferences ran after db.deleteOne/db.deleteMany. Its query matches on user.value, but by then the payload_preferences_rels row had already cascaded away with the user, so it matched nothing and preference rows piled up.
  2. Locks held by the user were never released, on any adapter. Only locks on the deleted document were cleared. On SQL the payload_locked_documents_rels row cascades away too, leaving a lock with no user. Every lock check compares lock.user?.value with the current user, so that ownerless lock counts as "locked by someone else": the document can't be updated with overrideLock: false and shows as locked in the admin until the lock expires (5 minutes by default). On 3.x the edit view crashes instead (extractID(undefined) in getIsLocked).

How?

  • New deleteUserDocumentLocks helper next to the existing bulk lock helpers in checkDocumentLockStatus.ts. It deletes locks whose user matches the deleted documents, and only runs for auth collections.
  • deleteByID: deleteUserPreferences and deleteUserDocumentLocks now run before db.deleteOne, inside the same transaction.
  • delete: both run before the document delete in the per-document path (bulkOperationsSingleTransaction) and in the batched path. The trailing deleteUserPreferences call is removed. As a side effect, preferences are now only removed for documents that were actually deleted. Before, they were also removed for documents skipped because they were locked or failed in beforeDelete.

Trade-off: because the cleanup now runs before the delete, a failed deleteMany on an adapter without transactions (e.g. MongoDB without a replica set) leaves the users in place with their preferences already removed. Running it afterwards isn't possible on SQL, and adapters with transactions roll it back.

I left reader-side handling of ownerless locks (getIsLocked, _userEditing in find/findByID/findOne, handleFormStateLocking) out of this PR. With this change those locks are no longer created, and any that already exist expire on their own.

Tests

New test/locked-documents/delete-user.int.spec.ts: deleting a user by ID releases their lock (and the locked document can be updated again), deleting by ID removes their preferences, and a bulk delete removes the deleted user's lock and preferences while keeping another user's.

  • All three fail without the fix on SQLite and pass with it.
  • locked-documents + auth int suites pass on SQLite (114 tests), including with bulkOperationsSingleTransaction forced on to cover the per-document path.
  • I couldn't run MongoDB/Postgres locally (no Docker), so I'm relying on CI for those.

Fixes #18200

@santichausis

Copy link
Copy Markdown
Author

Could a maintainer approve the workflow run here? The ci run is sitting in action_required, so the integration tests haven't run on this PR yet.

I could only run the suites on SQLite locally (no Docker), so the MongoDB and Postgres runs are the part I can't verify myself — locked-documents and auth pass there, and the three new tests fail without the fix.

Happy to rebase or split anything out if that helps. Thanks!

Deleting an auth collection document ran deleteUserPreferences after the
document itself was deleted. On SQL adapters the preferences' user
relationship rows cascade away with the user, so the query matched nothing
and the preferences were left behind. Locks held by the user were never
released at all, leaving behind a lock owned by no one that blocks other
users until it expires.

Both are now removed before the document is deleted, in deleteByID and in
both bulk delete paths.

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.

Deleting a user leaves their document locks and preferences behind (Postgres): an ownerless lock crashes the edit view

1 participant