-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix retain cycle between
URLSessionHTTPClient
and `URLSession.deleg…
…ate` (#335) See #334 for more information on the underlying issue. This PR creates a private `URLSessionDelegateWrapper` class to act as an intermediary between `URLSessionHTTPClient` and `URLSession.delegate`. From the inline documentation I added: ``` /// This class exists to avoid a retain cycle between `URLSessionHTTPClient` and its underlying /// `URLSession.delegate`. Since `URLSession` retains its `delegate` strongly, setting the /// `URLSessionHTTPClient` directly as the `delegate` will cause a retain cycle: /// https://developer.apple.com/documentation/foundation/urlsession/1411597-init#parameters /// /// To work around this, `URLSessionDelegateWrapper` maintains a `weak` reference to the /// `URLSessionHTTPClient` and passes delegate calls through to it, avoiding the retain cycle. ``` The client now also calls `self.session.finishTasksAndInvalidate()` on `deinit`. This workaround isn't the _cleanest_, but it does solve the issue and I'm not sure there's a better way of doing it. Open to suggestions, though. I also made the builder function in the example app `static` to avoid capturing `self` in the builder closures. With these changes, I was able to confirm the cycle demonstrated in the above issue is resolved in the memory debugger: <img width="544" alt="Screenshot 2025-01-09 at 9 56 03 AM" src="https://github.com/user-attachments/assets/713a59b8-958f-4a61-84fe-4c05eaaba83e" /> Note that I avoided changing the `open func` signatures of `URLSessionHTTPClient` since that would be a breaking API change. Resolves #334. --------- Signed-off-by: Michael Rebello <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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