Skip to content

Conversation

@xgopilot
Copy link

@xgopilot xgopilot bot commented Nov 11, 2025

Requested by @zhangzqs

修复问题

修复了storagev2中uplog功能存在的数据竞争问题。

问题分析

根据提供的data race报告,问题出现在internal/uplog/request_uplog.go中:

  • 竞争位置:

    • 读取:goroutine在json.Marshal(uplog)时读取ConnectElapsedTime等timing字段
    • 写入:另一个goroutine在httptrace.ClientTrace回调中写入这些字段
  • 根本原因: 多个goroutine同时访问RequestUplog结构体的timing字段(uint64类型),造成并发读写竞争

解决方案

  1. 使用原子操作: 将所有并发访问的timing字段的写入操作替换为atomic.StoreUint64
  2. 创建安全快照: 添加atomicSnapshot方法,在JSON序列化前使用atomic.LoadUint64创建线程安全的结构体副本
  3. 修复的字段:
    • DNSElapsedTime
    • ConnectElapsedTime
    • TLSConnectElapsedTime
    • RequestElapsedTime
    • WaitElapsedTime
    • ResponseElapsedTime
    • TotalElapsedTime

测试

  • ✅ 项目编译成功
  • ✅ 没有引入新的依赖
  • ✅ 使用Go标准库的atomic包,性能开销最小

兼容性

此修复完全向后兼容,不会影响现有的API或行为,只是确保了线程安全。

Fixes #174

- Use atomic operations for concurrent access to timing fields
- Add atomicSnapshot method for thread-safe JSON marshaling
- Prevent race conditions in httptrace callbacks

Fixes #174

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: zhangzqs <[email protected]>
@qiniu-prow
Copy link

qiniu-prow bot commented Nov 11, 2025

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storagev2的uplog存在data race问题

2 participants