casbin / jcasbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Java
https://casbin.org
Apache License 2.0
2.4k stars 464 forks source link

Concurrency exception: After 30 seconds of running the test code #292 , there are other problems. #298

Closed shijianjs closed 2 years ago

shijianjs commented 2 years ago

After 30 seconds of running the test code #292 , there are other problems.

jcasbin version: 1.28.2、1.29.0 exception:

java.lang.ClassCastException: class java.util.HashMap$Node cannot be cast to class java.util.HashMap$TreeNode (java.util.HashMap$Node and java.util.HashMap$TreeNode are in module java.base of loader 'bootstrap')

    at java.base/java.util.HashMap$TreeNode.moveRootToFront(HashMap.java:1882)
    at java.base/java.util.HashMap$TreeNode.treeify(HashMap.java:1998)
    at java.base/java.util.HashMap.treeifyBin(HashMap.java:767)
    at java.base/java.util.HashMap.putVal(HashMap.java:639)
    at java.base/java.util.HashMap.put(HashMap.java:607)
    at org.casbin.jcasbin.util.BuiltInFunctions$GenerateGFunctionClass$1.variadicCall(BuiltInFunctions.java:382)
    at com.googlecode.aviator.runtime.function.AbstractVariadicFunction.call(AbstractVariadicFunction.java:71)
    at Script_1663051805038_59.execute0(Script_1663051805038_59:1)
    at com.googlecode.aviator.ClassExpression.executeDirectly(ClassExpression.java:44)
    at com.googlecode.aviator.BaseExpression.execute(BaseExpression.java:237)
    at org.casbin.jcasbin.main.CoreEnforcer.enforce(CoreEnforcer.java:519)
    at org.casbin.jcasbin.main.CoreEnforcer.enforce(CoreEnforcer.java:631)
    at com.fingard.rdc.avatar.auth.casbin.CasbinTest.lambda$parallelTest$5(CasbinTest.java:97)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

Originally posted by @shijianjs in https://github.com/casbin/jcasbin/issues/294#issuecomment-1244982764

casbin-bot commented 2 years ago

@tangyang9464 @imp2002

hsluoyz commented 2 years ago

@imp2002

imp2002 commented 2 years ago

I presumably identified the problem, I modified the hashmap outside, but forgot the internal hashmap in line 348.

I found the same error at stackoverflow it's a hashmap problem too. So it's most likely to be solved by https://github.com/casbin/jcasbin/pull/299.

https://github.com/casbin/jcasbin/blob/f43e679eef7eda7f471761462ba2d384233a046e/src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java#L338-L353

it's should be

memorizedMap.put(name, new ConcurrentHashMap<>());

I'm sorry for my carelessness.

@shijianjs I use my own model and policy to reproduce your bug in #292, but can't reproduce this new bug. Could you provide your policy in #292, just Model you have post, I need your policy to reproduce it and make sure this new modification can fix it. Thanks.

hsluoyz commented 2 years ago

@imp2002 can you make a PR? Maybe the user can test it by himself

shijianjs commented 2 years ago

I presumably identified the problem, I modified the hashmap outside, but forgot the internal hashmap in line 348.

https://github.com/casbin/jcasbin/blob/f43e679eef7eda7f471761462ba2d384233a046e/src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java#L338-L353

it's should be

memorizedMap.put(name, new ConcurrentHashMap<>());

I'm sorry for my carelessness.

@shijianjs I use my own model and policy to reproduce your bug in #292, but can't reproduce this new bug. Could you provide your policy in #292, just Model you have post, I need your policy to reproduce it and make sure this new modification can fix it. Thanks.

@imp2002 I send you the data once it's desensitized. It takes longer for the local PGSQL database to report an error.

16:32:49.192 [pool-1-thread-13] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:ApplicationManage, update ---> true
16:32:49.193 [pool-1-thread-8] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:ApplicationManage, update ---> true
16:32:49.186 [pool-1-thread-2] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:EnvironmentManage, helmApplyNamespace ---> true
执行异常出错:counter=457950
16:32:49.187 [pool-1-thread-3] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:environment/psq-test-001, QUERY ---> true
16:32:49.187 [pool-1-thread-9] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:ApplicationManage, update ---> true
16:32:49.193 [pool-1-thread-14] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:NodeLimit, update ---> true
java.lang.ClassCastException: class java.util.HashMap$Node cannot be cast to class java.util.HashMap$TreeNode (java.util.HashMap$Node and java.util.HashMap$TreeNode are in module java.base of loader 'bootstrap')
16:32:49.193 [pool-1-thread-13] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:GlobalConfig, save ---> true
    at java.base/java.util.HashMap$TreeNode.moveRootToFront(HashMap.java:1882)
    at java.base/java.util.HashMap$TreeNode.treeify(HashMap.java:1998)
    at java.base/java.util.HashMap.treeifyBin(HashMap.java:767)
    at java.base/java.util.HashMap.putVal(HashMap.java:639)
16:32:49.194 [pool-1-thread-8] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:ApplicationManage, update ---> true
16:32:49.194 [pool-1-thread-2] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:EnvironmentManage, findAll ---> true
    at java.base/java.util.HashMap.put(HashMap.java:607)
    at org.casbin.jcasbin.util.BuiltInFunctions$GenerateGFunctionClass$1.variadicCall(BuiltInFunctions.java:382)
    at com.googlecode.aviator.runtime.function.AbstractVariadicFunction.call(AbstractVariadicFunction.java:71)
    at Script_1663317085834_64.execute0(Script_1663317085834_64:1)
    at com.googlecode.aviator.ClassExpression.executeDirectly(ClassExpression.java:44)
    at com.googlecode.aviator.BaseExpression.execute(BaseExpression.java:237)
    at org.casbin.jcasbin.main.CoreEnforcer.enforce(CoreEnforcer.java:519)
    at org.casbin.jcasbin.main.CoreEnforcer.enforce(CoreEnforcer.java:631)
    at com.fingard.rdc.avatar.auth.casbin.CasbinTestGithub.lambda$parallelTest$0(CasbinTestGithub.java:53)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
16:32:49.194 [pool-1-thread-14] INFO org.casbin.jcasbin - Request: user1029, avatar-ci:NodeLimit, findAll ---> false

policy

casbin_rule_202209161635.zip

model.conf

Model.conf has been updated as follows

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act, eft

[role_definition]
g = _, _

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

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && keyMatch2(r.act, p.act)  || g(r.sub, "administrator") || r.sub == "admin"
imp2002 commented 2 years ago

I still can't reproduce, using your policy. But this modification(https://github.com/casbin/jcasbin/pull/299) is still necessary, and I found the same error at stackoverflow it's a hashmap problem too. So it's most likely to be solved by https://github.com/casbin/jcasbin/pull/299. you can try after the pr be merged, if there still this problem, I will think again how to solve it.

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.29.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

shijianjs commented 2 years ago

👍 The concurrency problem has been solved. The test code was scaled up to 100 threads and ran for 5 minutes without further problems.