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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

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
1 participant