-
Notifications
You must be signed in to change notification settings - Fork 426
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
fix(aiohttp): Remove strong reference to response object on callback (#8108) #11518
Conversation
fec0e96
to
e87fdf7
Compare
…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.
e87fdf7
to
446c43b
Compare
Failed test is for the botocore integration, which was not impacted by this change. |
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): |
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. |
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. |
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!! |
Just a heads up that I closed the cloned version of this PR in favor of @ZStriker19 's #12081 . |
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
Reviewer Checklist