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

Fixes to prevent memory leaks in RTI #446

Merged
merged 6 commits into from
Jul 1, 2024
Merged

Fixes to prevent memory leaks in RTI #446

merged 6 commits into from
Jul 1, 2024

Conversation

byeonggiljun
Copy link
Collaborator

@byeonggiljun byeonggiljun commented Jun 19, 2024

This PR resolves memory leaks from the RTI. I attached the output file from Valgrind.
valgrind-out-before.txt

With this PR, most of the leakages have disappeared.

valgrind-out-after.txt

Specifically, now the function pqueue_tag_remove_up_to frees dynamically allocated elements. Also, memory allocated for storing each node's upstream and min_delays is explicitly freed when the RTI terminates.

Valgrind reports two remaining issues. The two threads shown below are not explicitly joined.

lf_thread_create(&timeout_thread, wait_for_stop_request_reply, NULL);

This thread is created in the function handle_stop_request_message to set a timeout to wait for MSG_TYPE_STOP_REQUEST_REPLY messages. Thus, this thread cannot be joined in the function handle_stop_request_message and I didn't come up with joining this function.

lf_thread_create(&responder_thread, respond_to_erroneous_connections, NULL);

This thread is responsible for responding to erroneous connection requests. Thus, this thread should not be joined.

Summary by CodeRabbit

  • Bug Fixes

    • Improved thread management by adding a thread join for responder_thread to ensure proper synchronization.
    • Enhanced memory management by correcting memory deallocation in free_scheduling_nodes.
  • Refactor

    • Updated function calls within priority queue operations to improve performance and accuracy.

Copy link
Contributor

coderabbitai bot commented Jun 21, 2024

Walkthrough

Recent updates in the core/federated/RTI/rti_remote.c and core/utils/pqueue_tag.c files include thread synchronization and memory management enhancements. Specifically, adjustments were made to thread joining and deallocation processes, and a new function call was employed to manage priority queue operations, thereby ensuring more robust and efficient execution flow.

Changes

File Change Summary
core/federated/RTI/rti_remote.c Added thread join for responder_thread, set all_federates_exited to true, adjusted memory deallocation in free_scheduling_nodes, and modified its declaration.
core/utils/pqueue_tag.c Replaced pqueue_tag_pop() with pqueue_tag_pop_tag() in the loop within pqueue_tag_remove_up_to to enhance priority queue operations.

Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@byeonggiljun byeonggiljun marked this pull request as ready for review June 21, 2024 01:20
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 587a8f9 and c4a20c5.

Files selected for processing (2)
  • core/federated/RTI/rti_remote.c (2 hunks)
  • core/utils/pqueue_tag.c (1 hunks)
Additional comments not posted (3)
core/utils/pqueue_tag.c (1)

150-150: Approved change in pqueue_tag_remove_up_to function.

The replacement of pqueue_tag_pop with pqueue_tag_pop_tag in the loop is a significant improvement, as it ensures that dynamically allocated elements are properly freed, potentially preventing memory leaks.

core/federated/RTI/rti_remote.c (2)

1689-1690: Thread join operation added for responder_thread.

The addition of a thread join operation for responder_thread ensures that the RTI properly waits for this thread to complete before proceeding. This is crucial for avoiding potential race conditions or premature termination of threads which could lead to undefined behavior or resource leaks.


1752-1762: Enhanced memory deallocation in free_scheduling_nodes.

The changes made to free_scheduling_nodes function are critical for preventing memory leaks. The explicit deallocation of upstream, upstream_delay, min_delays, and downstream arrays, as well as the node itself, ensures that all dynamically allocated memory is properly freed. This is in line with best practices for memory management in C.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c4a20c5 and 52c5fce.

Files selected for processing (2)
  • core/federated/RTI/rti_remote.c (1 hunks)
  • core/utils/pqueue_tag.c (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • core/federated/RTI/rti_remote.c
  • core/utils/pqueue_tag.c

@lhstrh
Copy link
Member

lhstrh commented Jun 30, 2024

@edwardalee?

@lhstrh lhstrh changed the title Fix on the RTI's memory leak Fixes to prevent memory leaks in RTI Jul 1, 2024
@lhstrh lhstrh requested a review from cmnrd July 1, 2024 07:37
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.

LGTM!

@lhstrh lhstrh merged commit 7a08087 into main Jul 1, 2024
29 checks passed
@lhstrh lhstrh deleted the rti-memory-leak branch July 1, 2024 07:39
EhsanKhodadad pushed a commit to EhsanKhodadad/reactor-c that referenced this pull request Aug 1, 2024
* Free elements when removing them from a queue

* Free calloc'ed elements when terminating

* Explicitly join threads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants