Skip to content

fix: invalidate cached permission lists when user group memberships change - #7938

Merged
wxg0103 merged 1 commit into
v3from
pr@v3@fix_clean_cache
Nov 16, 2026
Merged

wxg0103 merged 1 commit into
v3from
pr@v3@fix_clean_cache

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

fix: invalidate cached permission lists when user group memberships change

@wxg0103
wxg0103 merged commit 206aa4b into v3 Nov 16, 2026
3 of 4 checks passed
@wxg0103
wxg0103 deleted the pr@v3@fix_clean_cache branch November 16, 2026 08:19
_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.


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.

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.

2 participants