Coder Social home page Coder Social logo

Comments (6)

hsluoyz avatar hsluoyz commented on May 20, 2024 1

I think we should add a lock for:

 	a.dropTable() 
 	a.createTable() 

Can you make a PR to fix it?

from xorm-adapter.

aavshr avatar aavshr commented on May 20, 2024

Yes, I can try working on it. I don't know if that exactly is the issue but will try to submit a PR.

from xorm-adapter.

linxing avatar linxing commented on May 20, 2024

Yes, I can try working on it. I don't know if that exactly is the issue but will try to submit a PR.

is this fixed ur issues ? #27

from xorm-adapter.

hsluoyz avatar hsluoyz commented on May 20, 2024

@linxing thanks for the PR!

from xorm-adapter.

aavshr avatar aavshr commented on May 20, 2024

From the code it seems like it's not been fixed yet. A routine should only release the lock when it's done with the entire savePolicy().
https://github.com/casbin/xorm-adapter/blob/master/adapter.go#L258-L287

// SavePolicy saves policy to database.
func (a *Adapter) SavePolicy(model model.Model) error {
	err := a.dropTable()
	if err != nil {
		return err
	}
	err = a.createTable()
	if err != nil {
		return err
	}

	var lines []*CasbinRule

	for ptype, ast := range model["p"] {
		for _, rule := range ast.Policy {
			line := savePolicyLine(ptype, rule)
			lines = append(lines, line)
		}
	}

	for ptype, ast := range model["g"] {
		for _, rule := range ast.Policy {
			line := savePolicyLine(ptype, rule)
			lines = append(lines, line)
		}
	}

	_, err = a.engine.Insert(&lines)
	return err
}

For instance, consider the following scenario where two routines (A and B) are calling savePolicy() at the same time.

  1. Routine A and B both wait on the lock on dropTable()
  2. Routine A gets the lock and proceeds to drop the table.
  3. Routine A releases the lock.
  4. Routine B obtains the lock and proceeds to drop the table. However, the table is already dropped as Routine A dropped it earlier but has not created it yet and is waiting on the lock to be released by Routine B.

I hope this is clear enough. This is only one of many scenarios I can think of when things might be problematic.

from xorm-adapter.

linxing avatar linxing commented on May 20, 2024

From the code it seems like it's not been fixed yet. A routine should only release the lock when it's done with the entire savePolicy().
https://github.com/casbin/xorm-adapter/blob/master/adapter.go#L258-L287

// SavePolicy saves policy to database.
func (a *Adapter) SavePolicy(model model.Model) error {
	err := a.dropTable()
	if err != nil {
		return err
	}
	err = a.createTable()
	if err != nil {
		return err
	}

	var lines []*CasbinRule

	for ptype, ast := range model["p"] {
		for _, rule := range ast.Policy {
			line := savePolicyLine(ptype, rule)
			lines = append(lines, line)
		}
	}

	for ptype, ast := range model["g"] {
		for _, rule := range ast.Policy {
			line := savePolicyLine(ptype, rule)
			lines = append(lines, line)
		}
	}

	_, err = a.engine.Insert(&lines)
	return err
}

For instance, consider the following scenario where two routines (A and B) are calling savePolicy() at the same time.

  1. Routine A and B both wait on the lock on dropTable()
  2. Routine A gets the lock and proceeds to drop the table.
  3. Routine A releases the lock.
  4. Routine B obtains the lock and proceeds to drop the table. However, the table is already dropped as Routine A dropped it earlier but has not created it yet and is waiting on the lock to be released by Routine B.

I hope this is clear enough. This is only one of many scenarios I can think of when things might be problematic.

just lock the entire SavePolicy func should be ok with the issuse

from xorm-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.