Skip to content

Commit 9993672

Browse files
committed
Thread safety
1 parent a8018e7 commit 9993672

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Foundation/URLSession/URLSession.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ open class URLSession : NSObject {
198198
/// - Note: It's a **concurrent** queue.
199199
internal let taskAttributesIsolation: DispatchQueue
200200
internal let taskRegistry = URLSession._TaskRegistry()
201+
fileprivate let identifierLock = NSLock()
201202
fileprivate let identifier: Int32
202203
fileprivate var invalidated = false
203204
fileprivate static let registerProtocols: () = {
@@ -406,9 +407,11 @@ extension URLSession._Request {
406407

407408
fileprivate extension URLSession {
408409
func createNextTaskIdentifier() -> Int {
409-
let i = nextTaskIdentifier
410-
nextTaskIdentifier += 1
411-
return i
410+
return identifierLock.synchronized {
411+
let i = nextTaskIdentifier
412+
nextTaskIdentifier += 1
413+
return i
414+
}
412415
}
413416
}
414417

0 commit comments

Comments
 (0)