Skip to content

Avoid calling destroy event inside host func - #278

Merged
MC952-arch merged 4 commits into
flagos-ai:mainfrom
MC952-arch:bug-fix
Oct 27, 2025
Merged

Avoid calling destroy event inside host func#278
MC952-arch merged 4 commits into
flagos-ai:mainfrom
MC952-arch:bug-fix

Conversation

@MC952-arch

@MC952-arch MC952-arch commented Oct 24, 2025

Copy link
Copy Markdown
Collaborator

This change refactors event management in flagcxHostSemaphore to prevent device context errors that may occur when destroying events inside host functions. Since host function behavior varies across different AI chips, event destruction is now handled in a context-safe manner to ensure consistent cross-platform stability.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @MC952-arch, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the event and semaphore lifecycle management within the flagcx framework. The primary goal is to enhance the robustness of asynchronous operations by ensuring that resources, particularly flagcxHostSemaphore objects and their associated events, are correctly and safely deallocated. This prevents potential issues such as use-after-free errors by introducing explicit start/end states and centralizing event destruction in the semaphore's destructor, alongside conditional semaphore deallocation based on operation completion.

Highlights

  • Semaphore Lifecycle Management: The flagcxHostSemaphore now uses distinct start and end flags instead of a single flag, providing more granular control over its state. Event destruction has been moved from the wait() method to the semaphore's destructor, ensuring proper cleanup when the semaphore object is deallocated.
  • Conditional Semaphore Deallocation: The logic for freeing flagcxHostSemaphore instances has been refined in proxy.cc. Semaphores are now freed only when the last associated operation is complete and the semaphore has signaled its end, preventing premature resource release.
  • Operation-Event Tracking: An eventId has been added to the flagcxProxyOp structure. This ID links each operation to a specific event within the semaphore, which is crucial for determining when it is safe to deallocate the semaphore.
Ignored Files
  • Ignored by pattern: .github/workflows/** (3)
    • .github/workflows/test.yml
    • .github/workflows/torch-api-test.yml
    • .github/workflows/unit-test.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the lifecycle management of flagcxHostSemaphore to avoid destroying events inside the host function. The ownership of the semaphore is transferred to the proxy progress thread, which now correctly waits for the host function to complete before cleaning up.

My review has identified a critical issue with the new implementation. The flagcxHostSemaphore struct contains a std::vector, making it a non-POD C++ object. However, it's being allocated with flagcxCalloc and deallocated with free. This is incorrect as it bypasses constructor and destructor calls, leading to memory leaks of both the events and the vector's internal buffer. I've provided suggestions to fix this by using new and delete for proper C++ object lifecycle management.

Additionally, I've pointed out a minor issue regarding a potentially unsafe cast from size_t to int.

Comment thread flagcx/core/launch_kernel.h Outdated
Comment thread flagcx/core/group.cc Outdated
Comment thread flagcx/core/proxy.cc Outdated
Comment thread flagcx/core/proxy.cc Outdated
Comment thread flagcx/core/proxy.cc Outdated
Comment thread flagcx/core/proxy.cc Outdated
@MC952-arch

Copy link
Copy Markdown
Collaborator Author

@gemini-code-assist please review this pull request in details

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors event management in flagcxHostSemaphore to improve stability by avoiding event destruction within host functions. The core change moves the event cleanup logic into the semaphore's destructor, which is now invoked from proxy.cc after the host function signals completion. This is a good architectural improvement. My review includes a critical fix for a memory management mismatch (calloc vs. delete) that could lead to undefined behavior, along with suggestions to improve code clarity and remove redundancy.

Comment thread flagcx/core/group.cc Outdated
Comment thread flagcx/core/group.cc Outdated
Comment thread flagcx/core/proxy.cc Outdated
Comment thread flagcx/core/proxy.cc Outdated
@MC952-arch

Copy link
Copy Markdown
Collaborator Author

@gemini-code-assist please review this pull request in details

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request aims to improve the stability of the flagcxHostSemaphore by managing event destruction in a context-safe manner. The changes include replacing flagcxCalloc with new for semaphore allocation, introducing signalStart and signalEnd methods, and handling event destruction in the semaphore's destructor. Additionally, an eventId is added to flagcxProxyOp to track semaphore events. The polling mechanism in flagcxProxySend and flagcxProxyRecv is updated to use pollStart. Finally, the semaphore deletion logic is adjusted to ensure only the last operation within a group releases the semaphore. I have identified a critical issue related to potential double freeing of the semaphore, which needs to be addressed.

Comment thread flagcx/core/proxy.cc Outdated
Comment on lines +203 to +207
if (op->args.semaphore->pollEnd()) {
delete op->args.semaphore;
flagcxIntruQueueDelete(queue, op);
free(op);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The condition op->args.semaphore->pollEnd() is checked before deleting the semaphore. However, op->args.semaphore might already be deleted in another thread if progressOps is executed concurrently for different operations belonging to the same semaphore. This could lead to a double-free scenario and crash the application.

To fix this, consider using a synchronization mechanism (e.g., a mutex or atomic flag) to ensure that only one thread can delete the semaphore at a time. Also, add a check to ensure that op->args.semaphore is not null before attempting to access it.

                    if (op->args.semaphore) { // Check if semaphore is not null
                      if (op->args.semaphore->pollEnd()) {
                        delete op->args.semaphore;
                        op->args.semaphore = nullptr; // Set to null after deletion
                        flagcxIntruQueueDelete(queue, op);
                        free(op);
                      }
                    }

@MC952-arch

Copy link
Copy Markdown
Collaborator Author

@gemini-code-assist please review this pull request in details

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors event management to improve stability by moving event destruction into the flagcxHostSemaphore destructor, managed by a std::shared_ptr. The changes are logical and move towards safer resource management. However, I've found a critical issue related to memory management of flagcxProxyOp which now contains a non-POD std::shared_ptr member but is still allocated with calloc and free. This must be fixed to prevent memory leaks and ensure destructors are called. I've also included a suggestion to further improve the flagcxHostSemaphore implementation for better C++ idiomaticity and robustness.

Comment thread flagcx/core/proxy.h
Comment thread flagcx/core/launch_kernel.h
@MC952-arch

Copy link
Copy Markdown
Collaborator Author

@gemini-code-assist please review this pull request in details

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors event management in flagcxHostSemaphore to prevent device context errors from destroying events inside host functions. The change to using std::shared_ptr for flagcxHostSemaphore is a great improvement, leveraging RAII for safer resource management. The logic is sound and correctly implemented across all modified files, enhancing both safety and code clarity. I have one suggestion to further improve the robustness of the flagcxHostSemaphore struct.

Comment thread flagcx/core/launch_kernel.h

@mikethegoblin mikethegoblin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@chencjcj chencjcj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@MC952-arch
MC952-arch merged commit 3ea0baf into flagos-ai:main Oct 27, 2025
3 checks passed
@MC952-arch
MC952-arch deleted the bug-fix branch November 27, 2025 07:24
liuhe-spec pushed a commit to liuhe-spec/FlagCX that referenced this pull request Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants