context: fix errgroup interaction with context

Fixes #3307
This commit is contained in:
Aleksandar Jankovic 2019-07-01 10:33:21 +02:00 committed by Nick Craig-Wood
parent af2596f98b
commit 7b2b396d37
2 changed files with 9 additions and 9 deletions

View file

@ -122,9 +122,9 @@ func multiThreadCopy(ctx context.Context, f fs.Fs, remote string, src fs.Object,
return nil, errors.New("multi-thread copy: can't copy zero sized file") return nil, errors.New("multi-thread copy: can't copy zero sized file")
} }
g, ctx := errgroup.WithContext(context.Background()) g, gCtx := errgroup.WithContext(ctx)
mc := &multiThreadCopyState{ mc := &multiThreadCopyState{
ctx: ctx, ctx: gCtx,
size: src.Size(), size: src.Size(),
src: src, src: src,
streams: streams, streams: streams,
@ -136,7 +136,7 @@ func multiThreadCopy(ctx context.Context, f fs.Fs, remote string, src fs.Object,
defer fs.CheckClose(mc.acc, &err) defer fs.CheckClose(mc.acc, &err)
// create write file handle // create write file handle
mc.wc, err = openWriterAt(ctx, remote, mc.size) mc.wc, err = openWriterAt(gCtx, remote, mc.size)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "multpart copy: failed to open destination") return nil, errors.Wrap(err, "multpart copy: failed to open destination")
} }
@ -146,7 +146,7 @@ func multiThreadCopy(ctx context.Context, f fs.Fs, remote string, src fs.Object,
for stream := 0; stream < mc.streams; stream++ { for stream := 0; stream < mc.streams; stream++ {
stream := stream stream := stream
g.Go(func() (err error) { g.Go(func() (err error) {
return mc.copyStream(ctx, stream) return mc.copyStream(gCtx, stream)
}) })
} }
err = g.Wait() err = g.Wait()

View file

@ -1818,18 +1818,18 @@ func DirMove(ctx context.Context, f fs.Fs, srcRemote, dstRemote string) (err err
newPath string newPath string
} }
renames := make(chan rename, fs.Config.Transfers) renames := make(chan rename, fs.Config.Transfers)
g, ctx := errgroup.WithContext(context.Background()) g, gCtx := errgroup.WithContext(context.Background())
for i := 0; i < fs.Config.Transfers; i++ { for i := 0; i < fs.Config.Transfers; i++ {
g.Go(func() error { g.Go(func() error {
for job := range renames { for job := range renames {
dstOverwritten, _ := f.NewObject(ctx, job.newPath) dstOverwritten, _ := f.NewObject(gCtx, job.newPath)
_, err := Move(ctx, f, dstOverwritten, job.newPath, job.o) _, err := Move(gCtx, f, dstOverwritten, job.newPath, job.o)
if err != nil { if err != nil {
return err return err
} }
select { select {
case <-ctx.Done(): case <-gCtx.Done():
return ctx.Err() return gCtx.Err()
default: default:
} }