Repository navigation
fix: invalidate cached permission lists when user group memberships change - #7938
Conversation
| _clear_permission_cache(user_ids) | ||
| return True | ||
|
|
||
|
|
There was a problem hiding this comment.
Overall the diff is syntactically fine, but there are a few correctness and performance concerns.
Critical issues
-
Missing cache invalidation in
add_or_edit_user_group_relation@transaction.atomic def add_or_edit_user_group_relation(user, user_group_ids): UserGroupRelation.objects.filter(user=user).delete() ...
This changes a user’s group membership but does not clear that user’s permission cache. Add:
transaction.on_commit(lambda: _clear_permission_cache([user.id]))
-
Cache is cleared inside the DB transaction, before commit
Indelete,add_member, andremove_member,_clear_permission_cache(...)is called before the surrounding transaction commits, if these methods run insidetransaction.atomicor an outer atomic block. A concurrent request can read the old DB state after the cache was cleared and repopulate stale permissions. Usetransaction.on_commit:transaction.on_commit(lambda: _clear_permission_cache(member_user_ids))
Do this after capturing the IDs and after the DB write. This is especially important for group deletion and member add/remove.
-
Group deletion may invalidate incomplete member set
deleteonly collects users fromSystemUserGroupRelation:SystemUserGroupRelation.objects.filter(group_id=self.group.id)
If permissions also depend on
UserGroupRelationor other relation models affected byself.group.delete(), those users will keep stale permissions. Confirm all membership tables that can affectPERMISSION_LISTand invalidate all of them.
Performance / optimization
-
Avoid one cache delete per user
Use a set to deduplicate andcache.delete_manywhen supported:def _clear_permission_cache(user_ids): user_ids = set(user_ids or []) if not user_ids: return version = Cache_Version.PERMISSION_LIST.get_version() keys = [ Cache_Version.PERMISSION_LIST.get_key(user_id=user_id) for user_id in user_ids ] cache.delete_many(keys, version=version)
If the cache backend does not support
delete_many(..., version=...), keep the loop but still deduplicate and consider chunking. -
Large groups can make transactions slow
For group deletion,member_user_idscan be very large. Loading all IDs and deleting cache keys one-by-one inside or immediately around a transaction can increase lock time and latency. Consider chunking the invalidation and usingdelete_many. -
remove_memberdoes SELECT then DELETErelations = SystemUserGroupRelation.objects.filter(...) user_ids = list(relations.values_list("user_id", flat=True)) relations.delete()
This is two queries and is not protected against concurrent changes unless the caller is atomic. If concurrent membership changes matter, wrap it in
transaction.atomic()and possibly useselect_for_update(). In most cases this is probably acceptable, but worth confirming.
Other checks
-
Verify cache key granularity
_clear_permission_cacheassumes permissions are cached per user via:Cache_Version.PERMISSION_LIST.get_key(user_id=user_id)
If the key can also include workspace/tenant/group scope, clearing only the user-level key may leave other permission cache entries stale.
-
Cache failure behavior
Ifcache.delete/delete_manyraises inside an atomic block, the DB transaction may roll back. If moved totransaction.on_commit, the DB change is already committed and the request may still fail. Decide whether cache invalidation failures should fail the API request, be logged, or be retried. Because this affects permissions, silently ignoring failures is risky. -
Ensure all permission-changing paths invalidate
Besides membership add/remove/delete, any operation that changes a group’s roles/permissions should also invalidate all affected users’PERMISSION_LISTcache entries.
|
|
||
| return None | ||
|
|
||
|
|
There was a problem hiding this comment.
Here's my review of the code snippet:
Context assumption: This appears to be inside set_user_groups(user_id, instance) in a Django project, right after a database query/update (for group_id in user_group_ids) and before return None.
Issues / Observations
1. Cache key/version semantics — likely bug or at least fragile
version = Cache_Version.PERMISSION_LIST.get_version()
cache.delete(Cache_Version.PERMISSION_LIST.get_key(user_id=user_id), version=version)- You fetch the current version and delete with it. If the version was just bumped (e.g., as part of invalidation elsewhere), you may be deleting under the new namespace while consumers read from an older one — or vice versa depending on ordering.
- More importantly:
get_version()is called twice at different times (once here, once wherever readers call it). If the version changes between your read and delete, you'll delete the wrong entry. Consider whether this should be a bump (increment version) rather than a delete, which is generally safer for cache invalidation across namespaces.
2. Missing exception handling
A Redis/Memcached failure will raise and abort set_user_groups even though the DB state is already consistent. Consider:
try:
cache.delete(...)
except Exception:
logger.exception("Failed to invalidate permission list cache for user %s", user_id)Or use a wrapper that swallows/logs cache errors.
3. Placement / transaction safety
- If
set_user_groupsruns inside a DB transaction, deleting the cache before commit can allow another request to repopulate the cache with stale data (read-your-writes race). Prefertransaction.on_commit(lambda: cache.delete(...)). - Since it's just before
return None, confirm there are no early returns above that would bypass invalidation. It'd be cleaner to put invalidation as part of a single exit path.
4. Redundant return None
Implicit in Python; harmless but unnecessary. Minor style.
5. Naming / typos
Cache_Versionuses non-PEP-8 casing (should beCacheVersionif it's a class/enum).- Confirm
PERMISSION_LIST.get_key(user_id=user_id)actually scopes byuser_id— otherwise you'd be nuking a shared key for all users, which would be a correctness/perf concern.
6. No-op write when nothing changed
If user_group_ids is empty or unchanged, the delete still fires. If set_user_groups is called frequently, consider skipping invalidation when the group set didn't change (compare before/after), to reduce cache churn.
7. Concurrency
Multiple workers may call this concurrently; that's fine for a single-key delete, but if the reader side does get → miss → recompute → set, you should verify the recompute path also reads the current version to avoid resurrecting stale entries.
Suggested revision
def set_user_groups(user_id, instance):
... # existing logic
key = Cache_Version.PERMISSION_LIST.get_key(user_id=user_id)
def _invalidate():
try:
cache.delete(key, version=Cache_Version.PERMISSION_LIST.get_version())
except Exception:
logger.exception("permission_list cache invalidation failed for user=%s", user_id)
transaction.on_commit(_invalidate)(Only wrap in on_commit if already within a transaction; otherwise call directly.)
Summary: The change is functionally reasonable but has three real risks — version race, unhandled cache exceptions, and pre-commit invalidation. Address those and it's solid.
fix: invalidate cached permission lists when user group memberships change