From 1b44637606f09dc4078b627a6e9b38be55d9450d Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Thu, 12 Feb 2026 03:02:16 +0300 Subject: [PATCH] Fix mutexes in the cacher (again) --- internal/domains/cacher/files.go | 7 +++++-- internal/domains/cacher/stats.go | 4 ---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/internal/domains/cacher/files.go b/internal/domains/cacher/files.go index 6c27245..f9c8f3d 100644 --- a/internal/domains/cacher/files.go +++ b/internal/domains/cacher/files.go @@ -36,13 +36,13 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) { cacheFilePath := filepath.Join(c.cacheDir, cacheKey+".m4a") c.itemsMutex.Lock() - defer c.itemsMutex.Unlock() // Check if file information exists in cache if item, ok := c.items[cacheKey]; ok { - if _, err := os.Stat(item.Path); err != nil { + if _, err := os.Stat(item.Path); err == nil { // File exists in cache and on disk item.Updated = time.Now().UTC() + c.itemsMutex.Unlock() c.updateCachedStat(sourcePath, item.Size) @@ -64,6 +64,7 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) { } c.items[cacheKey] = item c.currentSize += cachedFileInfo.Size() + c.itemsMutex.Unlock() c.updateCachedStat(sourcePath, item.Size) @@ -82,6 +83,7 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) { // Convert file size, err := c.transcoder.Convert(sourcePath, cacheFilePath) if err != nil { + c.itemsMutex.Unlock() return nil, fmt.Errorf("%w: %w (%w)", ErrCacher, ErrFailedToTranscodeFile, err) } @@ -93,6 +95,7 @@ func (c *Cacher) getFile(sourcePath string) (*models.CacheItem, error) { } c.items[cacheKey] = item c.currentSize += size + c.itemsMutex.Unlock() c.updateCachedStat(sourcePath, size) // TODO: run cleanup on inotify events. diff --git a/internal/domains/cacher/stats.go b/internal/domains/cacher/stats.go index 870a5ba..c233df0 100644 --- a/internal/domains/cacher/stats.go +++ b/internal/domains/cacher/stats.go @@ -13,10 +13,6 @@ import ( // getStat returns file size without triggering conversion (for ls/stat). func (c *Cacher) GetStat(sourcePath string) (int64, error) { - c.statMutex.RLock() - defer c.statMutex.RUnlock() - - // First check cache if size, ok := c.getCachedStat(sourcePath); ok { return size, nil }