Swift-线程竞争的线程竞赛

发布于 2025-01-24 01:53:46 字数 773 浏览 1 评论 0原文

我在项目上正在运行线程速度,并且结果非常不一致。我的设置如下:

  1. 围绕Urlsession的包装器,该urlsession报告了后退任务及其状态。我通过:
actor MyMonitor {
  func getTasks() async -> [String: URLSessionTask] {
    await mySession.allTasks.reduce(into: [String: URLSessionTask]()) { newMap, element in
        if let taskDescription = element.taskDescription {
                newMap[taskDescription] = element
       }
    }
  }
}

对于我的单元测试,我有一个URLProtocol来嘲笑实际的工作请求。但是,似乎当我调用getTasks函数时,我会获得数据竞赛。 (尽管不一致)

TSAN工具指出种族在上述片段中:

线程问题:(2)中的数据竞赛等待恢复部分功能 myproject.mymonitor.getTasks()async-> swift.dictionary< swift.string, __c.nsurlsessionTask>请致电0x7B6000021400

这是一个假阳性,因为我没有控制Ove urlsession的螺纹?任何帮助将不胜感激。

I am running ThreadSanitizer on my project and I am getting some very inconsistent results. My setup is as follows:

  1. A wrapper around a URLSession which reports back tasks and their status. I get the tasks via:
actor MyMonitor {
  func getTasks() async -> [String: URLSessionTask] {
    await mySession.allTasks.reduce(into: [String: URLSessionTask]()) { newMap, element in
        if let taskDescription = element.taskDescription {
                newMap[taskDescription] = element
       }
    }
  }
}

For my unit tests, I have a URLProtocol to mock an actual working request. However, it seems when I call the getTasks function, I get a data race. (albeit inconsistent)

The TSan tool states the race is in the above snippet:

Threading Issues: Data race in (2) await resume partial function for
MyProject.MyMonitor.getTasks() async -> Swift.Dictionary<Swift.String,
__C.NSURLSessionTask> at 0x7b6000021400

Is this a false positive, because I do not have control ove URLSession's threading? Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文