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
fix: prevent concurrent map writes in LoadPolicyLine function #627
Conversation
…oadPolicyLine function. Signed-off-by: James <james.murtagh@vorteil.io>
Signed-off-by: James <james.murtagh@vorteil.io>
Signed-off-by: James <james.murtagh@vorteil.io>
@jamesjmurtagh Do you use the |
I also did same things: #625. |
@jamesjmurtagh any update? |
@jamesjmurtagh you can try if |
I can confirm that this error still occurs when using
In this case, when created a new SyncedEnforcer, the adapter (gorm-adapter, in this case) will ultimately call |
Why does it cause contention? |
In my case, the logic that is calling |
It's a bad way to load policy for each RESTful call. Please load policy at initial time only once. |
I call Edit: I've noticed that the Enforcer has a |
@jamesjmurtagh Casbin will call LoadPolicy automatically when you call |
@jamesjmurtagh can you provide a full minimized reproducible example code repo? |
I had the same issue, found out that I was using |
I've got this error too, when sending multiple requests to our app at once |
@Paxa can you check if the column names are as intended, I had a column name mismatch earlier. |
@Paxa @Glitchfix which adapter (github repo) and version tag do you use? |
@closetool @tangyang9464 |
|
@Paxa which adapter? (and version tag) |
I use
Line 43 in 277c1a2
But I think casbin solution doesn't fit our needs because it always loads all policies in memory and it takes ~180ms (with current data), and I expect the time to load will increase when we have more users in database. Or we need to think of additional mechanism to maintain in-memory cache updated. Also other race condition when 1 request is reloading policies while other request use enforcer for validation (may be I'm using it wrong, please suggest how to make it better) |
@Paxa you can use watcher and filters to optimize your case, see: https://casbin.org/docs/en/performance |
You should init the enforcer at your program start. |
@Glitchfix for gorm adapter, we have changed the column name from p_type to ptype from v3.1.0 in this PR: casbin/gorm-adapter#67 |
@Glitchfix @Paxa I think our discussion has deviated from the topic of this PR. This PR will be closed for now. And plz make new issues to the correct repos if you still have any questions. |
I managed to create an environment in which the
LoadPolicyLine
function regularly causesconcurrent map writes
panics. Added a globalsync.Mutex
object that is used exclusively within theLoadPolicyLine
function. Have not encountered any locks or concurrent map writes since this addition.