-
Notifications
You must be signed in to change notification settings - Fork 1.2k
improve threading correctness in URLSession #1186
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
weissi
merged 1 commit into
swiftlang:master
from
weissi:jw-improve-urlsession-threading
Aug 23, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,13 +34,10 @@ open class URLSessionTask : NSObject, NSCopying { | |
internal var session: URLSessionProtocol! //change to nil when task completes | ||
internal let body: _Body | ||
fileprivate var _protocol: URLProtocol! = nil | ||
private let syncQ = DispatchQueue(label: "org.swift.URLSessionTask.SyncQ") | ||
|
||
/// All operations must run on this queue. | ||
internal let workQueue: DispatchQueue | ||
/// Using dispatch semaphore to make public attributes thread safe. | ||
/// A semaphore is a simpler option against the usage of concurrent queue | ||
/// as the critical sections are very short. | ||
fileprivate let semaphore = DispatchSemaphore(value: 1) | ||
|
||
public override init() { | ||
// Darwin Foundation oddly allows calling this initializer, even though | ||
|
@@ -66,7 +63,8 @@ open class URLSessionTask : NSObject, NSCopying { | |
} | ||
internal init(session: URLSession, request: URLRequest, taskIdentifier: Int, body: _Body) { | ||
self.session = session | ||
self.workQueue = session.workQueue | ||
/* make sure we're actually having a serial queue as it's used for synchronization */ | ||
self.workQueue = DispatchQueue.init(label: "org.swift.URLSessionTask.WorkQueue", target: session.workQueue) | ||
self.taskIdentifier = taskIdentifier | ||
self.originalRequest = request | ||
self.body = body | ||
|
@@ -112,31 +110,19 @@ open class URLSessionTask : NSObject, NSCopying { | |
/// May differ from originalRequest due to http server redirection | ||
/*@NSCopying*/ open internal(set) var currentRequest: URLRequest? { | ||
get { | ||
semaphore.wait() | ||
defer { | ||
semaphore.signal() | ||
} | ||
return self._currentRequest | ||
return self.syncQ.sync { return self._currentRequest } | ||
} | ||
set { | ||
semaphore.wait() | ||
self._currentRequest = newValue | ||
semaphore.signal() | ||
self.syncQ.sync { self._currentRequest = newValue } | ||
} | ||
} | ||
fileprivate var _currentRequest: URLRequest? = nil | ||
/*@NSCopying*/ open internal(set) var response: URLResponse? { | ||
get { | ||
semaphore.wait() | ||
defer { | ||
semaphore.signal() | ||
} | ||
return self._response | ||
return self.syncQ.sync { return self._response } | ||
} | ||
set { | ||
semaphore.wait() | ||
self._response = newValue | ||
semaphore.signal() | ||
self.syncQ.sync { self._response = newValue } | ||
} | ||
} | ||
fileprivate var _response: URLResponse? = nil | ||
|
@@ -149,33 +135,21 @@ open class URLSessionTask : NSObject, NSCopying { | |
/// Number of body bytes already received | ||
open internal(set) var countOfBytesReceived: Int64 { | ||
get { | ||
semaphore.wait() | ||
defer { | ||
semaphore.signal() | ||
} | ||
return self._countOfBytesReceived | ||
return self.syncQ.sync { return self._countOfBytesReceived } | ||
} | ||
set { | ||
semaphore.wait() | ||
self._countOfBytesReceived = newValue | ||
semaphore.signal() | ||
self.syncQ.sync { self._countOfBytesReceived = newValue } | ||
} | ||
} | ||
fileprivate var _countOfBytesReceived: Int64 = 0 | ||
|
||
/// Number of body bytes already sent */ | ||
open internal(set) var countOfBytesSent: Int64 { | ||
get { | ||
semaphore.wait() | ||
defer { | ||
semaphore.signal() | ||
} | ||
return self._countOfBytesSent | ||
return self.syncQ.sync { return self._countOfBytesSent } | ||
} | ||
set { | ||
semaphore.wait() | ||
self._countOfBytesSent = newValue | ||
semaphore.signal() | ||
self.syncQ.sync { self._countOfBytesSent = newValue } | ||
} | ||
} | ||
|
||
|
@@ -215,16 +189,10 @@ open class URLSessionTask : NSObject, NSCopying { | |
*/ | ||
open var state: URLSessionTask.State { | ||
get { | ||
semaphore.wait() | ||
defer { | ||
semaphore.signal() | ||
} | ||
return self._state | ||
return self.syncQ.sync { self._state } | ||
} | ||
set { | ||
semaphore.wait() | ||
self._state = newValue | ||
semaphore.signal() | ||
self.syncQ.sync { self._state = newValue } | ||
} | ||
} | ||
fileprivate var _state: URLSessionTask.State = .suspended | ||
|
@@ -305,16 +273,10 @@ open class URLSessionTask : NSObject, NSCopying { | |
/// URLSessionTask.highPriority, but use is not restricted to these. | ||
open var priority: Float { | ||
get { | ||
semaphore.wait() | ||
defer { | ||
semaphore.signal() | ||
} | ||
return self._priority | ||
return self.workQueue.sync { return self._priority } | ||
} | ||
set { | ||
semaphore.wait() | ||
self._priority = newValue | ||
semaphore.signal() | ||
self.workQueue.sync { self._priority = newValue } | ||
} | ||
} | ||
fileprivate var _priority: Float = URLSessionTask.defaultPriority | ||
|
@@ -565,7 +527,9 @@ extension _ProtocolClient : URLProtocolClient { | |
session.delegateQueue.addOperation { | ||
delegate.urlSession(session, task: task, didCompleteWithError: nil) | ||
task.state = .completed | ||
session.taskRegistry.remove(task) | ||
task.workQueue.async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will fix an extremely intermittent crash a few of us have seen. |
||
session.taskRegistry.remove(task) | ||
} | ||
} | ||
case .noDelegate: | ||
task.state = .completed | ||
|
@@ -616,7 +580,9 @@ extension _ProtocolClient : URLProtocolClient { | |
session.delegateQueue.addOperation { | ||
delegate.urlSession(session, task: task, didCompleteWithError: error as Error) | ||
task.state = .completed | ||
session.taskRegistry.remove(task) | ||
task.workQueue.async { | ||
session.taskRegistry.remove(task) | ||
} | ||
} | ||
case .noDelegate: | ||
task.state = .completed | ||
|
@@ -625,7 +591,9 @@ extension _ProtocolClient : URLProtocolClient { | |
session.delegateQueue.addOperation { | ||
completion(nil, nil, error) | ||
task.state = .completed | ||
session.taskRegistry.remove(task) | ||
task.workQueue.async { | ||
session.taskRegistry.remove(task) | ||
} | ||
} | ||
case .downloadCompletionHandler(let completion): | ||
session.delegateQueue.addOperation { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. However, I recall we had replaced the use of DispatchQueues with semaphores in attempts to work around a weird and intermittent hang in
DispatchQueue.async
- something we could reproduce only in a couple of local environments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pushkarnk well, if that's the case I want to know and I'll fix the issue.