Hi, we have encountered an almost 100% reproducible deadlock issue on an iPhone 7 running iOS 15.7.x.
We have an object with a field that is thread-safe, where we use queue.sync {}
for reading and queue.async(flags: .barrier)
for writing. The queue is a concurrent queue.
When using it externally wrapped in Task(or Task.detached)+withTaskGroup
, we experience a deadlock on iPhone 7 (iOS 15.7.x). The code inside queue.async
is never executed, and Task.Detached
never completes its task.
However, we do not encounter this issue on other devices or simulators.
We are unsure if this issue is related to iOS or Xcode. The code is as follows:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Task.detached {
let manager = Manager()
await withTaskGroup(of: Void.self, returning: Void.self) { taskGroup in
taskGroup.addTask {
manager.get()
}
taskGroup.addTask {
manager.get()
}
taskGroup.addTask {
manager.get()
}
}
print("OK") // On iPhone 7 + iOS 15.x.x, this won't print "OK"
}
}
}
private let managerQueue = DispatchQueue(label: "ManagerQueue", qos: .default, attributes: .concurrent)
class Manager {
private(set) var int: Int = 0
func get() -> Int {
let value = managerQueue.sync {
return int
}
set()
return value
}
func set() {
print("Enter Set")
managerQueue.async(flags: .barrier) {
self.int += 1
print("Done Set") // On iPhone 7 + iOS 15.x.x, this won't be executed
}
}
}
Please note that this issue occurs consistently on iPhone 7 with iOS 15.7.x but not on other devices or simulators. We suspect that it may be related to either iOS or Xcode.