Skip to content
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

make sure writes to URLSession.taskRegistry happen on work queue #1625

Merged
merged 1 commit into from Jul 17, 2018

Conversation

ahti
Copy link
Contributor

@ahti ahti commented Jul 11, 2018

Addresses SR-8017.

The docs for taskRegistry specify any use should occur on the sessions workQueue, which wasn't respected with some of the removals.

Some of the removals previously happened on the tasks work queue, which has the session work queue as target and thus wouldn't cause issues, but I decided to make those explicitly use the sessions queue for clarity as well.

I'm not sure all callers of URLSession.behaviour(for:), the other place the taskRegistry is accessed, are run on the work queue, but the changes in this PR resolved the crashes I was seeing, so I didn't touch URLSession.behaviour(for:).

A test for this would be nice, but I couldn't really think of a way to test this, as the crashes occur randomly and require maybe a few thousand requests to semi-reliably reproduce.

@spevans
Copy link
Collaborator

spevans commented Jul 11, 2018

@swift-ci please test

Copy link
Collaborator

@pushkarnk pushkarnk left a comment

Choose a reason for hiding this comment

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

LGTM. I remember seeing some extremely intermittent crashes around task registry. They may well be due to the absence of this synchronisation.

@pushkarnk
Copy link
Collaborator

@swift-ci please test and merge

@pushkarnk
Copy link
Collaborator

Thanks @ahti !

@swift-ci swift-ci merged commit 497d1b8 into apple:master Jul 17, 2018
@ianpartridge
Copy link
Collaborator

@ahti @pushkarnk can we cherry pick this to 4.2 branch?

@ahti
Copy link
Contributor Author

ahti commented Jul 17, 2018

I would say yes, but I'm not yet familiar enough with the codebase to say so with much authority ^_^

@ianpartridge
Copy link
Collaborator

Please put up a PR and we'll get it merged. Otherwise, the fix won't be released until Swift 5 next year.

session.taskRegistry.remove(task)
}
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
session.workQueue.async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for necroposting but this and this https://github.com/apple/swift-corelibs-foundation/pull/1625/files#diff-14c6cb724eead661596e4468ae5ca638R625 looks like overkill
There is no thread switching with session.delegateQueue.addOperation
And if this method itself not synchronized race will be earlier at line 545

zayass pushed a commit to readdle/swift-android-toolchain that referenced this pull request Apr 11, 2019
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.

None yet

6 participants