Skip to content

Fix memory leak of URLSession in SocketEngine #1524

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adysart
Copy link

@adysart adysart commented Aug 6, 2025

SocketEngine retains an instance of URLSession, and this value is replaced in resetEngine(). But this instance of URLSession is leaked unless you call invalidateAndCancel(), which breaks the references between the session and its delegate and callbacks.

Note in contrast to other types in Foundation and UIKit, URLSession strongly retains its delegate. So the default configuration of SocketEngine still has a retain cycle since it sets itself as the delegate of the URLSession that it owns. Breaking this retain cycle requires user of the library to provide a custom URLSessionDelegate during configuration.

`SocketEngine` retains an instance of `URLSession`, and this value is replaced in `resetEngine()`. But this instance of `URLSession` is leaked unless you call `invalidateAndCancel()`, which breaks the references between the session and its delegate and callbacks.

Note in contrast to other types in Foundation and UIKit, `URLSession` _strongly_ retains its delegate. So the default configuration of `SocketEngine` still has a retain cycle since it sets itself as the delegate of the `URLSession` that it owns. Breaking this retain cycle requires user of the library to provide a custom `URLSessionDelegate` during configuration.
@adysart adysart changed the title Fix memory leak or URLSession in SocketEngine Fix memory leak of URLSession in SocketEngine Aug 6, 2025
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.

1 participant