Repository navigation
api: delete users in the background - #114
Merged
Merged
Conversation
Deleting a large account ran every collection's delete in one Mongo transaction, which outlived Mongo's time limit and the request timeout. The delete now marks the user's settings with DeletedAt and unregisters the user from Identity, and UserPurgeJob purges the data after a grace period, one idempotent step at a time, retrying until it's done. A user that still exists in Identity is unmarked instead of purged. A deleted user's open sync connections are cut off by a hub filter, which also takes over the sync access check, and unregistering clears the user's cached token introspection. Identity drops delete_user and adds user_exists, so it must be deployed before this API.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved issues can permanently prevent data purging, allow late uploads to recreate deleted data, and break account deletion during rollout, while existing-connection sync protection lacks test coverage.
4 open issues
What changed in this PR
Moves account data deletion out of the request’s MongoDB transaction into a background purge job, reducing timeouts for large accounts while preserving the deletion endpoint.
Changes:
- Marks accounts for deletion and schedules non-transactional cleanup after a grace period.
- Updates Identity unregistration to end sessions and invalidate cached tokens.
- Adds sync authorization filtering and tests for purge behavior and deleted-user access.
| File | Description |
|---|---|
| tests/Streetwriters.Identity.Tests/TokenContractTests.cs | Updates unregistration expectations and removes the old deletion test. |
| tests/Notesnook.API.Tests/UserPurgeJobTests.cs | Tests purge isolation, grace periods, retries, and Identity failures. |
| tests/Notesnook.API.Tests/SyncV2HubTests.cs | Tests connection authorization and deleted-user rejection. |
| tests/Notesnook.API.Tests/ApiErrorResponseTests.cs | Updates the deletion service stub. |
| Streetwriters.Identity/Services/UserAccountService.cs | Adds existence checks and session-aware unregistration. |
| Streetwriters.Identity/Services/ScopedUserAccountService.cs | Exposes the new existence check through scoped delegation. |
| Streetwriters.Identity/Services/PersistedGrantSessionStore.cs | Returns ended-session keys for cache invalidation. |
| Streetwriters.Identity/OpenIddictEngine/OpenIddictSessionStore.cs | Returns modern and legacy session cache keys. |
| Streetwriters.Identity/Legacy/LegacyGrants.cs | Collects grant keys before deletion. |
| Streetwriters.Identity/Interfaces/ISessionStore.cs | Updates the session termination return contract. |
| Streetwriters.Common/Interfaces/IUserAccountService.cs | Replaces the deletion RPC with an existence check. |
| Notesnook.API/Startup.cs | Registers the sync filter and recurring purge job. |
| Notesnook.API/Services/UserService.cs | Marks accounts before unregistering them from Identity. |
| Notesnook.API/Services/MongoIndexesHostedService.cs | Indexes deletion timestamps for purge selection. |
| Notesnook.API/Models/UserSettings.cs | Adds the deletion timestamp. |
| Notesnook.API/Jobs/UserPurgeJob.cs | Implements delayed, retryable account data cleanup. |
| Notesnook.API/Hubs/SyncV2Hub.cs | Moves connection authorization into the filter. |
| Notesnook.API/Hubs/SyncAuthorizationHubFilter.cs | Checks deletion status on connections and method calls. |
| Notesnook.API/Controllers/UsersController.cs | Removes the extended deletion request timeout. |
Review effort: Balanced
Comment on lines
+19
to
+20
| // longer than a cached token's introspection (30m) and a presigned upload URL (1h), so writes made with either land before the purge | ||
| public static readonly TimeSpan GracePeriod = TimeSpan.FromMinutes(75); |
Comment on lines
+52
to
+54
| await repositories.UsersSettings.Collection.UpdateOneAsync( | ||
| Builders<UserSettings>.Filter.Eq((s) => s.UserId, userId), | ||
| Builders<UserSettings>.Update.Unset((s) => s.DeletedAt)); |
Comment on lines
+23
to
+24
| await EnsureNotDeletedAsync(invocationContext.Context, invocationContext.ServiceProvider); | ||
| return await next(invocationContext); |
Comment on lines
+11
to
+12
| [WampProcedure("co.streetwriters.identity.users.user_exists")] | ||
| Task<bool> UserExistsAsync(string userId); |
Covers uploads admitted by a cached token in the grace period, keeps a mark set by a retried delete, tests that open connections are cut off, and keeps delete_user for API instances that predate this change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Deleting a large account timed out: every collection's delete ran in one Mongo transaction, which outlived Mongo's 60s limit and the request timeout.
Changes
POST /users/deletemarks the user'suser_settingswithDeletedAt(upserted), unregisters the user from Identity and returns. Same endpoint, form and response, so old clients are unaffected.UserPurgeJob(every 5 min) purges marked users after a 150 min grace period, covering uploads a token cached at deletion (30 min) can still start: a 5 GiB direct upload (90 min) or a presigned upload URL (1 h). Each step is idempotent and non-transactional, anduser_settingsis deleted last, so a failed purge is retried. A user that still exists in Identity is unmarked instead of purged.SyncAuthorizationHubFiltercuts off a deleted user's open sync connections (hub connections outlive their token) and takes over the sync access check fromSyncV2Hub.OnConnectedAsync.UnregisterUserAsyncends sessions before deleting, throws on failure or for another client's user, and clears the API's cached introspection of the ended tokens.user_existsis added;delete_userstays for API instances that predate this change, to be removed in a later release.Deploy order
Identity first (
user_exists,unregister_user; old APIs keep working throughdelete_user), then this API.Testing
UserPurgeJobTests,SyncV2HubTests,ApiErrorResponseTests,TokenContractTests.