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

Memory leak in Python target fixed #246

Merged
merged 13 commits into from
Jul 11, 2023
Merged

Memory leak in Python target fixed #246

merged 13 commits into from
Jul 11, 2023

Conversation

jackyk02
Copy link
Contributor

@jackyk02 jackyk02 commented Jun 28, 2023

This pull request addresses a memory leak issue observed in the current implementation. As shown in the following screenshot, it is noticeable that with each iteration, the size of Python objects constructed using C extensions continues to grow.

Untitled picture

This problem could be attributed to several key issues, which are as follows:

  1. PyObject Reference Count: The reference counts for the majority of PyObjects are not being appropriately managed.

  2. Port Assignment: Rather than using the _LF_SET macro to assign values to the generic_port_instance_struct in the current implementation, the more suitable method would be to use lf_set_token and define a destructor for the token to ensure proper garbage collection.

  3. Function Definition: The existing _lf_free_token_value function is incorrect and fails to invoke the destructor.

Comments have been added to the code within the commits to provide additional context and insight into the changes made. Other related changes have also been made in the code generator repository (LINK).

Please feel free to comment if further modifications are required. Similar issues persist during the federated multiport setting. To address this, a separate PR will be submitted later.

Copy link
Contributor

@edwardalee edwardalee left a comment

Choose a reason for hiding this comment

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

Fantastic! Some minor suggestions included.

core/lf_token.c Outdated Show resolved Hide resolved
python/lib/python_port.c Outdated Show resolved Hide resolved
python/lib/python_port.c Outdated Show resolved Hide resolved
python/lib/python_port.c Outdated Show resolved Hide resolved
python/lib/python_port.c Show resolved Hide resolved
Copy link
Member

@lhstrh lhstrh left a comment

Choose a reason for hiding this comment

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

Looks good! I left some minor comments...

core/lf_token.c Outdated Show resolved Hide resolved
core/lf_token.c Outdated Show resolved Hide resolved
core/lf_token.c Outdated Show resolved Hide resolved
python/lib/python_port.c Outdated Show resolved Hide resolved
Copy link
Contributor

@petervdonovan petervdonovan left a comment

Choose a reason for hiding this comment

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

Other than the lingering comments, this looks good to me. I like that it hooks the reference count into our destructor mechanism -- this seems like the right way to get our memory management system to interact properly with CPython's. As before, I'm really grateful that I didn't have to do this because this looks really hard.

python/lib/python_port.c Outdated Show resolved Hide resolved
Copy link
Contributor

@petervdonovan petervdonovan left a comment

Choose a reason for hiding this comment

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

We should not merge this yet because there are test failures; see the discussion at lf-lang/lingua-franca#1873

jackyk02 and others added 12 commits July 5, 2023 20:56
Co-authored-by: Edward A. Lee <eal@eecs.berkeley.edu>
It was incorporated to assist with debugging.
Co-authored-by: Edward A. Lee <eal@eecs.berkeley.edu>
Co-authored-by: Edward A. Lee <eal@eecs.berkeley.edu>
Co-authored-by: Marten Lohstroh <marten@berkeley.edu>
Co-authored-by: Marten Lohstroh <marten@berkeley.edu>
The function is called by the deserializer during federated execution.
This commit fixes segfault for unfederated execution, but leads to memory leak during federated execution.
@petervdonovan petervdonovan force-pushed the memory_leak_fix branch 2 times, most recently from df0379b to d28a9f5 Compare July 10, 2023 19:58
Copy link
Contributor

@petervdonovan petervdonovan left a comment

Choose a reason for hiding this comment

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

Tests seem to be passing without a segfault in the companion PR, and the most problematic memory leak seems to be fixed. I think this is ready to merge (as soon as the companion PR can also be merged).

Copy link
Member

@lhstrh lhstrh left a comment

Choose a reason for hiding this comment

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

🚀 🚀 🚀

@lhstrh lhstrh changed the title Memory Leak during Federated Execution Memory leak in Python target fixed Jul 11, 2023
@lhstrh lhstrh merged commit 294fbb2 into main Jul 11, 2023
12 of 25 checks passed
@lhstrh lhstrh deleted the memory_leak_fix branch July 11, 2023 00:20
@lhstrh lhstrh added the bugfix label Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants