diff --git a/internal/orchestrator/repo/repo.go b/internal/orchestrator/repo/repo.go index 210cbd5b..314a93a5 100644 --- a/internal/orchestrator/repo/repo.go +++ b/internal/orchestrator/repo/repo.go @@ -114,21 +114,14 @@ func (r *RepoOrchestrator) Mount(ctx context.Context) (string, func(), error) { return "", func() {}, r.mountErr } - // Give mount 60 seconds to succeed, but if it succeeds allow it to run indefinitely in the background. - mountCtx, cancel := context.WithCancel(context.Background()) - cancelMounting := time.AfterFunc(60*time.Second, func() { - cancel() - }) - - if err := r.repo.Mount(mountCtx, r.mountDir); err != nil { + mountCtx, cancel := context.WithCancel(ctx) + if err := r.repo.Mount(mountCtx, r.mountDir, 60*time.Second); err != nil { r.logger(ctx).Warn("failed to mount repo", zap.Error(err)) r.mountErr = fmt.Errorf("mount repo %v: %w", r.repoConfig.Id, err) cancel() - cancelMounting.Stop() os.Remove(r.mountDir) return "", func() {}, r.mountErr } - cancelMounting.Stop() r.mountTimer = time.AfterFunc(1000*time.Hour, func() { r.mountMu.Lock() defer r.mountMu.Unlock() diff --git a/pkg/restic/restic.go b/pkg/restic/restic.go index 7029854d..45fbc6e1 100644 --- a/pkg/restic/restic.go +++ b/pkg/restic/restic.go @@ -468,7 +468,7 @@ func (r *Repo) Stats(ctx context.Context, opts ...GenericOption) (*RepoStats, er return &stats, nil } -func (r *Repo) Mount(ctx context.Context, dir string, opts ...GenericOption) error { +func (r *Repo) Mount(ctx context.Context, dir string, mountTimeout time.Duration, opts ...GenericOption) error { // Check if already mounted if info, err := os.Stat(dir); err == nil { if info.IsDir() { @@ -489,11 +489,17 @@ func (r *Repo) Mount(ctx context.Context, dir string, opts ...GenericOption) err ticker := time.NewTicker(100 * time.Millisecond) defer ticker.Stop() + timeout := time.NewTimer(mountTimeout) + defer timeout.Stop() + for { select { case <-ctx.Done(): cmd.Process.Kill() return ctx.Err() + case <-timeout.C: + cmd.Process.Kill() + return fmt.Errorf("mount timeout") case <-ticker.C: if _, err := os.Stat(dir); err == nil { if ents, err := os.ReadDir(filepath.Join(dir, "ids")); err == nil && len(ents) > 0 {