Skip to content

Commit ea5be46

Browse files
committed
fix(libraryingest): treat drainer shutdown cancel as clean stop
TV/series full scans (libraries with new or updated items) were recorded as "cancelled" with an empty error message and never completed matching. When the file-walk finishes, the ingest executor waits out a settle window and then calls stopDrainers() to shut down the concurrent match goroutines. That cancels the drainer context while a ProcessBatchByFolderAndPathPrefix call may still be in flight. The drainer treated the resulting context.Canceled as a fatal error: it pushed the error to drainerErrCh and called cancel() on the whole scan context, so scanqueue.process() mapped it to cancelRun(). Large/slow libraries (many series, slow provider lookups) keep a batch in flight continuously, so stopDrainers() almost always landed mid-call and the scan was cancelled; small/fast libraries were usually idle at that instant and completed normally. Treat a cancelled drainer context as a deliberate shutdown: return cleanly without escalating. Genuine external cancellation still reaches the run via the main goroutine's scanCtx checks, so real cancels are not swallowed. Adds a regression test (settle window made injectable) that fails against the old handler with 'concurrent match scope ...: context canceled' and passes with the fix.
1 parent e46f608 commit ea5be46

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎internal/libraryingest/executor.go‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ type Executor struct {
9090
realtime *notifications.Hub
9191
now func() time.Time
9292

93+
// tvDrainSettleWindow overrides scopedTVDrainSettleWindow when > 0. Kept
94+
// injectable so tests can exercise the settle-window shutdown path without
95+
// waiting the full production interval.
96+
tvDrainSettleWindow time.Duration
97+
9398
mu sync.Mutex
9499
running []runningClaim
95100
}
@@ -185,6 +190,16 @@ func (e *Executor) ingest(ctx context.Context, folder *models.MediaFolder, mode
185190
concurrentMatched.Add(int64(processed))
186191
}
187192
if err != nil {
193+
// A cancelled drainer context means this is a deliberate
194+
// shutdown (the settle window ended and stopDrainers ran, or
195+
// the whole scan is being torn down), not a real failure. The
196+
// in-flight ProcessBatch call returns context.Canceled here, so
197+
// exit cleanly without escalating. Genuine external
198+
// cancellation still reaches the run via the scanCtx checks in
199+
// the main goroutine, so this does not swallow real cancels.
200+
if drainerCtx.Err() != nil {
201+
return
202+
}
188203
select {
189204
case drainerErrCh <- fmt.Errorf("concurrent match scope %q: %w", scopePath, err):
190205
default:
@@ -238,12 +253,16 @@ func (e *Executor) ingest(ctx context.Context, folder *models.MediaFolder, mode
238253
}
239254
matchScopes = scanMatchScopes
240255
if shouldWaitForTVQueueSettle(folder, scanResult) {
256+
settleWindow := e.tvDrainSettleWindow
257+
if settleWindow <= 0 {
258+
settleWindow = scopedTVDrainSettleWindow
259+
}
241260
slog.Info("library ingest: waiting for tv queue settle window",
242261
"folder_id", folder.ID,
243262
"mode", mode,
244-
"wait", scopedTVDrainSettleWindow,
263+
"wait", settleWindow,
245264
)
246-
timer := time.NewTimer(scopedTVDrainSettleWindow)
265+
timer := time.NewTimer(settleWindow)
247266
select {
248267
case <-scanCtx.Done():
249268
timer.Stop()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package libraryingest
2+
3+
import (
4+
"context"
5+
"sync/atomic"
6+
"testing"
7+
"time"
8+
9+
"github.com/Silo-Server/silo-server/internal/models"
10+
"github.com/Silo-Server/silo-server/internal/scanner"
11+
)
12+
13+
// settleBlockingMatcher simulates a TV match drainer whose provider lookup is
14+
// still in flight when the settle window expires: the batch call blocks until
15+
// its context is cancelled, then returns context.Canceled — exactly what
16+
// stopDrainers triggers when it cancels the drainer context.
17+
type settleBlockingMatcher struct {
18+
batchCalls atomic.Int64
19+
}
20+
21+
func (m *settleBlockingMatcher) ProcessBatchByFolderAndPathPrefix(ctx context.Context, _ int, _ string, _ time.Time) (int, error) {
22+
m.batchCalls.Add(1)
23+
<-ctx.Done()
24+
return 0, ctx.Err()
25+
}
26+
27+
func (m *settleBlockingMatcher) ProcessAllByFolderAndPathPrefix(context.Context, int, string, time.Time) (int, error) {
28+
return 0, nil
29+
}
30+
31+
func (m *settleBlockingMatcher) RetryUnmatchedItemsByFolderAndPathPrefix(context.Context, int, string) (int, int, error) {
32+
return 0, 0, nil
33+
}
34+
35+
// settleStubScanner returns a scan result that triggers the TV settle window
36+
// (a series library with new items) and accepts the post-match finalize call.
37+
type settleStubScanner struct {
38+
result *scanner.ScanResult
39+
}
40+
41+
func (s *settleStubScanner) ScanFolder(context.Context, *models.MediaFolder) (*scanner.ScanResult, error) {
42+
return s.result, nil
43+
}
44+
45+
func (s *settleStubScanner) ScanSubtree(context.Context, *models.MediaFolder, string) (*scanner.ScanResult, error) {
46+
return s.result, nil
47+
}
48+
49+
func (s *settleStubScanner) ScanFile(context.Context, string, *models.MediaFolder) error {
50+
return nil
51+
}
52+
53+
func (s *settleStubScanner) FinalizeVariantsByPathPrefix(context.Context, *models.MediaFolder, string) error {
54+
return nil
55+
}
56+
57+
// TestIngestFolderCompletesWhenDrainerCanceledMidBatch is a regression test for
58+
// the settle-window cancellation bug: a TV library full scan was recorded as
59+
// "cancelled" because a drainer batch in flight when stopDrainers() ran returned
60+
// context.Canceled, which the drainer escalated as a fatal scan error. The
61+
// ingest must instead complete normally.
62+
func TestIngestFolderCompletesWhenDrainerCanceledMidBatch(t *testing.T) {
63+
matcher := &settleBlockingMatcher{}
64+
exec := &Executor{
65+
scanner: &settleStubScanner{result: &scanner.ScanResult{New: 1}},
66+
matcher: matcher,
67+
now: time.Now,
68+
tvDrainSettleWindow: 50 * time.Millisecond,
69+
}
70+
folder := &models.MediaFolder{ID: 5, Type: "series", Paths: []string{"/tv"}}
71+
72+
result, err := exec.IngestFolder(context.Background(), folder)
73+
if err != nil {
74+
t.Fatalf("expected ingest to complete, got error: %v", err)
75+
}
76+
if result == nil || result.Skipped {
77+
t.Fatalf("expected a non-skipped result, got %+v", result)
78+
}
79+
if matcher.batchCalls.Load() == 0 {
80+
t.Fatal("drainer never ran a batch; test did not exercise the settle-window shutdown path")
81+
}
82+
}

0 commit comments

Comments
 (0)