Skip to content

Conversation

@wjddn279
Copy link
Contributor

related: #56641

@kaxil has addressed the second item in this issue. 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


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@amoghrajesh
Copy link
Contributor

@wjddn279 I am working on a fix for the failing tests, do not worry about that one :)

@amoghrajesh
Copy link
Contributor

Fix here: #57335

Copy link
Contributor

@amoghrajesh amoghrajesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the investigation @wjddn279!

A new client is created per task execution, that number could be high in a busy environment. Looks like a nice improvement.

@kaxil kaxil added this to the Airflow 3.1.2 milestone Oct 27, 2025
Copy link
Member

@kaxil kaxil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff @wjddn279 , left minor comments. Once you fix it, will merge it for 3.1.2

@wjddn279 wjddn279 force-pushed the fix-ssl-context-memory-leak branch from c417b1d to 7ca1a48 Compare October 27, 2025 12:52
@wjddn279
Copy link
Contributor Author

@kaxil @amoghrajesh Thanks for checking! 👍

@kaxil kaxil added the backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch label Oct 27, 2025
@kaxil kaxil merged commit 7369e46 into apache:main Oct 27, 2025
83 checks passed
github-actions bot pushed a commit that referenced this pull request Oct 27, 2025
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](https://github.com/user-attachments/files/23160226/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](https://github.com/user-attachments/files/23160405/memray2.html)
(cherry picked from commit 7369e46)

Co-authored-by: Jeongwoo Do <48639483+wjddn279@users.noreply.github.com>
@github-actions
Copy link

Backport successfully created: v3-1-test

Status Branch Result
v3-1-test PR Link

potiuk pushed a commit that referenced this pull request Oct 27, 2025
… (#57374)

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](https://github.com/user-attachments/files/23160226/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](https://github.com/user-attachments/files/23160405/memray2.html)
(cherry picked from commit 7369e46)

Co-authored-by: Jeongwoo Do <48639483+wjddn279@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants