Skip to content

fix(drivers): stop the upload retry loop when the context is canceled - #2892

Open
beardthelion wants to merge 1 commit into
OpenListTeam:mainfrom
beardthelion:fix/ineffective-break-upload-retry
Open

fix(drivers): stop the upload retry loop when the context is canceled#2892
beardthelion wants to merge 1 commit into
OpenListTeam:mainfrom
beardthelion:fix/ineffective-break-upload-retry

Conversation

@beardthelion

Copy link
Copy Markdown

Summary / 摘要

The 115 and pikpak multipart uploaders check for cancellation with case <-ctx.Done() inside a select, and break there only leaves the select, not the enclosing for retry := 0; retry < 3; retry++ loop. Cancelling an upload therefore did not stop the retry loop.

The visible effect is wasted work rather than a stuck transfer. RateLimitReader.Read checks Ctx.Err() first (internal/stream/limit.go:43-46), so no part data actually goes out after a cancel. But every remaining chunk still runs all three attempts, each allocating a chunk.Size buffer and reading that many bytes off disk before firing a request that cannot succeed, and each chunk then reports an OSS transport error instead of the cancellation. chunksProducer is not context aware, so this repeats for every chunk left in the channel, times ThreadsNum workers.

The fix moves the check ahead of the select and uses utils.IsCanceled, which is the pattern the other drivers already use for this (drivers/doubao/util.go, drivers/onedrive/util.go, drivers/189_tv/utils.go, and others). Assigning ctx.Err() to err is load-bearing: without it a cancelled chunk falls through to the success branch, counts toward progress, and appends a zero-value oss.UploadPart to parts.

Scope is deliberately limited to the ineffective break. chunksProducer staying context unaware, and the case <-errCh: return nil, err consumer discarding the received value, are both pre-existing and left alone.

  • This PR has breaking changes.
    / 此 PR 包含破坏性变更。
  • This PR changes public API, config, storage format, or migration behavior.
    / 此 PR 修改了公开 API、配置、存储格式或迁移行为。
  • This PR requires corresponding changes in related repositories.
    / 此 PR 需要关联仓库同步修改。

Testing / 测试

Found with staticcheck (SA4011), which flagged both sites and reports clean after the change:

$ staticcheck -checks SA4011 ./...
drivers/115/util.go:342:7: ineffective break statement. Did you mean to break out of the outer loop? (SA4011)
drivers/pikpak/util.go:508:7: ineffective break statement. Did you mean to break out of the outer loop? (SA4011)

# after
$ staticcheck -checks SA4011 ./...
$

A full staticcheck ./... and go vet ./... before and after show no new findings, only those two removed. go build ./... and gofmt are clean.

go test ./... was run. It is not fully green on 940b69fe either: TestNewOSSClientUsesEnvironmentHTTPSProxy in internal/net fails identically before and after this change, and several drivers fail to build under vet for pre-existing non-constant format string errors. Neither driver touched here has tests, and UploadByMultipart needs a live bucket, so there is no unit test to add without restructuring the upload path. I did not want to grow the diff for that. Happy to if you would prefer it.

  • go test ./...
  • Manual test / 手动测试:

Checklist / 检查清单

  • I have read CONTRIBUTING.
    / 我已阅读 CONTRIBUTING
  • I confirm this contribution follows the repository license, contribution policy, and code of conduct.
    / 我确认此贡献符合仓库许可证、贡献规范和行为准则。
  • I have formatted the changed code with gofmt, go fmt, or prettier where applicable.
    / 我已按适用情况使用 gofmtgo fmtprettier 格式化变更代码。

AI Disclosure / AI 使用声明

  • This PR includes AI-assisted content.
    / 此 PR 包含 AI 辅助内容。

Tools used / 使用工具:

  • Other (please specify) / 其他(请注明): Claude and Grok, plus staticcheck for the original detection.

Usage scope / 使用范围:

staticcheck surfaced the defect. AI assistance was used for the root cause investigation and the patch, and a second model from a different family reviewed the change independently. The reporter reviewed and validated the change before submission.

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant