[v3-1-test] Fix memory leak in Client via SSL context creation (#57334) #57374
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
related: #56641
When I performed the same memray inspection on the latest version that includes both fixes, the first issue was clearly resolved, but the second issue still persists.
memray1.html
When a Client object is created,
ctx = ssl.create_default_context(cafile=ca_file)continues to be executed repeatedly, which accumulates in memory and causes a memory leak. (It appears to be allocated as a C language object and remains in memory regardless of Python object GC)This PR uses caching to prevent the SSL context object from being recreated.
Here are the results after running for two hours with this change. The memory usage, which was previously growing to tens of MBs, now stabilizes at approximately 700KB.
memray2.html
(cherry picked from commit 7369e46)
Co-authored-by: Jeongwoo Do 48639483+wjddn279@users.noreply.github.com