Skip to content

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
merged 1 commit into from
Aug 23, 2017
Merged

improve threading correctness in URLSession #1186

merged 1 commit into from
Aug 23, 2017

Conversation

weissi
Copy link
Contributor

@weissi weissi commented Aug 22, 2017

this is really only the beginning to prevent everything from breaking with the simplest test that does use more than one thread. Thread-safely seems to be large absent in this part of foundation 😢.
Unfortunately, I seem to be going down a rabbit hole here but it's a start.

@weissi weissi requested a review from pushkarnk August 22, 2017 17:51
@@ -555,7 +517,9 @@ extension _ProtocolClient : URLProtocolClient {
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: nil)
task.state = .completed
session.taskRegistry.remove(task)
task.workQueue.async {
Copy link
Member

Choose a reason for hiding this comment

The 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.

@pushkarnk
Copy link
Member

@swift-ci please test

@@ -62,7 +59,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)
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -171,12 +171,13 @@
import CoreFoundation
import Dispatch


fileprivate let globalVarSyncQ = DispatchQueue(label: "org.swift.Foundation.URLSession.GlobalVarSyncQ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This was long pending and ignored :(

@weissi
Copy link
Contributor Author

weissi commented Aug 23, 2017

@swift-ci please test

1 similar comment
@weissi
Copy link
Contributor Author

weissi commented Aug 23, 2017

@swift-ci please test

@ianpartridge
Copy link
Contributor

@weissi thank you for exploring this rabbit hole 😄 Really hope you continue to improve this area of the codebase 👍

@weissi
Copy link
Contributor Author

weissi commented Aug 23, 2017

tests all passed and approved, so merging.

@weissi weissi merged commit 3f0ff62 into swiftlang:master Aug 23, 2017
@weissi weissi deleted the jw-improve-urlsession-threading branch August 23, 2017 14:14
pushkarnk pushed a commit to pushkarnk/swift-corelibs-foundation that referenced this pull request Oct 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants