From 05f4e33470ece649e6430128f46b637a27f1523d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 16:52:38 +0100 Subject: [PATCH] fix(duplicates): finish updating the index even if the client leaves after the file is deleted Found reviewing the branch: a request cancelled between deleting the file and updating the index left a track whose file does not exist. The index step no longer depends on the request's context. Test first: it cancels the request at exactly that moment. --- internal/duplicates/service.go | 7 +++--- internal/duplicates/service_test.go | 34 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/internal/duplicates/service.go b/internal/duplicates/service.go index 185f309..1a9e7bb 100644 --- a/internal/duplicates/service.go +++ b/internal/duplicates/service.go @@ -181,9 +181,10 @@ func (s *Service) Clean(ctx context.Context, keep int64, remove []int64) (Result if err := removeFile(t.root, t.rel); err != nil { return res, err } - // The file is gone. If the index cannot follow, the next scan flags the path as missing, and - // nothing is left broken; the error says so. - if err := s.store.RemoveCopy(ctx, keep, t.id); err != nil { + // The file is gone, so the index must follow whether or not the client is still there: a request + // that was cancelled now would leave a track whose file does not exist. If the index still cannot + // follow, the next scan flags the path as missing, and nothing is left broken; the error says so. + if err := s.store.RemoveCopy(context.WithoutCancel(ctx), keep, t.id); err != nil { s.log.Error("a duplicate file was removed but its track is still in the index; a scan will flag it as missing", zap.Int64("track_id", t.id), zap.Int64("kept_track_id", keep), zap.Error(err)) return res, fmt.Errorf("the file was removed but the index could not be updated: %w", err) diff --git a/internal/duplicates/service_test.go b/internal/duplicates/service_test.go index 576c8d7..56367c0 100644 --- a/internal/duplicates/service_test.go +++ b/internal/duplicates/service_test.go @@ -495,3 +495,37 @@ func TestTheListensOfARemovedCopyMoveToTheOneThatStays(t *testing.T) { t.Errorf("the copy that stays has %d listens, want 3 (err=%v)", n, err) } } + +// cancelsBeforeUpdating ends the request at the worst moment: after the file was deleted, when the +// index is about to follow. The context the service hands to the index is the one that is cancelled, +// unless the service has made sure that what it began is finished. +type cancelsBeforeUpdating struct { + duplicates.Store + cancel context.CancelFunc +} + +func (s cancelsBeforeUpdating) RemoveCopy(ctx context.Context, keep, remove int64) error { + s.cancel() + return s.Store.RemoveCopy(ctx, keep, remove) +} + +// A client that disconnects once the file is deleted must not leave the index behind: the file is gone, +// so the track has to go too, whether or not anyone is still waiting for the answer. +func TestTheIndexIsUpdatedEvenIfTheClientLeavesAfterTheFileIsDeleted(t *testing.T) { + e := newEnv(t) + flac, mp3 := twoCopies(e) + ctx, cancel := context.WithCancel(e.ctx) + defer cancel() + svc := duplicates.NewService(cancelsBeforeUpdating{Store: e.store, cancel: cancel}, + duplicates.Options{AllowDelete: true, Logger: zap.NewNop()}) + + if _, err := svc.Clean(ctx, flac, []int64{mp3}); err != nil { + t.Fatalf("got %v: the request was cancelled, but the work had begun and has to be finished", err) + } + if e.exists("b/song.mp3") { + t.Error("the copy is still on the disk") + } + if e.tracks() != 1 { + t.Errorf("%d tracks, want 1: the file is gone but its track is still in the index", e.tracks()) + } +}