diff --git a/changelog/unreleased/issue-1978 b/changelog/unreleased/issue-1978 new file mode 100644 index 000000000..b3f5101ba --- /dev/null +++ b/changelog/unreleased/issue-1978 @@ -0,0 +1,12 @@ +Bugfix: Do not return an error when the scanner is faster than backup + +When restic makes a backup, there's a background task called "scanner" which +collects information on how many files and directories are to be saved, in +order to display progress information to the user. When the backup finishes +faster than the scanner, it is aborted because the result is not needed any +more. This logic contained a bug, where quitting the scanner process was +treated as an error, and caused restic to print an unhelpful error message +("context canceled"). + +https://github.com/restic/restic/issues/1978 +https://github.com/restic/restic/pull/1991 diff --git a/internal/archiver/scanner.go b/internal/archiver/scanner.go index ba3d5c60b..bd789893c 100644 --- a/internal/archiver/scanner.go +++ b/internal/archiver/scanner.go @@ -52,20 +52,17 @@ func (s *Scanner) Scan(ctx context.Context, targets []string) error { } if ctx.Err() != nil { - return ctx.Err() + return nil } } - if ctx.Err() != nil { - return ctx.Err() - } s.Result("", stats) return nil } func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (ScanStats, error) { if ctx.Err() != nil { - return stats, ctx.Err() + return stats, nil } // exclude files by path before running stat to reduce number of lstat calls @@ -89,10 +86,6 @@ func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (Sca stats.Files++ stats.Bytes += uint64(fi.Size()) case fi.Mode().IsDir(): - if ctx.Err() != nil { - return stats, ctx.Err() - } - names, err := readdirnames(s.FS, target) if err != nil { return stats, s.Error(target, fi, err) @@ -109,9 +102,6 @@ func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (Sca stats.Others++ } - if ctx.Err() != nil { - return stats, ctx.Err() - } s.Result(target, stats) return stats, nil } diff --git a/internal/archiver/scanner_test.go b/internal/archiver/scanner_test.go index f02546f3c..a171df5f6 100644 --- a/internal/archiver/scanner_test.go +++ b/internal/archiver/scanner_test.go @@ -289,7 +289,7 @@ func TestScannerCancel(t *testing.T) { "other": TestFile{Content: "other"}, } - result := ScanStats{Files: 2, Bytes: 6} + result := ScanStats{Files: 2, Dirs: 1, Bytes: 6} ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -319,12 +319,8 @@ func TestScannerCancel(t *testing.T) { } err = sc.Scan(ctx, []string{"."}) - if err == nil { - t.Errorf("did not find expected error") - } - - if err != context.Canceled { - t.Errorf("unexpected error found, want %v, got %v", context.Canceled, err) + if err != nil { + t.Errorf("unexpected error %v found", err) } if lastStats != result {