From a41a294e1d969dcb5b64ae4e5290022a0518b1c1 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 25 Jun 2020 14:20:54 +0100 Subject: [PATCH] box: cancel in progress multipart uploads and copies on rclone exit #4300 --- backend/box/upload.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/backend/box/upload.go b/backend/box/upload.go index e259740c2..73390e66b 100644 --- a/backend/box/upload.go +++ b/backend/box/upload.go @@ -19,6 +19,7 @@ import ( "github.com/rclone/rclone/backend/box/api" "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/accounting" + "github.com/rclone/rclone/lib/atexit" "github.com/rclone/rclone/lib/rest" ) @@ -182,15 +183,13 @@ func (o *Object) uploadMultipart(ctx context.Context, in io.Reader, leaf, direct fs.Debugf(o, "Multipart upload session started for %d parts of size %v", session.TotalParts, fs.SizeSuffix(chunkSize)) // Cancel the session if something went wrong - defer func() { - if err != nil { - fs.Debugf(o, "Cancelling multipart upload: %v", err) - cancelErr := o.abortUpload(ctx, session.ID) - if cancelErr != nil { - fs.Logf(o, "Failed to cancel multipart upload: %v", err) - } + defer atexit.OnError(&err, func() { + fs.Debugf(o, "Cancelling multipart upload: %v", err) + cancelErr := o.abortUpload(ctx, session.ID) + if cancelErr != nil { + fs.Logf(o, "Failed to cancel multipart upload: %v", cancelErr) } - }() + })() // unwrap the accounting from the input, we use wrap to put it // back on after the buffering