Repository navigation
fix(libraryingest): TV full scans cancelled at settle-window boundary - #15
Conversation
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.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughExecutor adds a configurable ChangesTV Drain Timeout Configuration
🎯 4 (Complex) | ⏱️ ~45 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
…-window-cancel fix(libraryingest): TV full scans cancelled at settle-window boundary
fix(libraryingest): TV full scans cancelled at settle-window boundary
Problem
TV libraries could never finish a full scan. Manually scanning the large
series libraries —
TV Shows(id 2),TV Shows - International(id 4), andAnime(id 5) — never reachedcompleted. The scan walked every file fine andmatched some titles, then silently stopped: the next library's scan started
immediately and matching/metadata for the affected library never finished.
What an operator saw. In the admin scan view the run showed up as
cancelled(labelled "aborted" in the UI), but with an empty error message —so there was nothing to tell you why. In the app log, the run logged
library ingest: concurrent scoped matching stopped, then nothing — nocompletion, no error — and the worker moved on to the next library. The
cancellation always landed exactly 11 seconds after
waiting for tv queue settle window. The affected scans never loggedmetadata: scoped matcher selectedorlibrary ingest: completed.Why only the big libraries. Small/fast libraries like
Sports(id 7)completed normally every time. Only the large, slow series libraries (many
shows, slow TVDB/TMDB lookups) were affected — which is exactly the ones that
matter most. This was a pre-existing logic race, unrelated to the recent
PostgreSQL tuning, pool sizing, or statement/lock timeouts.
scan_runsevidence:Solution
The fix is a single behavioral change in the concurrent match drainer, plus a
regression test.
fix(libraryingest): treat drainer shutdown cancel as clean stop
When a TV library finishes its file-walk,
ingest()waits out an 11s settlewindow for the match queue to quiesce, then calls
stopDrainers()to shut downthe concurrent matcher goroutines (
internal/libraryingest/executor.go). ThatstopDrainers()cancels the drainer context while aMatcher.ProcessBatchByFolderAndPathPrefixcall may still be in flight. Thein-flight call returns
context.Canceled— and the drainer's error handlertreated any error as fatal:
drainerErrCh, andcancel()on the entire scan context.The executor then returned that error, and
scanqueue.process()(
internal/scanqueue/service.go:342) mapscontext.CanceledtocancelRun(),which records the run as
cancelledwith an emptyerror_message— exactly thesymptom above. The
fmt.Errorf("... %w", context.Canceled)wrapping stillsatisfies
errors.Is(err, context.Canceled), so the mapping fired.Large/slow libraries keep a batch in flight essentially continuously, so the
settle-window
stopDrainers()almost always landed mid-call. Small/fastlibraries were usually idle between the 2s drain ticks at that instant and
exited via the clean
case <-drainerCtx.Done()path, so they completed.The fix special-cases a cancelled drainer context as a deliberate shutdown
(settle window ended, or the scan is being torn down) and returns cleanly
instead of escalating:
Checking
drainerCtx.Err()(rather than unwrappingerr) is robust againstpgx/matcher errors that may not cleanly unwrap to
context.Canceled. Realexternal cancellation (e.g.
CancelLibrary, shutdown) still reaches the run viathe main goroutine's
scanCtxchecks, so legitimate cancels are not swallowed —only the drainer's own deliberate shutdown is suppressed.
The settle window was also made injectable (
Executor.tvDrainSettleWindow,defaulting to the existing 11s const) purely so the regression test can exercise
the shutdown path without waiting 11 seconds.
Risk / follow-ups
when its own context is already cancelled. Genuine match errors (provider
failures, DB errors) before shutdown still fail the run as before.
on a context with no deadline while the pool is saturated by
refresh_metadata)is not addressed here and still needs the DB-timeout / pool-size work to
make TV scans reliable end to end.
go vet'slostcancelwarning on the settle-windowscanCtx.Done()returnpath is pre-existing on
mainand unrelated to this change.Verification
go test ./internal/libraryingest/...passes, including the newTestIngestFolderCompletesWhenDrainerCanceledMidBatch.expected ingest to complete, got error: concurrent match scope "/tv": context canceled; with the fix it passes and the run completes.gofmtclean on both changed files.go build ./internal/libraryingest/...clean. (A fullgo build ./...requires
web/dist, built bymake build; unrelated to this change.)silo, manually scanlibraries 2/4/5, confirm
metadata: scoped matcher selected+library ingest: completedappear andscan_runs.status = completed.AI-use disclosure
Diagnosed and implemented with assistance from Claude Code.
Summary by CodeRabbit
New Features
Bug Fixes
Tests