Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/users/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,9 @@ def set_user_groups(user_id, instance):
for group_id in user_group_ids
)

version = Cache_Version.PERMISSION_LIST.get_version()
cache.delete(Cache_Version.PERMISSION_LIST.get_key(user_id=user_id), version=version)

return None


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_groups runs inside a DB transaction, deleting the cache before commit can allow another request to repopulate the cache with stale data (read-your-writes race). Prefer transaction.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_Version uses non-PEP-8 casing (should be CacheVersion if it's a class/enum).
  • Confirm PERMISSION_LIST.get_key(user_id=user_id) actually scopes by user_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.

Expand Down
23 changes: 21 additions & 2 deletions apps/users/serializers/user_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import uuid_utils.compat as uuid
from collections import defaultdict
from django.core.cache import cache
from django.db import transaction
from django.db.models import Count
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from common.auth.constants.role_constants import RoleConstants
from common.constants.cache_version import Cache_Version
from common.database_model_manage.database_model_manage import DatabaseModelManage
from common.db.search import page_search
from common.exception.app_exception import AppApiException, NotFound404
from system_manage.models import UserGroup, UserGroupRelation
from users.models.user_group import SystemUserGroup, SystemUserGroupRelation


def _clear_permission_cache(user_ids):
"""Invalidate the cached permission list for users whose group membership changed."""
if not user_ids:
return
version = Cache_Version.PERMISSION_LIST.get_version()
for user_id in user_ids:
cache.delete(Cache_Version.PERMISSION_LIST.get_key(user_id=user_id), version=version)


@transaction.atomic
def add_or_edit_user_group_relation(user, user_group_ids):
UserGroupRelation.objects.filter(user=user).delete()
Expand Down Expand Up @@ -99,7 +110,11 @@ def delete(self, *, with_valid=True):
if with_valid:
self.is_valid(raise_exception=True)

member_user_ids = list(
SystemUserGroupRelation.objects.filter(group_id=self.group.id).values_list("user_id", flat=True)
)
self.group.delete()
_clear_permission_cache(member_user_ids)
return True

class Query(serializers.Serializer):
Expand Down Expand Up @@ -181,6 +196,7 @@ def add_member(self, with_valid=True):
for user_id in to_add
]
)
_clear_permission_cache(to_add)
return True


Expand All @@ -206,11 +222,14 @@ def remove_member(self, with_valid=True):
self.is_valid(raise_exception=True)
data = self.validated_data
relation_ids = data["group_relation_ids"]
SystemUserGroupRelation.objects.filter(
relations = SystemUserGroupRelation.objects.filter(
id__in=relation_ids,
group_id=data["id"],
group__workspace_id=data["workspace_id"],
).delete()
)
user_ids = list(relations.values_list("user_id", flat=True))
relations.delete()
_clear_permission_cache(user_ids)
return True


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the diff is syntactically fine, but there are a few correctness and performance concerns.

Critical issues

  1. 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]))
  2. Cache is cleared inside the DB transaction, before commit
    In delete, add_member, and remove_member, _clear_permission_cache(...) is called before the surrounding transaction commits, if these methods run inside transaction.atomic or an outer atomic block. A concurrent request can read the old DB state after the cache was cleared and repopulate stale permissions. Use transaction.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.

  3. Group deletion may invalidate incomplete member set
    delete only collects users from SystemUserGroupRelation:

    SystemUserGroupRelation.objects.filter(group_id=self.group.id)

    If permissions also depend on UserGroupRelation or other relation models affected by self.group.delete(), those users will keep stale permissions. Confirm all membership tables that can affect PERMISSION_LIST and invalidate all of them.

Performance / optimization

  1. Avoid one cache delete per user
    Use a set to deduplicate and cache.delete_many when 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.

  2. Large groups can make transactions slow
    For group deletion, member_user_ids can 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 using delete_many.

  3. remove_member does SELECT then DELETE

    relations = 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 use select_for_update(). In most cases this is probably acceptable, but worth confirming.

Other checks

  1. Verify cache key granularity
    _clear_permission_cache assumes 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.

  2. Cache failure behavior
    If cache.delete/delete_many raises inside an atomic block, the DB transaction may roll back. If moved to transaction.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.

  3. 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_LIST cache entries.

Expand Down
Loading