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

[Bug]SyncedCachedEnforcer cannot use LoadFilteredPolicy and LoadPolicy concurrently #1298

Open
longtengz opened this issue Aug 18, 2023 · 3 comments

Comments

@longtengz
Copy link

Describe the bug

I'm trying to use LoadFilteredPolicy and LoadPolicy on SyncedCachedEnforcer in different places, thus they might be called in the same time. I think these two might interfere with each other, because sometimes my backend would wait on LoadFilteredPolicy, it's kind of random and does not have a particular pattern that I can debug with, so I think it's lock-related.

I'm using SyncedCachedEnforcer with this adapter https://github.com/pckhoi/casbin-pgx-adapter

To Reproduce

Just change https://github.com/casbin/casbin/blob/master/enforcer_cached_synced_test.go to have it run LoadPolicy and LoadFilteredPolicy concurrently, and the bug persists. It's pretty easy to reproduce.

Expected behavior

No deadlock and no bug.

Related Issue: #1268

@casbin-bot
Copy link
Member

@tangyang9464 @JalinWang

@hsluoyz
Copy link
Member

hsluoyz commented Aug 18, 2023

@PokIsemaine

@PokIsemaine
Copy link
Member

PokIsemaine commented Sep 10, 2023

@longtengz Hey,I remember the issue, but it wasn't dealt with because the consistency wasn't reproduced

I tried the following code, but no error occurred.

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/casbin/casbin/v2"
	fileadapter "github.com/casbin/casbin/v2/persist/file-adapter"
	pgxadapter "github.com/pckhoi/casbin-pgx-adapter/v3"
)

func main() {
	a, err := pgxadapter.NewAdapter("postgresql://postgres:root@localhost:5432/postgres?sslmode=disable") // Your driver and data source.
	if err != nil {
		panic(err)
	}
	e, err := casbin.NewSyncedCachedEnforcer("examples/basic_model.conf", a)
	if err != nil {
		panic(err)
	}

	wg := sync.WaitGroup{}
	wg.Add(2000)
	for i := 0; i < 1000; i++ {
		go func(id int) {
			_, _ = e.AddPolicy("alice", "data"+fmt.Sprintln(id), "read")
			wg.Done()
		}(i)
		go func(id int) {
			_, _ = e.AddPolicy("bob", "data"+fmt.Sprintln(id), "read")
			wg.Done()
		}(i)
	}

	wg.Wait()
	fmt.Println("init policy finished")

	filter := &fileadapter.Filter{
		P: []string{"alice", "", ""},
	}
	g := sync.WaitGroup{}
	for {
		g.Add(2)
		go func() {
			_ = e.LoadPolicy()
			g.Done()
		}()

		go func() {
			_ = e.LoadFilteredPolicy(filter)
			g.Done()
		}()
		g.Wait()
		fmt.Println(time.Now())
	}
}

Just change https://github.com/casbin/casbin/blob/master/enforcer_cached_synced_test.go to have it run LoadPolicy and LoadFilteredPolicy concurrently, and the bug persists. It's pretty easy to reproduce.

So, can you please tell us more about how it was changed? Can you show the modified code directly? Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants