Skip to content

SR-11854: Fix concurrent operations execution in OperationQueue #2937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/Foundation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -938,15 +938,15 @@ open class OperationQueue : NSObject, ProgressReporting {
let queue: DispatchQueue
if let qos = _propertyQoS {
if let name = __name {
queue = DispatchQueue(label: name, qos: qos.qosClass)
queue = DispatchQueue(label: name, qos: qos.qosClass, attributes: .concurrent)
} else {
queue = DispatchQueue(label: "NSOperationQueue \(Unmanaged.passUnretained(self).toOpaque())", qos: qos.qosClass)
queue = DispatchQueue(label: "NSOperationQueue \(Unmanaged.passUnretained(self).toOpaque())", qos: qos.qosClass, attributes: .concurrent)
}
} else {
if let name = __name {
queue = DispatchQueue(label: name)
queue = DispatchQueue(label: name, attributes: .concurrent)
} else {
queue = DispatchQueue(label: "NSOperationQueue \(Unmanaged.passUnretained(self).toOpaque())")
queue = DispatchQueue(label: "NSOperationQueue \(Unmanaged.passUnretained(self).toOpaque())", attributes: .concurrent)
}
}
__backingQueue = queue
Expand Down
54 changes: 54 additions & 0 deletions Tests/Foundation/Tests/TestOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class TestOperationQueue : XCTestCase {
("test_CustomOperationReady", test_CustomOperationReady),
("test_DependencyCycleBreak", test_DependencyCycleBreak),
("test_Lifecycle", test_Lifecycle),
("test_ConcurrentOperations", test_ConcurrentOperations),
("test_ConcurrentOperationsWithDependenciesAndCompletions", test_ConcurrentOperationsWithDependenciesAndCompletions),
]
}

Expand Down Expand Up @@ -699,6 +701,58 @@ class TestOperationQueue : XCTestCase {
Thread.sleep(forTimeInterval: 1) // Let queue to be deallocated
XCTAssertNil(weakQueue, "Queue should be deallocated at this point")
}

func test_ConcurrentOperations() {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 2

// Running several iterations helps to reveal use-after-dealloc crashes
for _ in 0..<3 {
let didRunOp1 = expectation(description: "Did run first operation")
let didRunOp2 = expectation(description: "Did run second operation")

queue.addOperation {
self.wait(for: [didRunOp2], timeout: 0.2)
didRunOp1.fulfill()
}
queue.addOperation {
didRunOp2.fulfill()
}

self.wait(for: [didRunOp1], timeout: 0.3)
}
}

func test_ConcurrentOperationsWithDependenciesAndCompletions() {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 2

// Running several iterations helps to reveal use-after-dealloc crashes
for _ in 0..<3 {
let didRunOp1 = expectation(description: "Did run first operation")
let didRunOp1Completion = expectation(description: "Did run first operation completion")
let didRunOp1Dependency = expectation(description: "Did run first operation dependency")
let didRunOp2 = expectation(description: "Did run second operation")

let op1 = BlockOperation {
self.wait(for: [didRunOp1Dependency, didRunOp2], timeout: 0.2)
didRunOp1.fulfill()
}
op1.completionBlock = {
didRunOp1Completion.fulfill()
}
let op1Dependency = BlockOperation {
didRunOp1Dependency.fulfill()
}
queue.addOperations([op1, op1Dependency], waitUntilFinished: false)
queue.addOperation {
didRunOp2.fulfill()
}

self.wait(for: [didRunOp1, didRunOp1Completion], timeout: 0.3)
}
}

}

class AsyncOperation: Operation {
Expand Down