Skip to content

Creating and modifying authorization policies is not thread safe #9711

Closed

Description

@lhotari

Describe the bug

Creating and modifying authorization policies is not thread safe. It leads to unexpected NullPointerExceptions for example and could lead to authorization data corruption.

To Reproduce

The flaky test #9709 is one example of the problem.

Expected behavior

Creating and modifying authorization policies should be thread safe.

Observations

It looks like the org.apache.pulsar.common.policies.data.AuthPolicies data structures are modified without a way to ensure thread safety and data consistency.

Activity

  1. added
    type/bug
    on Feb 25, 2021
  2. eolivelli commented on Feb 25, 2021

    @eolivelli
    Contributor

    Good catch @lhotari

    I believe this is a blocker issue.

    @wolfstudy @codelipenghui can we fix it for 2.7.1 ?

  3. 315157973 commented on Feb 26, 2021

    @315157973
    Contributor

    When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?

  4. lhotari commented on Feb 26, 2021

    @lhotari
    MemberAuthor

    When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?

    that part is not the concern. The concerns are the maps that are contained in the cached AuthPolicies entry. They are mutated without synchronization on both read & write access. Mutation for example here:

    policies.auth_policies.namespace_auth.remove(role);

    Accessed here:
    permissions.put(role, auth.namespace_auth.get(role));

  5. 315157973 commented on Feb 26, 2021

    @315157973
    Contributor

    When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?

    that part is not the concern. The concerns are the maps that are contained in the cached AuthPolicies entry. They are mutated without synchronization on both read & write access. Mutation for example here:

    policies.auth_policies.namespace_auth.remove(role);

    Accessed here:

    permissions.put(role, auth.namespace_auth.get(role));

    Could you give an example like the following?

    //MetadataCacheImpl.java 175
    return store.put(path, newValue, Optional.of(expectedVersion)).thenAccept(stat -> {
                            objCache.put(path,
                                    FutureUtils.value(Optional.of(new SimpleImmutableEntry<T, Stat>(newValueObj, stat))));
                        });
    

    Normal scene:
    t1: thread 1 updates zk successfully
    t2: thread 1 updates the cache
    t3: thread 2 updates zk successfully
    t4: Thread 2 updates the cache

    Concurrent scene:
    t1: thread 1 updates zk successfully
    t2: Thread 2 updates zk, which fails because the version is different
    t3: Thread 1 updates the cache

  6. lhotari commented on Feb 26, 2021

    @lhotari
    MemberAuthor

    Could you give an example like the following?

    the problem is not about certain sequence of events or interactions with zk. It's about unsafe mutation and access of the TreeMap / HashMap instances contained in the AuthPolicies class. A TreeMap or HashMap can only be safely mutated when all access (mutation and reading) happens under the same object monitor. Violating this rule will lead to nondeterministic behavior.

  7. lhotari commented on Mar 1, 2021

    @lhotari
    MemberAuthor

    It seems that ReplicatorTest triggers some of the thread safety issues in updating policies. Some of the reported issues about ReplicatorTest flakiness.

  8. codelipenghui commented on Mar 2, 2021

    @codelipenghui
    Contributor

    @eolivelli we don't want to block the 2.7.1 release, instead we can accelerate the 2.7.2 release. Here is the release proposal that we have discussed before https://github.com/apache/pulsar/wiki/PIP-47%3A-Time-Based-Release-Plan. If the problem is not introduced after 2.7.0 and it does not break the existing behavior, it's better to do not to block the 2.7.1 release.

  9. eolivelli commented on Mar 2, 2021

    @eolivelli
    Contributor

    @codelipenghui I am fine with keeping it only on master branch.

  10. lhotari commented on Mar 8, 2021

    @lhotari
    MemberAuthor

    On a busy system, unsafe HashMap mutation and access will eventually lead to infinite loops and 100% CPU core usage when the HashMap data structures get corrupted.
    This problem is demonstrated and explained in this article, in the section "HashMap infinite looping problem replication".

    I might be missing something, but I can see that the Pulsar policy creation and modification contains quite a few locations where there is unsafe HashMap/TreeMap mutation and access. @merlimat PTAL

  11. lhotari commented on Mar 8, 2021

    @lhotari
    MemberAuthor

    @lhotari How about change HashMap to ConcurrentHashMap, and do not update the cache, only invalidate the cache .

    +1, I guess ConcurrentHashMap is one possibility and probably leads to the simplest fix to the issue.

    In general, I'd prefer a solution that is based on immutability so that an update returns a new copy and new instance. This approach is used in an open PR #9598 . However in that case, there aren't Maps that are involved.
    Using persistent collections such as PCollections library could be useful in the immutable approach.

  12. 315157973 commented on Mar 8, 2021

    @315157973
    Contributor

    In general, I'd prefer a solution that is based on immutability so that an update returns a new copy and new instance. This approach is used in an open PR #9598 . However in that case, there aren't Maps that are involved.

    Creating new objects will produce garbage, I think ConcurrentHashMap will be better

  13. lhotari commented on Mar 8, 2021

    @lhotari
    MemberAuthor

    Creating new objects will produce garbage, I think ConcurrentHashMap will be better

    New objects are only a problem if it causes a bottleneck or excessive GC pressure. I don't think that policy modifications would be a large part of the workload. Therefore there isn't a reason to reject solutions around immutability.
    btw. Persistent data collections such as PCollections library help minimize the number of new objects that are created when using immutable collections.

  14. 315157973 commented on Mar 8, 2021

    @315157973
    Contributor

    New objects are only a problem if it causes a bottleneck or excessive GC pressure. I don't think that policy modifications would be a large part of the workload. Therefore there isn't a reason to reject solutions around immutability.

    In the scene of this issue, compared with ConcurrentHashMap, what are its advantages?

  15. lhotari commented on Mar 9, 2021

    @lhotari
    MemberAuthor

    In the scene of this issue, compared with ConcurrentHashMap, what are its advantages?

    One of the advantages of immutable objects (all fields final in Java) is that there isn't any need for locks at access time after there is a reference to the object. A design where there's some kind of "single writer principle" and immutable objects can be easier to get right. However it might be an overkill to modify an existing solution to follow this type of pattern.
    I guess that the benefit of using ConcurrentHashMap (or Collections.synchronizedMap wrapper) is that the change is relatively small to the existing solution as you have already demonstrated in the PR #9850 .

  16. changed the title [-]Creating and modifying authorization policies in not thread safe[/-] [+]Creating and modifying authorization policies is not thread safe[/+] on Mar 12, 2021
  17. codelipenghui commented on Mar 4, 2022

    @codelipenghui
    Contributor

    The issue had no activity for 30 days, mark with Stale label.

  18. lhotari commented on Jun 18, 2025

    @lhotari
    MemberAuthor

    Addressed for namespace policies by #9900, separate issue for topic policies in #21303

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedlifecycle/staletype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions