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

Suggested setup for automatically tracking network requests causes memory leaks #2235

Open
Noobish1 opened this issue Mar 17, 2025 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@Noobish1
Copy link

Noobish1 commented Mar 17, 2025

Describe the bug

Hi there. We noticed recently that the steps you suggest to track network requests automatically with RUM cause memory leaks due to an important detail that Apple notes in their docs when using the URLSession.init(configuration:delegate:delegateQueue:) initializer.

Your instructions suggest implementing automatic network network request tracking like so:

URLSessionInstrumentation.enable(
    with: .init(
        delegateClass: <YourSessionDelegate>.self
    )
)

let session = URLSession(
    configuration: .default,
    delegate: <YourSessionDelegate>(),
    delegateQueue: nil
)

In our implementation we have any empty delegate like this:

public final class OurURLSessionDelegate: NSObject, URLSessionDataDelegate {}

The Apple docs for the URLSession.init(configuration:delegate:delegateQueue:) initializer have this warning for the delegate parameter:

Important
The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you do not invalidate the session by calling the invalidateAndCancel() or finishTasksAndInvalidate() method, your app leaks memory until it exits.

This means that if you don't call invalidateAndCancel() or finishtasksandinvalidate() on URLSession's after use you'll end up with memory leaks.

Example of some leaked URLSessionDelegates:

Image

I would suggest that you at least mention this detail in your docs and possibly look at other ways of implementing automatic network request tracking.

Reproduction steps

Create some URLSession's with the suggested implementation and have a look at Xcode's Memory Debugger or Instruments afterwards.

SDK logs

No response

Expected behavior

No response

Affected SDK versions

We're on 2.22.1 but any version with this setup would be affected

Latest working SDK version

The one before this tracking was added I guess?

Did you confirm if the latest SDK version fixes the bug?

No

Integration Methods

SPM

Xcode Version

No response

Swift Version

No response

MacOS Version

No response

Deployment Target

No response

Device Information

No response

Other relevant information

No response

@Noobish1 Noobish1 added the bug Something isn't working label Mar 17, 2025
@maxep
Copy link
Member

maxep commented Mar 17, 2025

Hey @Noobish1 👋

Thanks for the report 👍 Our instrumentation does not generate a leak directly, URLSession does, so I will remove the bug tag. Tho I agree that since our instrumentation relies on session's delegate, we should mention this in doc. I will make an update.

@maxep maxep added documentation Improvements or additions to documentation and removed bug Something isn't working labels Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants