From 4a7e86e989465203b16d9df74264556fc07f753b Mon Sep 17 00:00:00 2001 From: Deluan Date: Thu, 2 Feb 2023 10:36:27 -0500 Subject: [PATCH] Fix file descriptor leaking. --- core/artwork/artwork.go | 2 +- utils/cache/file_caches.go | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core/artwork/artwork.go b/core/artwork/artwork.go index 3ed48d02..5d0677f3 100644 --- a/core/artwork/artwork.go +++ b/core/artwork/artwork.go @@ -65,7 +65,7 @@ func (a *artwork) Get(ctx context.Context, artID model.ArtworkID, size int) (rea r, err := a.cache.Get(ctx, artReader) if err != nil { - if !errors.Is(err, context.Canceled) { + if !errors.Is(err, context.Canceled) && !errors.Is(err, ErrUnavailable) { log.Error(ctx, "Error accessing image cache", "id", artID, "size", size, err) } return nil, time.Time{}, err diff --git a/utils/cache/file_caches.go b/utils/cache/file_caches.go index bbb008fd..2125ff82 100644 --- a/utils/cache/file_caches.go +++ b/utils/cache/file_caches.go @@ -85,7 +85,11 @@ func (fc *fileCache) invalidate(ctx context.Context, key string) error { if !fc.cache.Exists(key) { return nil } - return fc.cache.Remove(key) + err := fc.cache.Remove(key) + if err != nil { + log.Warn(ctx, "Error removing key from cache", "cache", fc.name, "key", key, err) + } + return err } func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) { @@ -110,14 +114,15 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) { log.Trace(ctx, "Cache MISS", "cache", fc.name, "key", key) reader, err := fc.getReader(ctx, arg) if err != nil { + r.Close() + w.Close() + _ = fc.invalidate(ctx, key) return nil, err } go func() { if err := copyAndClose(w, reader); err != nil { log.Debug(ctx, "Error storing file in cache", "cache", fc.name, "key", key, err) - if err = fc.invalidate(ctx, key); err != nil { - log.Warn(ctx, "Error removing key from cache", "cache", fc.name, "key", key, err) - } + _ = fc.invalidate(ctx, key) } else { log.Trace(ctx, "File successfully stored in cache", "cache", fc.name, "key", key) }