fix(repository): fixed 'context canceled' regression (#1799)

This was broken in #1758 by holding onto a context beyond its intended
lifetime.

Fixes #1796
This commit is contained in:
Jarek Kowalski authored and GitHub committed 2022-03-03 21:02:40 -08:00
1 parent 192191e056
commit 6baf10ad31
2 files changed
+19 -5

No files matched your search

+14 -5
View File
@@ -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)
}
+5
View File
@@ -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{}
}