navidrome/utils/cache/file_caches.go
Deluan Quintão 97da9993d7
fix(stream): abort the response when a transcoded stream is truncated (#6035)
* fix(stream): abort the response when a transcoded stream is truncated

When a transcode failed after some audio had already been sent, Serve logged
the error and returned nil, so Go finished the chunked body normally and the
client received an apparently complete, silently short file. Symfonium users
hit this on large offline syncs, and the worst path, ffmpeg dying mid-write
behind the transcoding cache, produced no error and nothing in the log above
Debug: the cache writer was closed plainly, so readers drained the truncated
entry to a clean EOF.

The root cause of that silence is an fscache limitation: Close is the only way
to end a cache write, and Close always means "complete". This adopts the
deluan/fscache fork, which adds CloseWithError: on failure copyAndClose now
cancels the entry with the cause, so every attached reader fails mid-read with
the real error instead of EOF, a late Get for the entry is refused, and the
entry never reports a final size. The error travels inside the entry each
reader holds, which makes per-generation delivery automatic and needs no
bookkeeping on our side.

With the failure arriving in-band, one change in Serve covers every mode: an
io.Copy error after bytes are on the wire panics with http.ErrAbortHandler.
Go aborts the response without the terminating chunk (RST_STREAM on HTTP/2),
chi's Recoverer re-panics that value, and the deferred stream.Close() still
runs, so the transcode limiter slot is released as before.

Two behaviors improve as side effects. A transcoder that dies before its first
byte now yields a Subsonic error response instead of a 200 with an empty body,
since the failure reaches Serve as an error while the status is still
unsent; genuinely empty output (clean EOF, exit 0) keeps the 200. And a failed
entry's invalidation no longer defers its unlink past a replacement entry
re-creating the same file, because canceling already closed its readers.

* fix(cache): warn when the cache writer cannot report failures to readers

The CloseWithError capability comes from the fscache fork via a go.mod
replace directive, and a type assertion picks it up. If that directive is
ever lost, the assertion fails silently, readers of a dead writer go back to
draining a truncated entry to a clean EOF, and nothing says so.

Two layers against that: a warning on the failure path when the writer lacks
the capability, and a test that asserts the writer fscache returns carries
it, so losing the fork fails CI instead of a listener's download.

* build: point the fscache replace at the fork's master

deluan/fscache#1 is merged; pin the merge commit instead of the review
branch. Pinned by sha because the module proxy still resolves the fork's
master ref to its pre-merge commit.

* build: reference the upstream fscache PR in the replace comment

The replace itself must keep pointing at the fork: the commit only exists in
djherbis/fscache under refs/pull/22/head, which the Go module fetcher cannot
resolve (verified: unknown revision for both short and full sha). The same
commit is advertised on the fork's master, so that is the fetchable source.
2026-08-25 18:48:43 -04:00

320 lines
9.3 KiB
Go

package cache
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/djherbis/fscache"
"github.com/dustin/go-humanize"
"github.com/hashicorp/go-multierror"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
)
// Item represents an item that can be cached. It must implement the Key method that returns a unique key for a
// given item.
type Item interface {
Key() string
}
// ReadFunc is a function that retrieves the data to be cached. It receives the Item to be cached and returns
// an io.Reader with the data and an error.
type ReadFunc func(ctx context.Context, item Item) (io.Reader, error)
// FileCache is designed to cache data on the filesystem to improve performance by avoiding repeated data
// retrieval operations.
//
// Errors are handled gracefully. If the cache is not initialized or an error occurs during data
// retrieval, it will log the error and proceed without caching.
type FileCache interface {
// Get retrieves data from the cache. This method checks if the data is already cached. If it is, it
// returns the cached data. If not, it retrieves the data using the provided getReader function and caches it.
//
// Example Usage:
//
// s, err := fc.Get(context.Background(), cacheKey("testKey"))
// if err != nil {
// log.Fatal(err)
// }
// defer s.Close()
//
// data, err := io.ReadAll(s)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(data))
Get(ctx context.Context, item Item) (*CachedStream, error)
// Available checks if the cache is available
Available(ctx context.Context) bool
// Disabled reports if the cache has been permanently disabled
Disabled(ctx context.Context) bool
}
// NewFileCache creates a new FileCache. This function initializes the cache and starts it in the background.
//
// name: A string representing the name of the cache.
// cacheSize: A string representing the maximum size of the cache (e.g., "1KB", "10MB").
// cacheFolder: A string representing the folder where the cache files will be stored.
// maxItems: An integer representing the maximum number of items the cache can hold.
// getReader: A function of type ReadFunc that retrieves the data to be cached.
//
// Example Usage:
//
// fc := NewFileCache("exampleCache", "10MB", "cacheFolder", 100, func(ctx context.Context, item Item) (io.Reader, error) {
// // Implement the logic to retrieve the data for the given item
// return strings.NewReader(item.Key()), nil
// })
func NewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader ReadFunc) FileCache {
fc := &fileCache{
name: name,
cacheSize: cacheSize,
cacheFolder: filepath.FromSlash(cacheFolder),
maxItems: maxItems,
getReader: getReader,
mutex: &sync.RWMutex{},
}
go func() {
start := time.Now()
cache, sfs, err := newFSCache(fc.name, fc.cacheSize, fc.cacheFolder, fc.maxItems)
fc.mutex.Lock()
defer fc.mutex.Unlock()
fc.cache = cache
fc.fs = sfs
fc.disabled = cache == nil || err != nil
log.Info("Finished initializing cache", "cache", fc.name, "maxSize", fc.cacheSize, "elapsedTime", time.Since(start))
fc.ready.Store(true)
if err != nil {
log.Error(fmt.Sprintf("Cache %s will be DISABLED due to previous errors", "name"), fc.name, err)
}
if fc.disabled {
log.Debug("Cache DISABLED", "cache", fc.name, "size", fc.cacheSize)
}
}()
return fc
}
type fileCache struct {
name string
cacheSize string
cacheFolder string
maxItems int
cache fscache.Cache
fs *spreadFS
getReader ReadFunc
disabled bool
ready atomic.Bool
mutex *sync.RWMutex
}
func (fc *fileCache) Available(_ context.Context) bool {
fc.mutex.RLock()
defer fc.mutex.RUnlock()
return fc.ready.Load() && !fc.disabled
}
func (fc *fileCache) Disabled(_ context.Context) bool {
fc.mutex.RLock()
defer fc.mutex.RUnlock()
return fc.disabled
}
func (fc *fileCache) invalidate(ctx context.Context, key string) error {
if !fc.Available(ctx) {
log.Debug(ctx, "Cache not initialized yet. Cannot invalidate key", "cache", fc.name, "key", key)
return nil
}
if !fc.cache.Exists(key) {
return nil
}
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) {
if !fc.Available(ctx) {
log.Debug(ctx, "Cache not initialized yet. Reading data directly from reader", "cache", fc.name)
reader, err := fc.getReader(ctx, arg)
if err != nil {
return nil, err
}
return &CachedStream{Reader: reader}, nil
}
key := arg.Key()
r, w, err := fc.cache.Get(key)
if errors.Is(err, fs.ErrNotExist) {
// The entry outlived its data file. Drop it and retry, or every future Get
// for this key fails for the rest of the process's life.
log.Debug(ctx, "Cache entry lost its data file. Re-fetching", "cache", fc.name, "key", key)
_ = fc.invalidate(ctx, key)
r, w, err = fc.cache.Get(key)
}
if err != nil {
return nil, err
}
cached := w == nil
if !cached {
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 := fc.copyAndClose(ctx, key, w, reader); err != nil {
log.Debug(ctx, "Error storing file in 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)
}
}()
}
// If it is in the cache, check if the stream is done being written. If so, return a ReadSeeker
if cached {
size := getFinalCachedSize(r)
if size >= 0 {
log.Trace(ctx, "Cache HIT", "cache", fc.name, "key", key, "size", size)
sr := io.NewSectionReader(r, 0, size)
return &CachedStream{
Reader: sr,
Seeker: sr,
Closer: r,
Cached: true,
}, nil
} else {
log.Trace(ctx, "Cache HIT", "cache", fc.name, "key", key)
}
}
// All other cases, just return the cache reader, without Seek capabilities
return &CachedStream{Reader: r, Cached: cached}, nil
}
// CachedStream is a wrapper around an io.ReadCloser that allows reading from a cache.
type CachedStream struct {
io.Reader
io.Seeker
io.Closer
Cached bool
}
func (s *CachedStream) Close() error {
if s.Closer != nil {
return s.Closer.Close()
}
if c, ok := s.Reader.(io.Closer); ok {
return c.Close()
}
return nil
}
func getFinalCachedSize(r fscache.ReadAtCloser) int64 {
cr, ok := r.(*fscache.CacheReader)
if ok {
size, final, err := cr.Size()
if final && err == nil {
return size
}
}
return -1
}
// copyAndClose marks the entry complete before closing w, so EOF implies the entry is settled on disk.
func (fc *fileCache) copyAndClose(ctx context.Context, key string, w io.WriteCloser, r io.Reader) error {
_, err := io.Copy(w, r)
if err != nil {
err = fmt.Errorf("copying data to cache: %w", err)
}
if c, ok := r.(io.Closer); ok {
if cErr := c.Close(); cErr != nil {
err = multierror.Append(err, fmt.Errorf("closing source stream: %w", cErr))
}
}
if err == nil {
fc.markComplete(ctx, key)
} else if cw, ok := w.(interface{ CloseWithError(error) error }); ok {
// Cancel instead of close, so readers fail with the cause rather than
// draining a truncated entry to a clean EOF.
if cErr := cw.CloseWithError(err); cErr != nil {
// Join, not Append: err is now shared with readers and must not be mutated.
return errors.Join(err, fmt.Errorf("closing cache writer: %w", cErr))
}
return err
} else {
log.Warn(ctx, "Cache writer cannot report failures; readers will see a truncated entry as a clean EOF",
"cache", fc.name, "key", key, err)
}
if cErr := w.Close(); cErr != nil {
err = multierror.Append(err, fmt.Errorf("closing cache writer: %w", cErr))
}
return err
}
// markComplete records on disk that the entry for key was written in full,
// so it is eligible for adoption after a restart (see spreadFS.Reload).
func (fc *fileCache) markComplete(ctx context.Context, key string) {
if fc.fs == nil {
return
}
if err := fc.fs.MarkComplete(key); err != nil {
log.Warn(ctx, "Error writing cache completion marker", "cache", fc.name, "key", key, err)
}
}
func newFSCache(name, cacheSize, cacheFolder string, maxItems int) (fscache.Cache, *spreadFS, error) {
size, err := humanize.ParseBytes(cacheSize)
if err != nil {
log.Error("Invalid cache size. Using default size", "cache", name, "size", cacheSize,
"defaultSize", humanize.Bytes(consts.DefaultCacheSize))
size = consts.DefaultCacheSize
}
if size == 0 {
log.Warn(fmt.Sprintf("%s cache disabled", name))
return nil, nil, nil
}
lru := NewFileHaunter(name, maxItems, size, consts.DefaultCacheCleanUpInterval)
h := fscache.NewLRUHaunterStrategy(lru)
cacheFolder = filepath.Join(conf.Server.CacheFolder.MustPath(), cacheFolder)
var fs *spreadFS
log.Info(fmt.Sprintf("Creating %s cache", name), "path", cacheFolder, "maxSize", humanize.Bytes(size))
fs, err = NewSpreadFS(cacheFolder, 0755)
if err != nil {
log.Error(fmt.Sprintf("Error initializing %s cache FS", name), err)
return nil, nil, err
}
ck, err := fscache.NewCacheWithHaunter(fs, h)
if err != nil {
log.Error(fmt.Sprintf("Error initializing %s cache", name), err)
return nil, nil, err
}
ck.SetKeyMapper(fs.KeyMapper)
return ck, fs, nil
}