-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix a memory leak in DownloadTaskManager
and DataTaskManager
#7408
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
ahoppen
merged 1 commit into
swiftlang:main
from
ahoppen:ahoppen/download-data-task-manager-leak
Mar 20, 2024
Merged
Fix a memory leak in DownloadTaskManager
and DataTaskManager
#7408
ahoppen
merged 1 commit into
swiftlang:main
from
ahoppen:ahoppen/download-data-task-manager-leak
Mar 20, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@swift-ci Please test |
MaxDesiatov
reviewed
Mar 19, 2024
MaxDesiatov
reviewed
Mar 19, 2024
MaxDesiatov
reviewed
Mar 19, 2024
179445f
to
3572bdc
Compare
@swift-ci Please test |
@swift-ci test windows |
MaxDesiatov
reviewed
Mar 20, 2024
MaxDesiatov
reviewed
Mar 20, 2024
MaxDesiatov
reviewed
Mar 20, 2024
`DownloadTaskManager` had a strong reference to `URLSession`, which retained its delegate, which was the `DownloadTaskManager`. This formed a retain cycle. Make the reference from `URLSession` to the delegate weak and only keep `DownloadTaskManager` alive as long as `DownloadTask`s need it. This causes the `DownloadTaskManager` to be deallocated once nobody holds a reference to it anymore and all the tasks it manages are finished. Finally, we need to call `invalidate` on the `URLSession` to tell it that we’re done and that it should release its delegate (which is now the `WeakDownloadTaskManager`wrapper). rdar://125012584
3572bdc
to
614a502
Compare
@swift-ci Please test |
@swift-ci Please test Windows |
MaxDesiatov
approved these changes
Mar 20, 2024
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!
ahoppen
added a commit
that referenced
this pull request
Mar 25, 2024
#7416) * **Explanation**: `DownloadTaskManager` had a strong reference to `URLSession`, which retained its delegate, which was the `DownloadTaskManager`. This formed a retain cycle. Make the reference from `URLSession` to the delegate weak and only keep `DownloadTaskManager` alive as long as `DownloadTask`s need it. This causes the `DownloadTaskManager` to be deallocated once nobody holds a reference to it anymore and all the tasks it manages are finished. Finally, we need to call `invalidate` on the `URLSession` to tell it that we’re done and that it should release its delegate (which is now the `WeakDownloadTaskManager` wrapper). This retain cycle was causing a leak in sourcekit-lsp. I verified that the leak no longer exists with this patch. * **Scope**: The change should be transparent to all users of `DataTaskManager` and `DownloadTaskManager` and only prevent the memory leak * **Risk**: I don’t see a risk with this change since the `{Data|Download}TaskManager` is kept alive as long as there is anything left that might call it. * **Testing**: I verified that the leak no longer exists with this patch. * **Issue**: rdar://125012584 * **Reviewer**: @MaxDesiatov on #7408
furby-tm
pushed a commit
to wabiverse/swift-package-manager
that referenced
this pull request
May 15, 2024
…ftlang#7408) `DownloadTaskManager` had a strong reference to `URLSession`, which retained its delegate, which was the `DownloadTaskManager`. This formed a retain cycle. Make the reference from `URLSession` to the delegate weak and only keep `DownloadTaskManager` alive as long as `DownloadTask`s need it. This causes the `DownloadTaskManager` to be deallocated once nobody holds a reference to it anymore and all the tasks it manages are finished. Finally, we need to call `invalidate` on the `URLSession` to tell it that we’re done and that it should release its delegate (which is now the `WeakDownloadTaskManager`wrapper). This retain cycle was causing a leak in sourcekit-lsp. I verified that the leak no longer exists with this patch. rdar://125012584
furby-tm
pushed a commit
to wabiverse/swift-package-manager
that referenced
this pull request
May 15, 2024
…ftlang#7408) `DownloadTaskManager` had a strong reference to `URLSession`, which retained its delegate, which was the `DownloadTaskManager`. This formed a retain cycle. Make the reference from `URLSession` to the delegate weak and only keep `DownloadTaskManager` alive as long as `DownloadTask`s need it. This causes the `DownloadTaskManager` to be deallocated once nobody holds a reference to it anymore and all the tasks it manages are finished. Finally, we need to call `invalidate` on the `URLSession` to tell it that we’re done and that it should release its delegate (which is now the `WeakDownloadTaskManager`wrapper). This retain cycle was causing a leak in sourcekit-lsp. I verified that the leak no longer exists with this patch. rdar://125012584
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
DownloadTaskManager
had a strong reference toURLSession
, which retained its delegate, which was theDownloadTaskManager
. This formed a retain cycle.Make the reference from
URLSession
to the delegate weak and only keepDownloadTaskManager
alive as long asDownloadTask
s need it. This causes theDownloadTaskManager
to be deallocated once nobody holds a reference to it anymore and all the tasks it manages are finished.Finally, we need to call
invalidate
on theURLSession
to tell it that we’re done and that it should release its delegate (which is now theWeakDownloadTaskManager
wrapper).This retain cycle was causing a leak in sourcekit-lsp. I verified that the leak no longer exists with this patch.
rdar://125012584