Skip to content

Commit eea8fee

Browse files
fix(drivers): stop the upload retry loop when the context is canceled (#2892)
The 115 and pikpak multipart uploaders checked for cancellation with a `case <-ctx.Done()` inside a select, and a break there only leaves the select, not the enclosing `for retry := 0; retry < 3; retry++` loop. Cancelling an upload therefore ran all three attempts for every remaining chunk, each allocating a chunk-sized buffer and reading it off disk before firing a request that could not succeed, and reported a transport error instead of the cancellation. Move the check ahead of the select and use utils.IsCanceled, matching the pattern the other drivers already use. Assigning ctx.Err() to err is load-bearing: without it a cancelled chunk takes the success branch, counts toward progress, and appends a zero-value UploadPart. Found with staticcheck (SA4011). Co-authored-by: Pikachu Ren <40362270+PIKACHUIM@users.noreply.github.com>
1 parent c066569 commit eea8fee

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎drivers/115/util.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,11 @@ func (d *Pan115) UploadByMultipart(ctx context.Context, params *driver115.Upload
337337
for chunk := range chunksCh {
338338
var part oss.UploadPart // 出现错误就继续尝试,共尝试3次
339339
for retry := 0; retry < 3; retry++ {
340-
select {
341-
case <-ctx.Done():
340+
if utils.IsCanceled(ctx) {
341+
err = ctx.Err()
342342
break
343+
}
344+
select {
343345
case <-ticker.C:
344346
if ossToken, err = d.client.GetOSSToken(); err != nil { // 到时重新获取ossToken
345347
errCh <- errors.Wrap(err, "刷新token时出现错误")

‎drivers/pikpak/util.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,11 @@ func (d *PikPak) UploadByMultipart(ctx context.Context, params *S3Params, fileSi
503503
for chunk := range chunksCh {
504504
var part oss.UploadPart // 出现错误就继续尝试,共尝试3次
505505
for retry := 0; retry < 3; retry++ {
506-
select {
507-
case <-ctx.Done():
506+
if utils.IsCanceled(ctx) {
507+
err = ctx.Err()
508508
break
509+
}
510+
select {
509511
case <-ticker.C:
510512
errCh <- errors.Wrap(err, "ossToken 过期")
511513
default:

0 commit comments

Comments
 (0)