-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix HTTPCookieStorage threading issues #1182
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
fix HTTPCookieStorage threading issues #1182
Conversation
@swift-ci please test |
@mamabusi FYI |
@parkera / @pushkarnk sorry for prodding, mind letting me know what you think here? I want to some more concurrency fixes and the more open PRs I have in this area the harder it gets as they'll start colliding. |
@@ -103,10 +121,12 @@ open class HTTPCookieStorage: NSObject { | |||
*/ | |||
open class var shared: HTTPCookieStorage { | |||
get { | |||
if sharedStorage == nil { | |||
sharedStorage = HTTPCookieStorage(cookieStorageName: "shared") | |||
return sharedSyncQ.sync { |
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.
this looks like a dispatch_once style; shouldn't that be fine as a lazy var?
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.
- lazy vars aren't thread safe (https://bugs.swift.org/browse/SR-1042)
- there's no
dispatch_once
in Swift
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.
@weissi - How about open static var shared: HTTPCookieStorage?
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { | ||
dispatchPrecondition(condition: DispatchPredicate.onQueue(self.syncQ)) | ||
} | ||
return self._allCookies |
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.
self.
is not needed here
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.
what's wrong with the self.
? I think that's actually a very important piece of information. Otherwise it just looks like a local variable. The self.
makes it very implicit that this is a class property and I believe that makes it more obvious to others that there are possible concurrency concerns.
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { | ||
dispatchPrecondition(condition: DispatchPredicate.onQueue(self.syncQ)) | ||
} | ||
self._allCookies = newValue |
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.
again self.
is redundant
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.
see above
for key in cookies0.keys { | ||
if let cookie = createCookie(cookies0[key]!) { | ||
allCookies[key] = cookie | ||
self.syncQ.sync { |
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.
again self.
is redundant
theCookies = Array(self.allCookies.values) | ||
} | ||
return theCookies | ||
return Array(self.syncQ.sync { self.allCookies.values }) |
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.
ditto on self.
@@ -138,7 +160,7 @@ open class HTTPCookieStorage: NSObject { | |||
same name, domain and path, if any. | |||
*/ | |||
open func setCookie(_ cookie: HTTPCookie) { | |||
workQueue.sync { | |||
self.syncQ.sync { |
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.
ditto on self.
private var _allCookies: [String: HTTPCookie] | ||
private var allCookies: [String: HTTPCookie] { | ||
get { | ||
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { |
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.
what about linux?
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.
@phausler Linux seems to be handled in the *
:
$ cat test.swift
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
print("yes")
} else {
print("no")
}
$ swift test.swift
yes
$ uname -s
Linux
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.
I agree to @weissi
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { | ||
dispatchPrecondition(condition: DispatchPredicate.onQueue(self.syncQ)) | ||
} | ||
|
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.
Are we not supporting linux?
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.
the *
seems to match Linux
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.
@weissi - Yup agreed.
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.
LGTM
@swift-ci please test and merge |
|
there seem to be lots of files missing for some reason. Suspecting something went wrong with Jenkins' workspace. Retrying the tests. |
@swift-ci please test and merge |
HTTPCookieStorage
isn't thread-safe at all but should be.