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

fix(aiohttp): Remove strong reference to response object on callback (#8108) #11518

Conversation

seandstewart
Copy link
Contributor

@seandstewart seandstewart commented Nov 23, 2024

The previous implementation added a lambda which held a reference to the response object to the request task, creating a strong reference to the request object which could lead to a memory leak, because the request and response cannot be garbage collected until the task itself is.

This reworks the middleware to store the request span in a context var and use only that context var for the task callback, ensuring that the request and response objects can be freed from memory even if the underlying task has not been garbage collected.

I haven't been able to get my local environment working correctly, so I have not been able to test this behavior to ensure correctness.

Attempts to resolve #8108

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@seandstewart seandstewart requested review from a team as code owners November 23, 2024 15:56
@seandstewart seandstewart requested review from sanchda, erikayasuda and wconti27 and removed request for a team November 23, 2024 15:56
@seandstewart seandstewart changed the base branch from 2.x to main November 23, 2024 15:59
@seandstewart seandstewart requested a review from a team as a code owner November 23, 2024 15:59
@seandstewart seandstewart force-pushed the seandstewart/8108/remove-strong-response-ref branch 3 times, most recently from fec0e96 to e87fdf7 Compare November 24, 2024 13:13
…g responses(DataDog#8108)

Without this change, the request and response objects are not freed from memory until the asyncio Task is freed, which can create a memory leak. This change leverages a contextvar to accomplish the same result as the previous version, while ensuring any memory is freed once the current async context is exited.
@seandstewart seandstewart force-pushed the seandstewart/8108/remove-strong-response-ref branch from e87fdf7 to 446c43b Compare November 24, 2024 13:19
@seandstewart
Copy link
Contributor Author

Failed test is for the botocore integration, which was not impacted by this change.

@seandstewart
Copy link
Contributor Author

image

I vendored and deployed this change to an application in my organization. The purple line is ddtrace's current implementation, the yellow is the implementation in this MR.

As you can see, the memory increases linearly in the current version in this library, but stabilizes in this proposed change.

Additionally, you can see the pending GC count increases linearly without this change, but resets periodically with the change, indicating the current version does block garbage collection (the drops in the purple line are when the app is rolled to the new version):

image

@wantsui
Copy link
Collaborator

wantsui commented Dec 3, 2024

Hi @seandstewart, thanks for contributing this PR!

Just as a heads up, I'm duplicating this PR so the rest of the test suite runs: #11611 ! I'm also reviewing this PR to get this merged in.

@chamini2
Copy link

chamini2 commented Dec 9, 2024

Hi @seandstewart, very thankful for you to take the time to do this. Do you have any time to make tests pass so we can try to get this merged? We have been having memory leak issues for a while now.

Let me know if I can help in any way to get this done.

@seandstewart
Copy link
Contributor Author

Hi @seandstewart, very thankful for you to take the time to do this. Do you have any time to make tests pass so we can try to get this merged? We have been having memory leak issues for a while now.

Let me know if I can help in any way to get this done.

Hey @chamini2 - see @wantsui's comment above. I am not a DataDog employee, just a contributor. They have opened their own PR with my changes and are working on that.

@sanchda
Copy link
Contributor

sanchda commented Dec 10, 2024

Looks like this was duplicated in #11611. I'm going to go ahead and close this out, but the author's code changes are being taken. Thank you very much for the contribution!!

@sanchda sanchda closed this Dec 10, 2024
@wantsui
Copy link
Collaborator

wantsui commented Jan 27, 2025

Just a heads up that I closed the cloned version of this PR in favor of @ZStriker19 's #12081 .

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.

Memory leak in aiohttp integration since v2.2.0
4 participants