修复可能导致死锁冲突的Bug

This commit is contained in:
刘祥超
2021-06-16 16:10:02 +08:00
parent 77a8eb5c1a
commit 489e081720
2 changed files with 23 additions and 3 deletions

View File

@@ -281,19 +281,18 @@ func (this *FileList) Purge(count int, callback func(hash string) error) error {
if err != nil { if err != nil {
return err return err
} }
defer func() {
_ = rows.Close()
}()
hashStrings := []string{} hashStrings := []string{}
for rows.Next() { for rows.Next() {
var hash string var hash string
err = rows.Scan(&hash) err = rows.Scan(&hash)
if err != nil { if err != nil {
_ = rows.Close()
return err return err
} }
hashStrings = append(hashStrings, hash) hashStrings = append(hashStrings, hash)
} }
_ = rows.Close() // 不能使用defer防止读写冲突
// 不在 rows.Next() 循环中操作是为了避免死锁 // 不在 rows.Next() 循环中操作是为了避免死锁
for _, hash := range hashStrings { for _, hash := range hashStrings {

View File

@@ -236,6 +236,27 @@ func TestFileList_CleanAll(t *testing.T) {
t.Log(list.Count()) t.Log(list.Count())
} }
func TestFileList_Conflict(t *testing.T) {
list := NewFileList(Tea.Root + "/data").(*FileList)
err := list.Init()
if err != nil {
t.Fatal(err)
}
rows, err := list.purgeStmt.Query(time.Now().Unix(), 1000)
if err != nil {
t.Fatal(err)
}
go func() {
time.Sleep(5 * time.Second)
_ = rows.Close()
}()
t.Log("before exists")
t.Log(list.Exist("123456"))
t.Log("after exists")
}
func BenchmarkFileList_Exist(b *testing.B) { func BenchmarkFileList_Exist(b *testing.B) {
list := NewFileList(Tea.Root + "/data") list := NewFileList(Tea.Root + "/data")
err := list.Init() err := list.Init()