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

fix: revert lock less when syncing #1026

Merged
merged 1 commit into from Jun 7, 2022

Conversation

johanneswuerbach
Copy link
Contributor

Reverts #988

This change can cause races when the management api is used while a new model is being prepared.

Situation:

  • T1: Routine 1: loads new model from db into newModel
  • T2: Routine 2: write change against db and current model
  • T3: Routine 1: replace model with newModel

In this case the write in T2 would not be present until the next reload.

Sadly that brings back the issue that slow policy loading stalls any enforcements. One option might be to use a different lock for such performance critical operation and accept a certain degree of eventual consistency.

@casbin-bot
Copy link
Member

@tangyang9464 @closetool @sagilio please review

@hsluoyz
Copy link
Member

hsluoyz commented Jun 3, 2022

@johanneswuerbach plz fix:

image

@johanneswuerbach johanneswuerbach changed the title Revert "fix: lock less when syncing" fix: revert lock less when syncing Jun 3, 2022
@johanneswuerbach
Copy link
Contributor Author

@hsluoyz fix pushed.

@tangyang9464
Copy link
Member

@johanneswuerbach Maybe we don't need to revert, just add a read lock to LoadPolicy from the beginning. Temporary inconsistencies are allowed, read but not write.

@johanneswuerbach
Copy link
Contributor Author

That won’t work as an open read lock blocks any writes and there is no way to upgrade a read to write lock in RWMutex today golang/go#38859 (comment)

@tangyang9464
Copy link
Member

@johanneswuerbach I don't understand very well, isn't our goal to add RLock, only allow reading and not writing? My code is

func (e *SyncedEnforcer) LoadPolicy() error {
	e.m.RLock()
	defer e.m.RUnlock()
	return e.Enforcer.LoadPolicy()
}

@johanneswuerbach
Copy link
Contributor Author

@tangyang9464 sorry I misunderstood your proposal. LoadPolicy is doing changes on the enforcer (e.g.

for _, rm := range e.rmMap {
) and I'm not sure what happens if reads occur during those changes.

@tangyang9464
Copy link
Member

tangyang9464 commented Jun 7, 2022

@johanneswuerbach yes, we need use new rmMap like newModel. But role manager has no Copy interface. Maybe we need to add it for this.

@johanneswuerbach
Copy link
Contributor Author

Hey @tangyang9464, my company is likely migrating away from casbin due to several scalability issues discovered with our usage and the intransparent maintainer decision making, so I won't have much time to implement those changes.

I just wanted to revert this change, as I've originally introduced it in #1026.

@hsluoyz hsluoyz merged commit 860d290 into casbin:master Jun 7, 2022
@github-actions
Copy link

github-actions bot commented Jun 7, 2022

🎉 This PR is included in version 2.47.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@johanneswuerbach johanneswuerbach deleted the revert-988-background-sync branch June 7, 2022 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants