Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforcer hangs during multi-threaded modifications #336

Open
v-loboda opened this issue Nov 10, 2023 · 8 comments
Open

Enforcer hangs during multi-threaded modifications #336

v-loboda opened this issue Nov 10, 2023 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@v-loboda
Copy link

Hi guys

We encountered a problem in an environment where intensive policy modification occurs. It's very difficult to reproduce, and I can't do it reliably.
We have rbac model, several nodes with synchronization via IWatcher and a lot of policy changes including removing them.

Sometimes, very rarely, we catch an error
Error Write lock may not be acquired with read lock held. This pattern is prone to deadlocks. Please ensure that read locks are released before taking a write lock. If an upgrade is necessary, use an upgrade lock in place of the read lock.

From my point of view, there are 3 problem pleces:

  1. DefaultPolicyStore.Node class, where PolicyTextSet collection can be changed from different threads in several places
  2. Casbin.Rbac.Role class where _roles collection can be changed from different threads
  3. InternalEnforce class and the InternalEnforce method, where Scanner.Interrupt is not executed on every execution path. This can lead to a deadlock in the internal Iterator->node.Lock, where after _node.Lock.EnterReadLock ExitReadLock will not be executed.
@casbin-bot casbin-bot added the question Further information is requested label Nov 10, 2023
@casbin-bot
Copy link
Member

@sagilio sagilio self-assigned this Nov 10, 2023
@sagilio sagilio added bug Something isn't working and removed question Further information is requested labels Nov 10, 2023
@sagilio
Copy link
Member

sagilio commented Nov 10, 2023

Hi! Thank you for your feedback. I will review your points later and it would be helpful if you could provide more details like the error throw line.

@v-loboda
Copy link
Author

This happen in the Default PolicyStore.Node class in TryAddPolicy and TryRemovePolicy, lines 74 and 133 respectively.

@dmolochnikov
Copy link

Hello! Any updates on this issue?

@hsluoyz
Copy link
Member

hsluoyz commented Apr 4, 2024

@v-loboda @dmolochnikov can you provide stable reproduce steps? and also:

  1. Casbin.NET version?
  2. Related code (like a test case)
  3. Casbin model
  4. Casbin policy

@v-loboda
Copy link
Author

v-loboda commented Apr 5, 2024

Hi, as I said before, it is hard to reprocude becase of multithreaded nature. You just need to modify your policies in several threads and read them simultaneously. In the first message I have pointed out several places that do not meet the multithreading requirements.

  1. It was 2.1.1 version
  2. There is no specific code, just multithreaded operations with AddPoliciesAsync, AddRoleForUserAsync, RemovePolicyAsync, EnforceAsync and GetImplicitPermissionsForUser
[request_definition]
r = sub, resource_type, resource, act

[policy_definition]
p = sub, resource_type, resource, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && (r.resource_type == p.resource_type || p.resource_type == "*") && (r.resource == p.resource || p.resource == "*") && (r.act == p.act || p.act == "*")
  1. Policy pattern: ["user1", "test1", "test1", "test1"]

@hsluoyz
Copy link
Member

hsluoyz commented Apr 14, 2024

@v-loboda how to reproduce it stably?

@v-loboda
Copy link
Author

I would make a test case if I could reproduce the bug reliably.

I was able to reproduce it several times by running the modification and receiving permissions in multiple threads and waiting for errors for a long time. Sometimes the error appeared within a few seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

5 participants