-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 NSUrlSession memory leak by finishing session #1480
Conversation
… indicate the session is finished (ktorio#1420)
Do you know, if |
HI @bherbst, thanks for the PR, seems like it fixes a significant issue. Speaking about the change, do I understand correctly that |
The other alternative would be setting |
I'll start by saying I'm certainly no iOS networking expert, but here's my understanding:
The docs for
Not necessarily- after talking to some iOS developers my belief is that most iOS developers use NSURLSession a bit differently than KTOR does. Instead of creating a new NSURLSession for each network request, they typically have a singleton session that they reuse for all requests. Since it is being reused, the session can't be invalidated. A |
isn't this the same issue that was resolved here? #1342 |
@matarasso Indeed it looks to be the same! Good find. Unfortunately that PR was only ever merged into ktorio:e5l/develop. I've updated this PR to move the |
A shame that this couldn't make it to 1.3.0, I've been using my own fork for months because this is a show stopper for an app that downloads hundreds of files such as ours. |
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.
Thanks. LGTM
Sorry for the delay, it took some time to clarify details
Subsystem
iOS client engine
Motivation
#1420 describes a memory leak in the iOS client. I have posted more details in that issue, but the gist of it is that when opening an
NSUrlSession
usingsessionWithConfiguration()
, thedelegate
is leaked until the app exits unless we callinvalidateAndCancel()
orfinishTasksAndInvalidate()
at some point. See the documentation for thedelegate
parameter on developer.apple.com for more information.In effect, this meant that the engine, the session, and the coroutine used for executing the request are all leaked until the app dies.
Solution
After adding the data task to
NSUrlSession
, we immediate callfinishTasksAndInvalidate()
, which blocks creation of new tasks but allows the existing data task to complete. Once that data task completes,NSUrlSession
will then break references to the delegate and callback objects, allowing those objects to become garbage collected.