Skip to content

Commit 05a70f6

Browse files
johnno1962Pushkar Kulkarni
authored andcommitted
Thread safety
1 parent 0a66c40 commit 05a70f6

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
@@ -185,6 +185,7 @@ public let NSURLSessionTransferSizeUnknown: Int64 = -1
185185
open class URLSession : NSObject {
186186
fileprivate let _configuration: _Configuration
187187
fileprivate let multiHandle: _MultiHandle
188+
fileprivate let taskIdentifierLock = NSLock()
188189
fileprivate var nextTaskIdentifier = 1
189190
internal let workQueue: DispatchQueue
190191
/// This queue is used to make public attributes on `URLSessionTask` instances thread safe.
@@ -399,9 +400,11 @@ extension URLSession._Request {
399400

400401
fileprivate extension URLSession {
401402
func createNextTaskIdentifier() -> Int {
402-
let i = nextTaskIdentifier
403-
nextTaskIdentifier += 1
404-
return i
403+
return taskIdentifierLock.synchronized {
404+
let i = nextTaskIdentifier
405+
nextTaskIdentifier += 1
406+
return i
407+
}
405408
}
406409
}
407410

0 commit comments

Comments
 (0)