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

There is a deadlock in gcache #3577

Closed
mijjjj opened this issue May 13, 2024 · 13 comments
Closed

There is a deadlock in gcache #3577

mijjjj opened this issue May 13, 2024 · 13 comments
Assignees
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version.

Comments

@mijjjj
Copy link

mijjjj commented May 13, 2024

Go version

go1.22.1

GoFrame version

2.7.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

gcache.GetOrSetFuncLock

What did you see happen?

使用gcache.GetOrSetFuncLock会死锁

What did you expect to see?

正常运行

@mijjjj mijjjj added the bug It is confirmed a bug, but don't worry, we'll handle it. label May 13, 2024
@wln32
Copy link
Member

wln32 commented May 14, 2024

你的代码是什么呢,发出来看看

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


What is your code? Send it and take a look.

@mijjjj
Copy link
Author

mijjjj commented May 14, 2024

image
很正常的语句,使用的是内存缓存,数据库使用时sqlite,调试发现函数有正常执行到,但是返回后就会出现死锁,使用GetOrSetFunc正常使用

@wln32
Copy link
Member

wln32 commented May 14, 2024

@gqcn @mijjjj
我复现了死锁的情况,以下是代码

// 创建表
create table issue3577(
	id INT PRIMARY KEY     NOT NULL,
   	name           TEXT    NOT NULL,
   	age            INT     NOT NULL
)
// 插入一条数据
insert into issue3577 values(1,'wln',28);

type Issue3577 struct {
	Id   int    `orm:"id"`
	Name string `orm:"name"`
	Age  int    `orm:"age"`
}

func init(){
       gdb.AddConfigNode("sqlite", gdb.ConfigNode{
		Link: "sqlite::@file(/testdb.sqlite3)",
	})
}

func Test_issue3577(t *testing.T) {
	ctx := context.TODO()
	sqlite := g.DB("sqlite")

	key := "isssue3577"
	t.Log("连接sqlite成功,开始查询")
	v, err := gcache.GetOrSetFuncLock(ctx, key, func(ctx context.Context) (value interface{}, err error) {
		return test_issue3557_getone(ctx, sqlite)
	}, 0)
	t.Log("查询结束")
	if err != nil {
		t.Fatal(err)
	}
	t.Log("val=", v.Interface())
}

func test_issue3557_getone(_ context.Context, db gdb.DB) (val any, err error) {
	res, err := db.Model("issue3577").One()
	if err != nil {
		return nil, err
	}
	data := Issue3577{}
	err = res.Struct(&data)
	return data, err
}

运行命令 go test -timeout 30s -run ^Test_issue3577$
运行上述代码,会死锁,可以自行查询打印的堆栈情况,

问题出现在当执行gcache.GetOrSetFuncLock ,
内部会调用(d *adapterMemoryData) SetWithLock,这个函数一开始就直接mu.Lock
然后在调用回调函数test_issue3557_getone的时候,
内部会调用(m *softTimeMaintainer) getSoftFieldNameAndType,文件路径: gf/database/gdb/gdb_model_soft_time.go:218
再往里面最终会调用(d *adapterMemoryData) Get,文件路径: gf/os/gcache/gcache_adapter_memory_data.go:145
这个函数一开始就调用了mu.RLock,然后上面一开始调用的mu.Lock还没有解锁,所以就造成了死锁

@wln32
Copy link
Member

wln32 commented May 14, 2024

图片在上面发不出来,在这里发

2222

@gogf gogf deleted a comment from Issues-translate-bot May 14, 2024
@wln32
Copy link
Member

wln32 commented May 14, 2024

pr #3579 可以解决此bug

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


#3579 can solve this bug

@mijjjj
Copy link
Author

mijjjj commented May 15, 2024

嗯嗯,我就是切换到默认的了

@mijjjj
Copy link
Author

mijjjj commented May 15, 2024

只能先这样用了

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Yeah, okay, I just switched to the default one.

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This is the only way to use it first

@gqcn gqcn self-assigned this May 15, 2024
@gqcn
Copy link
Member

gqcn commented May 15, 2024

Let me handle this.

@gqcn
Copy link
Member

gqcn commented May 16, 2024

#3585

@gqcn gqcn closed this as completed May 16, 2024
@gqcn gqcn added the done This issue is done, which may be release in next version. label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants