Skip to content

URLSession's delegateQueue should be serial as per the spec. #983

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
May 16, 2017

Conversation

mamabusi
Copy link
Contributor

Refer to the API document that mentions URLSession's delegateQueue should be serial:
https://developer.apple.com/reference/foundation/urlsession/1411597-init

@@ -226,6 +227,7 @@ open class URLSession : NSObject {
self.workQueue = DispatchQueue(label: "URLSession<\(identifier)>")
self.taskAttributesIsolation = DispatchQueue(label: "URLSession<\(identifier)>.taskAttributes", attributes: DispatchQueue.Attributes.concurrent)
self.delegateQueue = queue ?? OperationQueue()
self.delegateQueue.maxConcurrentOperationCount = 1
Copy link
Contributor

@ianpartridge ianpartridge May 15, 2017

Choose a reason for hiding this comment

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

This changes the user's OperationQueue to be serial if it isn't already. Does Darwin do that?

Copy link
Contributor Author

@mamabusi mamabusi May 16, 2017

Choose a reason for hiding this comment

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

The spec. says "The queue should be a serial queue, in order to ensure the correct ordering of callbacks". But yes, it is not mandatory and user can send in a concurrent queue as well. Tested with a small test-case on Darwin and I see that user's OperationQueue is not converted to serial if it isn't already. Thanks @ianpartridge for pointing this out.
Modified the changes for the same.

@mamabusi mamabusi force-pushed the urlSesSerialDelegQueue branch from b322388 to 620fcbb Compare May 16, 2017 07:00
@pushkarnk
Copy link
Member

@swift-ci test

@pushkarnk
Copy link
Member

@mamabusi I'd suggest we have a comment making it clear that we will NOT make a user supplied OperationQueue serial.

@@ -225,7 +226,9 @@ open class URLSession : NSObject {
identifier = nextSessionIdentifier()
self.workQueue = DispatchQueue(label: "URLSession<\(identifier)>")
self.taskAttributesIsolation = DispatchQueue(label: "URLSession<\(identifier)>.taskAttributes", attributes: DispatchQueue.Attributes.concurrent)
self.delegateQueue = queue ?? OperationQueue()
let operationQueue = OperationQueue()
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't create an OperationQueue unless we need to. This creates one in all cases. How about this:

if queue != nil {
    self.delegateQueue = queue
} else {
    self.delegateQueue = OperationQueue()
    self.delegateQueue.maxConcurrentOperationCount = 1
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed!

@mamabusi mamabusi force-pushed the urlSesSerialDelegQueue branch from 620fcbb to 6f7ea2c Compare May 16, 2017 10:08
@mamabusi
Copy link
Contributor Author

@pushkarnk @ianpartridge Review changes and suggestions are addressed. Thanks!

@ianpartridge
Copy link
Contributor

Thanks @mamabusi. @pushkarnk - can you re-run the CI?

@pushkarnk
Copy link
Member

Looks good to me. Thanks!

@pushkarnk
Copy link
Member

Just for the records, here's what @ianpartridge found: https://www.malhal.com/2017/01/07/nsoperationqueue-maxconcurrentoperationcount-1-serial-queue-problem/ (though it is unlikely that a user will supply a delegate method that will block indefinitely)

@pushkarnk
Copy link
Member

@swift-ci test

@pushkarnk
Copy link
Member

@swift-ci test and merge

@swift-ci swift-ci merged commit 5a0e57d into swiftlang:master May 16, 2017
@mamabusi mamabusi deleted the urlSesSerialDelegQueue branch December 22, 2017 08:10
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.

4 participants