Async task and event loop safety in Megatron Core - #2025
Merged
Conversation
tdene
commented
Oct 29, 2025
@tdene, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/ |
Contributor
Author
|
/ok to test 7bc898f |
Contributor
Author
|
/ok to test |
Contributor
|
@santhnm2 @sidsingh-nvidia @shanmugamr1992 can you please review this MR. |
shanmugamr1992
approved these changes
Nov 6, 2025
Contributor
Author
|
/ok to test d46b20e |
kvareddy
approved these changes
Nov 7, 2025
Contributor
Author
|
/ok to test c73a802 |
This reverts commit aeb4859.
Contributor
Author
|
/ok to test 8f5772b |
Contributor
Author
|
/ok to test 230e26e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do ?
Python has several idiosyncrasies when it comes to
asyncio. This PR addresses two of them by creating helper methods and establishing foolproof coding philosophy.The first problematic behavior is that coroutines that run inside a task fail silently, causing an eternal hang. While this is intended, and useful in some programming patterns, in our current usecase of
asynciotasks it does nothing but lead to untraceable silent deadlocks. A common opinion is that handling such behavior in the official, Pythonic, way, causes unnecessary code complication.This PR's first commit defines a wrapper that, when placed around a coroutine that gets called by a task, makes the coroutine's failure loud and traceable. It is the experience of the Megatron-RL team that this utility can reduce debugging time by days or weeks.
The second problematic behavior is that it is not rare for a running program to involve itself with multiple event loops, if not intentionally, then by accident. The Megatron Inference team has already encountered this issue and dealt with it through a
get_asyncio_loophelper. This PR's second commit standardizes the use of this helper across the current asyncio codebase. It also allows for loop objects to be passed around manually; the Megatron-RL team has witnessed that this code pattern greatly simplifies the debugging process.In addition, this PR's first commit preserves some async logging logic that Megatron-RL development heavily relies on. This logic is optional, and turned off by default.
Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.