From 6baf10ad31ceac13aba52a3e11388d910e522f7f Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Thu, 3 Mar 2022 21:02:40 -0800 Subject: [PATCH] fix(repository): fixed 'context canceled' regression (#1799) This was broken in #1758 by holding onto a context beyond its intended lifetime. Fixes #1796 --- internal/repotesting/repotesting.go | 19 ++++++++++++++----- repo/open.go | 5 +++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/repotesting/repotesting.go b/internal/repotesting/repotesting.go index 0bfd69211..fa7860d09 100644 --- a/internal/repotesting/repotesting.go +++ b/internal/repotesting/repotesting.go @@ -7,6 +7,8 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/require" + "github.com/kopia/kopia/internal/blobtesting" "github.com/kopia/kopia/internal/testlogging" "github.com/kopia/kopia/internal/testutil" @@ -101,10 +103,13 @@ func (e *Environment) setup(tb testing.TB, version content.FormatVersion, opts . e.connected = true - rep, err := repo.Open(ctx, e.ConfigFile(), e.Password, openOpt) - if err != nil { - tb.Fatalf("can't open: %v", err) - } + // ensure context passed to Open() is not used beyond its scope. + ctx2, cancel := context.WithCancel(ctx) + defer cancel() + + rep, err := repo.Open(ctx2, e.ConfigFile(), e.Password, openOpt) + + require.NoError(tb, err) e.Repository = rep @@ -154,7 +159,11 @@ func (e *Environment) MustReopen(tb testing.TB, openOpts ...func(*repo.Options)) tb.Fatalf("close error: %v", err) } - rep, err := repo.Open(ctx, e.ConfigFile(), e.Password, repoOptions(openOpts)) + // ensure context passed to Open() is not used for cancelation signal. + ctx2, cancel := context.WithCancel(ctx) + cancel() + + rep, err := repo.Open(ctx2, e.ConfigFile(), e.Password, repoOptions(openOpts)) if err != nil { tb.Fatalf("err: %v", err) } diff --git a/repo/open.go b/repo/open.go index 838f24559..49900ffd7 100644 --- a/repo/open.go +++ b/repo/open.go @@ -15,6 +15,7 @@ import ( "github.com/kopia/kopia/internal/atomicfile" "github.com/kopia/kopia/internal/cache" "github.com/kopia/kopia/internal/clock" + "github.com/kopia/kopia/internal/ctxutil" "github.com/kopia/kopia/internal/epoch" "github.com/kopia/kopia/internal/gather" "github.com/kopia/kopia/internal/retry" @@ -89,6 +90,10 @@ func Open(ctx context.Context, configFile, password string, options *Options) (r } }() + // ignore cancellation/timeout from the provided context, since we may be spawning + // goroutines that should survive when the provided context is done. + ctx = ctxutil.Detach(ctx) + if options == nil { options = &Options{} }