mirror of
https://github.com/rclone/rclone.git
synced 2025-01-24 12:56:36 +02:00
onedrive: rework cancel of multipart uploads on rclone exit #4300
This now uses the atexit.OnError framework rather than a home grown one.
This commit is contained in:
parent
a41a294e1d
commit
2c10ce64aa
@ -1625,41 +1625,22 @@ func (o *Object) uploadMultipart(ctx context.Context, in io.Reader, size int64,
|
|||||||
return nil, errors.New("unknown-sized upload not supported")
|
return nil, errors.New("unknown-sized upload not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadURLChan := make(chan string, 1)
|
|
||||||
gracefulCancel := func() {
|
|
||||||
uploadURL, ok := <-uploadURLChan
|
|
||||||
// Reading from uploadURLChan blocks the atexit process until
|
|
||||||
// we are able to use uploadURL to cancel the upload
|
|
||||||
if !ok { // createUploadSession failed - no need to cancel upload
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.Debugf(o, "Cancelling multipart upload")
|
|
||||||
cancelErr := o.cancelUploadSession(ctx, uploadURL)
|
|
||||||
if cancelErr != nil {
|
|
||||||
fs.Logf(o, "Failed to cancel multipart upload: %v", cancelErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cancelFuncHandle := atexit.Register(gracefulCancel)
|
|
||||||
|
|
||||||
// Create upload session
|
// Create upload session
|
||||||
fs.Debugf(o, "Starting multipart upload")
|
fs.Debugf(o, "Starting multipart upload")
|
||||||
session, err := o.createUploadSession(ctx, modTime)
|
session, err := o.createUploadSession(ctx, modTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
close(uploadURLChan)
|
|
||||||
atexit.Unregister(cancelFuncHandle)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
uploadURL := session.UploadURL
|
uploadURL := session.UploadURL
|
||||||
uploadURLChan <- uploadURL
|
|
||||||
|
|
||||||
defer func() {
|
// Cancel the session if something went wrong
|
||||||
if err != nil {
|
defer atexit.OnError(&err, func() {
|
||||||
fs.Debugf(o, "Error encountered during upload: %v", err)
|
fs.Debugf(o, "Cancelling multipart upload: %v", err)
|
||||||
gracefulCancel()
|
cancelErr := o.cancelUploadSession(ctx, uploadURL)
|
||||||
|
if cancelErr != nil {
|
||||||
|
fs.Logf(o, "Failed to cancel multipart upload: %v", cancelErr)
|
||||||
}
|
}
|
||||||
atexit.Unregister(cancelFuncHandle)
|
})()
|
||||||
}()
|
|
||||||
|
|
||||||
// Upload the chunks
|
// Upload the chunks
|
||||||
remaining := size
|
remaining := size
|
||||||
|
Loading…
x
Reference in New Issue
Block a user