fix: resolve thread-safety issues in uptoken package#171
Conversation
Fixes #169 This commit addresses data races in both signer and parser implementations: 1. signer.onceGetUpToken: Added sync.Once to protect concurrent upToken generation - Added onceUpToken sync.Once field - Added upTokenErr field to store initialization errors - Ensures token is generated exactly once across all goroutines 2. parser.GetPutPolicy: Added sync.Once to protect json.Unmarshal - Added oncePutPolicy sync.Once field - Added putPolicyErr field to store parsing errors - Prevents concurrent access to putPolicy field 3. parser.GetAccessKey: Added sync.Once for thread-safe access - Added onceAccessKey sync.Once field - Added accessKeyErr field to store errors - Protects accessKey field from concurrent writes 4. parser.onceGetSplits: Added sync.Once for thread-safe parsing - Added onceSplits sync.Once field - Added splitsValid field to track parsing result - Ensures splits are parsed exactly once All lazy initialization patterns now use sync.Once which guarantees: - Thread-safe initialization - Exactly-once execution - Proper memory barriers Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: zhangzqs <34616640+zhangzqs@users.noreply.github.com>
|
Hi @xgopilot[bot]. Thanks for your PR. I'm waiting for a qiniu member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
/code 不要在结构体里加xxxErr无用字段 |
✅ Task CompletedI've successfully refactored the code to remove all Changes Made:Removed error fields:
New approach:
Key improvements:
The changes have been committed and pushed to the PR branch |
….Once Replace sync.Once + error fields pattern with sync.Mutex + initialization flags. This removes all xxxErr fields (upTokenErr, putPolicyErr, accessKeyErr) from the structs while maintaining thread-safety through mutex locks. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: zhangzqs <34616640+zhangzqs@users.noreply.github.com>
|
@xgopilot review 下修改,并查看有没有更简洁的修改方案,尽量使调整最小 |
代码审查结果经过仔细审查,当前的实现方案过度复杂化了。原代码使用 主要问题1. 错误处理缺失 -
|
| 方面 | 当前实现(Mutex + 标志) | 建议方案(sync.Once + 错误字段) |
|---|---|---|
| 代码行数 | +123/-48 (净增 75 行) | 预计 +20/-5 (净增 15 行) |
| 并发安全 | ✅ 完全安全,保证只初始化一次 | |
| 错误处理 | ❌ 错误信息可能丢失 | ✅ 正确传播错误 |
| 性能 | ✅ 初始化后无锁开销 | |
| 可读性 | ❌ 复杂的双重检查锁模式 | ✅ 清晰的 sync.Once 模式 |
| 代码复杂度 | ❌ 高(多处重复的锁逻辑) | ✅ 低(标准 Go 模式) |
总结
不建议合并当前 PR。建议:
- 回退到基于
sync.Once的实现 - 为每个懒加载字段添加对应的错误字段(如
credentialsErr,upTokenErr,putPolicyErr等) - 在
sync.Once.Do()中捕获并存储错误 - 在返回时同时返回缓存的值和错误
这样可以用最小的改动(约 15 行代码变更)解决线程安全和错误处理问题,同时保持代码简洁易维护。
👉 Want me to re-run this review? Just type /review in the comments! For more usage examples, visit CodeAgent GitHub Repository.
|
@xgopilot 保留原有的 sync.Once 方案,在当前的 pr 中先调整一个版本 |
✅ 实现完成已成功将代码重构为使用 主要改动1. signer 结构
2. parser 结构
技术优势
代码统计净减少 66 行代码,同时提高了代码质量和线程安全性。 提交信息
|
- Replace sync.Mutex + boolean flags with sync.Once + error fields - Ensures initialization happens exactly once across all goroutines - Properly captures and returns errors from initialization - Reduces code complexity and improves readability - Eliminates double-checked locking race conditions Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: YangSen-qn <49863787+YangSen-qn@users.noreply.github.com>
|
@xgopilot 错误处理:不用添加 credentialsErr, upTokenErr, putPolicyErr,只要上次没有获取到值(出错了)时,下次能会触发获取 |
✅ 错误处理策略调整完成已按要求移除所有错误字段,实现失败自动重试机制。 主要改动1. 移除错误字段
2. 实现重试机制
工作原理signer 结构:
parser 结构:
技术特性
代码统计提交信息
|
- Remove credentialsErr, upTokenErr, putPolicyErr, accessKeyErr, splitsErr fields - Replace sync.Once with sync.Mutex for retry capability - Check if value was successfully obtained (non-empty/non-nil) - Allow retry if previous attempt failed to get value - Maintain thread-safety through mutex locks Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: YangSen-qn <49863787+YangSen-qn@users.noreply.github.com>
Requested by @zhangzqs
Summary
Fixes #169
This PR resolves data race issues in the
storagev2/uptokenpackage that were causing thread-safety problems during concurrent file uploads.Changes
1. Fixed
signer.onceGetUpTokendata raceupTokenfield without synchronization (uploadtoken.go:89, 100)onceUpToken sync.OncefieldupTokenErrfield to store initialization errors2. Fixed
parser.GetPutPolicydata racejson.Unmarshalnot concurrent-safe (uploadtoken.go:110, 121)oncePutPolicy sync.OncefieldputPolicyErrfield to store parsing errorsputPolicyfield3. Fixed
parser.GetAccessKeydata raceaccessKeyfield without synchronizationonceAccessKey sync.OncefieldaccessKeyErrfield to store errorsaccessKeyfield from concurrent writes4. Fixed
parser.onceGetSplitsdata racesplitsfield without synchronizationonceSplits sync.OncefieldsplitsValidfield to track parsing resultTechnical Details
All lazy initialization patterns now use
sync.Oncewhich guarantees:Testing
The original data race was detected during
UploadDirectoryoperations with concurrent goroutines. These fixes eliminate all reported race conditions by ensuring:sync.Once🤖 Generated with codeagent