From 489e081720db93a455055ea62aecabd63c9a17e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 16 Jun 2021 16:10:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=AF=E8=83=BD=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=AD=BB=E9=94=81=E5=86=B2=E7=AA=81=E7=9A=84Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file.go | 5 ++--- internal/caches/list_file_test.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/caches/list_file.go b/internal/caches/list_file.go index e61a993..defa88f 100644 --- a/internal/caches/list_file.go +++ b/internal/caches/list_file.go @@ -281,19 +281,18 @@ func (this *FileList) Purge(count int, callback func(hash string) error) error { if err != nil { return err } - defer func() { - _ = rows.Close() - }() hashStrings := []string{} for rows.Next() { var hash string err = rows.Scan(&hash) if err != nil { + _ = rows.Close() return err } hashStrings = append(hashStrings, hash) } + _ = rows.Close() // 不能使用defer,防止读写冲突 // 不在 rows.Next() 循环中操作是为了避免死锁 for _, hash := range hashStrings { diff --git a/internal/caches/list_file_test.go b/internal/caches/list_file_test.go index c71b231..d85b234 100644 --- a/internal/caches/list_file_test.go +++ b/internal/caches/list_file_test.go @@ -236,6 +236,27 @@ func TestFileList_CleanAll(t *testing.T) { 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) { list := NewFileList(Tea.Root + "/data") err := list.Init()