From d97bc26042589c2530d8335f28cdf48c6195f934 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 21 Nov 2020 19:32:25 +0800 Subject: [PATCH] Fix: add recycleLock preventing recycle FileSystem used by another goroutine --- assets | 2 +- pkg/filesystem/filesystem.go | 5 +++++ pkg/filesystem/hooks.go | 8 +++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/assets b/assets index 8ec4514..253bf0c 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 8ec4514b7690eef20b283c1f00ee2d921f19ae7d +Subproject commit 253bf0c5a064345af2ba4e5c3df68978de49755b diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index b6c80e3..af62983 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -97,6 +97,9 @@ type FileSystem struct { 文件系统处理适配器 */ Handler Handler + + // 回收锁 + recycleLock sync.Mutex } // getEmptyFS 从pool中获取新的FileSystem @@ -107,6 +110,7 @@ func getEmptyFS() *FileSystem { // Recycle 回收FileSystem资源 func (fs *FileSystem) Recycle() { + fs.recycleLock.Lock() fs.reset() FSPool.Put(fs) } @@ -120,6 +124,7 @@ func (fs *FileSystem) reset() { fs.Handler = nil fs.Root = nil fs.Lock = sync.Mutex{} + fs.recycleLock = sync.Mutex{} } // NewFileSystem 初始化一个文件系统 diff --git a/pkg/filesystem/hooks.go b/pkg/filesystem/hooks.go index 9c6692f..eb8bbed 100644 --- a/pkg/filesystem/hooks.go +++ b/pkg/filesystem/hooks.go @@ -228,7 +228,9 @@ func GenericAfterUpdate(ctx context.Context, fs *FileSystem) error { // 尝试清空原有缩略图并重新生成 if originFile.GetPolicy().IsThumbGenerateNeeded() { + fs.recycleLock.Lock() go func() { + defer fs.recycleLock.Unlock() if originFile.PicInfo != "" { _, _ = fs.Handler.Delete(ctx, []string{originFile.SourceName + conf.ThumbConfig.FileSuffix}) fs.GenerateThumbnail(ctx, &originFile) @@ -297,7 +299,11 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error { // 异步尝试生成缩略图 if fs.User.Policy.IsThumbGenerateNeeded() { - go fs.GenerateThumbnail(ctx, file) + fs.recycleLock.Lock() + go func() { + defer fs.recycleLock.Unlock() + fs.GenerateThumbnail(ctx, file) + }() } return nil