fix(jellyfin): avoid stalling playback info on slow lyrics plugins (#6423) (#6435)

This commit is contained in:
duongynhi000005-oss 2026-11-16 03:05:14 +07:00 committed by GitHub
parent 84af2760ce
commit 97df50888e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 101 additions and 7 deletions

View File

@ -283,7 +283,8 @@ track and Feishin on every song change, so lyric-less tracks are the hot path.
Finamp opens its lyrics view only when the track has a `Lyric` `MediaStream` (`HasLyrics` is just a
list badge). Browse lists set both from embedded lyrics only. `PlaybackInfo` runs the full pipeline
per track, so sidecar and plugin lyrics show up there. Feishin also requires server version ≥ 10.9
per track with a short timeout bound, so sidecar and cached/fast plugin lyrics show up there without
stalling playback start on a slow cold plugin lookup. Feishin also requires server version ≥ 10.9
(we advertise 12.1.0).
## AudioMuse-AI compatible endpoints

View File

@ -3,12 +3,39 @@ package jellyfin
import (
"context"
"net/http"
"time"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/jellyfin/dto"
)
// playbackInfoLyricsTimeout bounds how long getPlaybackInfo waits for lyrics before
// returning without the Lyric MediaStream; slow plugin lookups finish warming the cache in core/lyrics.
var playbackInfoLyricsTimeout = 100 * time.Millisecond
// playbackLyrics resolves lyrics for getPlaybackInfo within playbackInfoLyricsTimeout.
func (api *Router) playbackLyrics(ctx context.Context, mf *model.MediaFile) model.LyricList {
resCh := make(chan model.LyricList, 1)
go func() {
defer close(resCh)
defer func() {
if r := recover(); r != nil {
log.Error(ctx, "Panic recovered in playback lyrics lookup", "id", mf.ID, "err", r)
}
}()
resCh <- api.lyricsFor(ctx, mf)
}()
select {
case list := <-resCh:
return list
case <-ctx.Done():
return nil
case <-time.After(playbackInfoLyricsTimeout):
return nil
}
}
// lyricsFor resolves lyrics through the full source pipeline (embedded, sidecar, plugins).
func (api *Router) lyricsFor(ctx context.Context, mf *model.MediaFile) model.LyricList {
list, err := api.lyrics.GetLyrics(ctx, mf)

View File

@ -16,11 +16,27 @@ import (
// fakeLyricsService returns canned lyrics per media-file ID.
type fakeLyricsService struct {
lyrics map[string]model.LyricList
err error
lyrics map[string]model.LyricList
err error
panicVal any
blockCh <-chan struct{}
doneCh chan<- struct{}
}
func (f *fakeLyricsService) GetLyrics(_ context.Context, mf *model.MediaFile) (model.LyricList, error) {
func (f *fakeLyricsService) GetLyrics(ctx context.Context, mf *model.MediaFile) (model.LyricList, error) {
if f.doneCh != nil {
defer close(f.doneCh)
}
if f.blockCh != nil {
select {
case <-f.blockCh:
case <-ctx.Done():
return nil, ctx.Err()
}
}
if f.panicVal != nil {
panic(f.panicVal)
}
if f.err != nil {
return nil, f.err
}

View File

@ -50,10 +50,10 @@ func (api *Router) getPlaybackInfo(w http.ResponseWriter, r *http.Request) {
return
}
src := dto.MediaSourceFromMediaFile(*mf)
// The mapper only sees embedded lyrics; per-track we can afford the full pipeline
// (sidecars, plugins) so Finamp's Lyric-stream gate reflects every source.
// The mapper only sees embedded lyrics; also check external sources (sidecars, plugins)
// within playbackInfoLyricsTimeout so Finamp's Lyric-stream gate reflects them when ready.
if !slices.ContainsFunc(src.MediaStreams, func(s dto.MediaStream) bool { return s.Type == "Lyric" }) {
if _, found := servableLyric(api.lyricsFor(r.Context(), mf)); found {
if _, found := servableLyric(api.playbackLyrics(r.Context(), mf)); found {
src.MediaStreams = append(src.MediaStreams, dto.MediaStream{
Type: "Lyric", Index: len(src.MediaStreams), IsExternal: true,
})

View File

@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/navidrome/navidrome/core/stream"
"github.com/navidrome/navidrome/model"
@ -161,6 +162,55 @@ var _ = Describe("Stream", func() {
Expect(res.MediaSources[0].Id).To(Equal(dto.EncodeID(testID("s-err"))))
Expect(lyricStreams(res)).To(BeEmpty())
})
It("does not block playback info when lyrics lookup is slow", func() {
origTimeout := playbackInfoLyricsTimeout
playbackInfoLyricsTimeout = 20 * time.Millisecond
DeferCleanup(func() { playbackInfoLyricsTimeout = origTimeout })
blockCh := make(chan struct{})
doneCh := make(chan struct{})
DeferCleanup(func() {
close(blockCh)
Eventually(doneCh).Should(BeClosed())
})
ds.MediaFile().(*tests.MockMediaFileRepo).SetData(model.MediaFiles{
{ID: testID("s1"), Title: "Slow Song", Suffix: "mp3", Duration: 100, Size: 1000, LibraryID: 1},
})
api.lyrics = &fakeLyricsService{
lyrics: map[string]model.LyricList{
testID("s1"): {{Kind: "main", Synced: true, Line: []model.Line{{Value: "delayed"}}}},
},
blockCh: blockCh,
doneCh: doneCh,
}
start := time.Now()
res := playbackInfo()
duration := time.Since(start)
Expect(duration).To(BeNumerically("<", 150*time.Millisecond))
Expect(res.MediaSources).To(HaveLen(1))
Expect(lyricStreams(res)).To(BeEmpty())
})
It("recovers panics in detached lyrics lookup and returns 200 without Lyric stream", func() {
ds.MediaFile().(*tests.MockMediaFileRepo).SetData(model.MediaFiles{
{ID: testID("s-panic"), Title: "Panic Song", Suffix: "mp3", Duration: 100, Size: 1000, LibraryID: 1},
})
api.lyrics = &fakeLyricsService{panicVal: "lyrics plugin crash"}
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/Items/"+dto.EncodeID(testID("s-panic"))+"/PlaybackInfo", nil).WithContext(ctxUser())
r = withChiURLParam(r, "itemId", dto.EncodeID(testID("s-panic")))
api.getPlaybackInfo(w, r)
Expect(w.Code).To(Equal(http.StatusOK))
var res dto.PlaybackInfoResponse
Expect(json.Unmarshal(w.Body.Bytes(), &res)).To(Succeed())
Expect(res.MediaSources).To(HaveLen(1))
Expect(lyricStreams(res)).To(BeEmpty())
})
})
Describe("streamAudio", func() {