From 4d579c43870106fe371ec246cc5501ee83154734 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Fri, 5 Nov 2021 22:55:39 -0400 Subject: [PATCH] check: wait for progress bar output Further code will also output to the terminal and the bar's cursor positioning causes its output to overlap with the remaining output in a racy way. Fixes: #3344 --- cmd/restic/cmd_check.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index f5b6598a5..61bbd07ff 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -5,6 +5,7 @@ import ( "math/rand" "strconv" "strings" + "sync" "time" "github.com/spf13/cobra" @@ -252,7 +253,11 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { Verbosef("check snapshots, trees and blobs\n") errChan = make(chan error) + var wg sync.WaitGroup + + wg.Add(1) go func() { + defer wg.Done() bar := newProgressMax(!gopts.Quiet, 0, "snapshots") defer bar.Done() chkr.Structure(gopts.ctx, bar, errChan) @@ -270,6 +275,11 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { } } + // Wait for the progress bar to be complete before printing more below. + // Must happen after `errChan` is read from in the above loop to avoid + // deadlocking in the case of errors. + wg.Wait() + if opts.CheckUnused { for _, id := range chkr.UnusedBlobs(gopts.ctx) { Verbosef("unused blob %v\n", id)